首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
Web开发
> 详细
JSP&&SERVLET学习笔记(七):Servlet处理上传的文件
时间:
2014-12-25 01:29:28
阅读:
271
评论:
0
收藏:
0
[点我收藏+]
标签:
package cc.openhome;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.Position;
@WebServlet("/upload.do")
public class UploadServlet extends HttpServlet {
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;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//读取请求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);
}
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);
}
class Position{
int begin;
int end;
Position(int begin, int end){
this.begin = begin;
this.end = end;
}
}
private String getFilename(String reqBody){
String filename = reqBody.substring(reqBody.indexOf("filename = \"") + 10);
filename = filename.substring(0, filename.indexOf("\n"));
filename = filename.substring(filename.lastIndexOf("\\") + 1, filename.indexOf("\""));
return filename;
}
private void writeTo(String filename, byte[] body, Position p) throws FileNotFoundException, IOException{
FileOutputStream fileOutputStream = new FileOutputStream("c:/workspace/" + filename);
fileOutputStream.write(body, p.begin, (p.end - p.begin));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
JSP&&SERVLET学习笔记(七):Servlet处理上传的文件
标签:
原文地址:http://blog.csdn.net/u012965373/article/details/42134057
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
36.VUE — 认识 Webpack 和 安装
2021-07-28
【PHP】上传图片翻转问题
2021-07-28
php对数字进行万。亿的转化
2021-07-28
五个 .NET 性能小贴士
2021-07-28
Three.js中显示坐标轴、平面、球体、四方体
2021-07-28
.net 5+ 知新:【1】 .Net 5 基本概念和开发环境搭建
2021-07-27
1.html,css
2021-07-27
基于Docker搭建 Php-fpm + Nginx 环境
2021-07-27
nginx + http + svn
2021-07-27
kubernets kube-proxy的代理 iptables和ipvs
2021-07-26
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!