Servlet图片上传
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图片上传的更多相关文章
- 一个简单的安卓+Servlet图片上传例子
例子比较 简单,服务端为Java Web Servlet,doPost方法中接收图片并保存,然后将保存的图片名返回给客户端,关键代码: @SuppressWarnings("deprecat ...
- Java Servlet图片上传至指定文件夹并显示图片
在学习Servlet过程中,针对图片上传做了一个Demo,实现的功能是:在a页面上传图片,点击提交后,将图片保存到服务器指定路径(D:/image):跳转到b页面,b页面读取展示绝对路径(D:/ima ...
- JSP+Servlet中使用jspsmartupload.jar进行图片上传下载
JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...
- JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)
链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...
- commons-fileload图片文件上传工具 , servlet文件图片上传案列
本案列是java maven工程小项目,提供个大家学习! 1.在pom.xml文件中导入依赖: <!--文件上传依赖--><dependency> <groupId&g ...
- springmvc上传图片并显示图片--支持多图片上传
实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...
- java web图片上传和文件上传
图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定要写属性enctype=" ...
- CKEditor实现图片上传
本人用的CKEditor版本为4.3 CKEditor配置和部署参考CKEditor4.x部署和配置. CKEditor编辑器的工具栏中初始的时候应该是这样子的,没有图片上传按钮 并且预览中有一堆火星 ...
- springMVC3 ckeditor3.6 图片上传 JS回调
一.引入js文件 <script type="text/javascript" src="<%=base %>/resources/ckeditor/c ...
随机推荐
- svn clean up 出错解决方案
问题描述:svn执行clean up命令时报错"Previous operation has not finished; run 'cleanup' if it was interrupte ...
- 让innerHTML获取的内容包含input和select(option)的最新值
function refreshData(){ var allInputObject=document.body.getElementsByTagName("input"); fo ...
- CentOS6.5 部署VPN管理系统(StrongSwan+iKEv2+Freeradiu+Mysql+Daloradius)
一.环境介绍 Server IP:192.168.30.133 System: CentOS 6.5 Client:Winodows 二.编译安装StrongSwan 1.下载StrongSwan w ...
- Java泛型的定义以及对于<? extends T>和<? super T>
Java 中对于泛型方法的定义: public <T> T getT(){ .....相关代码; } 其中我对<T>的理解就是申明只是一个泛型方法的标记,T是返回的类型. 对于 ...
- 通过Windows常见性能计数器分析服务器性能瓶颈
转载 http://www.cnblogs./junzhongxu/archive/2011/02/15/1954889.html 监测对象 Ø System(系统) l %Total Process ...
- POJ 2761 Feed the dogs
主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- 《JavaScript网页经典特效300例》
<JavaScript网页经典特效300例> 基础篇 进阶篇 高级篇
- java装饰模式
在java的IO中就是运用装饰模式设计的.一层装饰一层 如:DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(n ...
- juit测试中报错:org.hibernate.HibernateException: Unable to get the default Bean Validation factory
org.hibernate.HibernateException: Unable to get the default Bean Validation factory 解决方法: 解决方案: 在hib ...
- JAVA MONGODB group查询的UTC时间问题
BasicDBList dateList = new BasicDBList(); dateList.add("$t"); dateList.add(28800000); DBOb ...