package com.blue.servlet;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import com.blue.beans.ProgressInfo;
import com.blue.util.UploadListener;
import com.blue.util.Util;
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String FILE_UPLOAD_JSP = "FileUploadForm.jsp";
private static final String SUCCESS_JSP = "success.jsp";
private static final String INDEX = "index.jsp";
private static final String NEXT_SERVLET = "NextServlet";
private static final String PROGRESS_INFO = "PROGRESS_INFO";
private static final long MAX_LENGTH = 10 * 1024 * 1024L; // 10MB
private static final int HTTP_REDUNDANT_LENGTH = 1024; // 1KB
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getSession().removeAttribute(PROGRESS_INFO);
RequestDispatcher rd = request.getRequestDispatcher(FILE_UPLOAD_JSP);
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Util.info("FileUploadServlet", "posting");
ProgressInfo pi = new ProgressInfo();
request.getSession().setAttribute(PROGRESS_INFO, pi);
try {
this.doUpload(request, pi);
} catch (Exception e) {
pi.setInProgress(false);
Util.info("FileUploadServlet", "cancelled or interrupted!");
e.printStackTrace();
throw new ServletException(e);
}
// RequestDispatcher rd = request.getRequestDispatcher(NEXT_SERVLET);
RequestDispatcher rd = request.getRequestDispatcher(FILE_UPLOAD_JSP);
rd.forward(request, response);
}
private String getFileName(String fileName) {
String name = fileName.substring(fileName.lastIndexOf("\\") + 1);
return System.getProperty("java.io.tmpdir") + "/" + name;
}
private void doUpload(HttpServletRequest request, ProgressInfo pi)
throws Exception {
String fldName = "";
FileItemStream item = null;
BufferedInputStream stream = null;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
int filesSize = request.getContentLength() - HTTP_REDUNDANT_LENGTH;
if (filesSize >= MAX_LENGTH) {
String errorMsg = "Error: Current files size is " + filesSize
/ (1024 * 1024) + "MB which has exceeded max " + "10MB";
pi.setCompleted(true);
pi.setErrorMsg(errorMsg);
throw new Exception(errorMsg);
}
ServletFileUpload upload = new ServletFileUpload();
upload.setFileSizeMax(MAX_LENGTH);
upload.setSizeMax(MAX_LENGTH);
UploadListener uploadListener = new UploadListener(pi);
upload.setProgressListener(uploadListener);
// Parse the request
FileItemIterator fIter = upload.getItemIterator(request);
long curTotalLength = 0L;
while (fIter.hasNext()) {
item = fIter.next();
fldName = item.getFieldName();
Util.info("FileUploadServlet", fldName + ", "
+ item.isFormField() + ", " + item.getContentType()
+ ", " + item.getName() + ", " + item.getHeaders());
if (!item.isFormField()) {
fldName = item.getFieldName();
if (item.getName() == null
|| "".equals(item.getName().trim())) {
continue;
}
stream = new BufferedInputStream(item.openStream());
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
long bStreamLen = Streams.copy(stream, bStream, true);
// Util.info("++++++++bStreamLen >> " + bStreamLen);
curTotalLength = curTotalLength + bStreamLen;
pi.setCurFileName(item.getName());
pi.setUploadedFiles(pi.getUploadedFiles() + "<b>"
+ item.getName() + " (" + bStreamLen / 1024
+ "KB)</b><br/>");
Util.info("FileUploadServlet", this.getFileName(item
.getName()));
File file = new File(this.getFileName(item.getName()));
if (file.exists()) {
file.delete();
}
OutputStream out = new FileOutputStream(file);
bStream.writeTo(out);
Util.info("FileUploadServlet", fldName
+ " just was uploaded, len " + bStreamLen);
}
}
// pi.setTotalSize(curTotalLength/1024);
}
}
}
DWR + Servlet 实现文件上传功能
5星 · 超过95%的资源 需积分: 10 13 浏览量
2008-10-23
21:50:13
上传
评论
收藏 1.08MB RAR 举报
唐伯虎_
- 粉丝: 2
- 资源: 27
最新资源
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- python tkinter-08-盒子模型.ev4.rar
- Doozy UI Manager 2023
- 基于matlab实现夜间车牌识别程序(1).rar
- 基于matlab实现无线传感器网络无需测距定位算法matlab源代码 包括apit,dv-hop,amorphous在内的共7个
- 基于python的yolov5实现的旋转目标检测
- 基于matlab实现无线传感器网络 CAB定位仿真程序 这是无线传感器节点定位CAB算法的仿真程序,由matlab完成.rar
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
- 基于matlab实现视频监控中车型识别代码,自己写的,希望和大家多多交流.rar
- springcodespringcodespringcodespringcode
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论2