package com.servlet;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class FileUpLoad extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//读取请求Body
byte[] body = readBody(request);
//取得所有Body内容的字符串表示
String textBody = new String(body, "ISO-8859-1");
//取得上传的文件名称
String fileName = getFileName(textBody);
//取得文件开始与结束位置
Position p = getFilePosition(request, textBody);
//输出至文件
writeTo(fileName, body, p);
} //构造类
class Position { int begin;
int end; public Position(int begin, int end) {
this.begin = begin;
this.end = end;
}
} private byte[] readBody(HttpServletRequest request) throws IOException {
//获取请求文本字节长度
int formDataLength = request.getContentLength();
//取得ServletInputStream输入流对象
DataInputStream dataStream = new DataInputStream(request.getInputStream());
byte body[] = new byte[formDataLength];
int totalBytes = 0;
while (totalBytes < formDataLength) {
int bytes = dataStream.read(body, totalBytes, formDataLength);
totalBytes += bytes;
}
return body;
} private Position getFilePosition(HttpServletRequest request, String textBody) throws IOException {
//取得文件区段边界信息
String contentType = request.getContentType();
String boundaryText = contentType.substring(contentType.lastIndexOf("=") + 1, contentType.length());
//取得实际上传文件的气势与结束位置
int pos = textBody.indexOf("filename=\"");
pos = textBody.indexOf("\n", pos) + 1;
pos = textBody.indexOf("\n", pos) + 1;
pos = textBody.indexOf("\n", pos) + 1;
int boundaryLoc = textBody.indexOf(boundaryText, pos) - 4;
int begin = ((textBody.substring(0, pos)).getBytes("ISO-8859-1")).length;
int end = ((textBody.substring(0, boundaryLoc)).getBytes("ISO-8859-1")).length; return new Position(begin, end);
} private String getFileName(String requestBody) {
String fileName = requestBody.substring(requestBody.indexOf("filename=\"") + 10);
fileName = fileName.substring(0, fileName.indexOf("\n"));
fileName = fileName.substring(fileName.indexOf("\n") + 1, fileName.indexOf("\"")); return fileName;
} private void writeTo(String fileName, byte[] body, Position p) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("e:/workspace/" + fileName); //这里的地址改成服务器中文件夹的地址
fileOutputStream.write(body, p.begin, (p.end - p.begin));
fileOutputStream.flush();
fileOutputStream.close();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'FileUpload.jsp' starting page</title>
<style type="text/css">
.mylabel{
height:33px;
width: 142px;
display: block;
overflow: hidden;
background: url(image/huang_1_btn.jpg);
}
.file{
display:none;
}
#show{
width : 600px;
height: 50px;
background-color:#efef00;
font-size: 21px;
color:#00f;
font-weight: bold;
line-height: 50px;
text-align: center;
overflow: hidden;
}
#sub{
display : block;
width : 142px;
height: 33px;
background-color:#e34545;
margin-top:10px;
line-height: 33px;
text-decoration: none;
color : #fff;
}
</style>
<script type="text/javascript">
function show(value){
var $show = document.getElementById("show");
$show.innerHTML= value;
var image = document.getElementById("image");
image.src = value;
}
function sub(){
var fo = document.getElementById("form1");
fo.submit();
}
</script>
</head> <body>
<center>
<form id="form1" action="http://localhost:8080/ServletFileUpload/servlet/FileUpLoad" method="POST" enctype="multipart/form-data">
<label class="mylabel">
<input type="file" name="image" class="file" onchange="show(this.value);"/>
</label>
</form>
<div id="show"> </div>
<a href="javascript:sub()" id="sub">上传文件</a>
</center>
</body>
</html>

  

Servlet图片上传的更多相关文章

  1. 一个简单的安卓+Servlet图片上传例子

    例子比较 简单,服务端为Java Web Servlet,doPost方法中接收图片并保存,然后将保存的图片名返回给客户端,关键代码: @SuppressWarnings("deprecat ...

  2. Java Servlet图片上传至指定文件夹并显示图片

    在学习Servlet过程中,针对图片上传做了一个Demo,实现的功能是:在a页面上传图片,点击提交后,将图片保存到服务器指定路径(D:/image):跳转到b页面,b页面读取展示绝对路径(D:/ima ...

  3. JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

    JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...

  4. JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)

    链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...

  5. commons-fileload图片文件上传工具 , servlet文件图片上传案列

    本案列是java  maven工程小项目,提供个大家学习! 1.在pom.xml文件中导入依赖: <!--文件上传依赖--><dependency> <groupId&g ...

  6. springmvc上传图片并显示图片--支持多图片上传

    实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...

  7. java web图片上传和文件上传

    图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定要写属性enctype=" ...

  8. CKEditor实现图片上传

    本人用的CKEditor版本为4.3 CKEditor配置和部署参考CKEditor4.x部署和配置. CKEditor编辑器的工具栏中初始的时候应该是这样子的,没有图片上传按钮 并且预览中有一堆火星 ...

  9. springMVC3 ckeditor3.6 图片上传 JS回调

    一.引入js文件 <script type="text/javascript" src="<%=base %>/resources/ckeditor/c ...

随机推荐

  1. 用websocket实现后台推送消息

    1前台实现 connect:function() { var webSocketIP = window.CRM_CONFIG.WebSocketIP; var target = 'ws://'+web ...

  2. 关于Mac中Clion使用OpenCV

    关于Mac中Clion使用OpenCV 目标 Clion能够使用OpenCV 步骤 下载安装cmake,官网下载 下载OpenCV mac/linux版 使用cmake gui编译opencv安装包, ...

  3. U31网管配置

    1.新建网元和子架配置: 在拓扑图空白处新建对象-创建承载传输网元-选择设备-填写网元名称(A).网元类型.IP(查询出SNP的),网关IP会自动在此基础上加2.如果数据库为空先选离线- 机架子架配置 ...

  4. 循序渐进看Java web日志跟踪(2)-Java日志API认识

    接触过Java的朋友应该都会知道,java的开源框架百花齐放,实现同样的功能,总能找到几个强大的开源框架来进行选择.在日志方面,Java同样不逊色.除了JDK本身自带的简单的日志工具,java还有如l ...

  5. 定位(position)

    position :属性规定元素的定位类型 语法: position : static | absolute | fixed | relative JavaScript语法:object.style. ...

  6. wpf 寻找某个控件下的子控件

    /// <summary> /// 寻找某个控件下的子控件 /// </summary> /// <typeparam name="ChildType" ...

  7. zabbix 布署实践【3 proxy安装】

    使用openstack在生产环境创建的一台虚拟机   环境 CentOS7 4核4G内存40G硬盘 IP:10.120.150.150 镜像默认关闭防火墙,selinux ,NetworkManage ...

  8. Spring Security(14)——权限鉴定基础

    目录 1.1     Spring Security的AOP Advice思想 1.2     AbstractSecurityInterceptor 1.2.1    ConfigAttribute ...

  9. double hashing 双重哈希

    二度哈希(rehashing / double hashing) 1.二度哈希的工作原理如下: 有一个包含多个哈希函数(H1……Hn)的集合.当我们要从哈希表中添加或获取元素时,首先使用哈希函数H1. ...

  10. Linux监控工具vmstat命令详解

        一.前言 很显然从名字中我们就可以知道vmstat是一个查看虚拟内存(Virtual Memory)使用状况的工具,但是怎样通过vmstat来发现系统中的瓶颈呢?在回答这个问题前,还是让我们回 ...