日期:2014-05-17 浏览次数:20886 次
package com.XXX.xheditor.servlet; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.lang.StringUtils; // xhEditor文件上传的Java - Servlet实现. // @ author WeiMiao // @ refer to easinchu // @ version 2 // @ description 增加html5上传功能的支持 public class UploadFileServlet extends HttpServlet { private static final long serialVersionUID = 1541334866883495283L; private static String baseDir = "/UploadFile/"; // 上传文件存储目录 private static String fileExt = "jpg,jpeg,bmp,gif,png"; private static Long maxSize = 0l; private static String dirType = "1"; // 0:不建目录 1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存 public void init() throws ServletException { baseDir = this.getInitParameter("baseDir"); //获取web.xml中servlet的配置文件目录参数 if (StringUtils.isEmpty(baseDir)) baseDir = "/UploadFile/"; //获取文件上传存储的相当路径 String realBaseDir = this.getServletConfig().getServletContext().getRealPath(baseDir); File baseFile = new File(realBaseDir); if (!baseFile.exists()) { baseFile.mkdir(); } fileExt = this.getInitParameter("fileExt"); //获取文件类型参数 if (StringUtils.isEmpty(fileExt)) fileExt = "jpg,jpeg,gif,bmp,png"; String maxSize_str = this.getInitParameter("maxSize"); //获取文件大小参数 if (StringUtils.isNotEmpty(maxSize_str)) { maxSize = new Long(maxSize_str); } else { maxSize = Long.valueOf("5242880"); //5M } dirType = this.getInitParameter("dirType"); //获取文件目录类型参数 if (StringUtils.isEmpty(dirType)) dirType = "1"; if (",0,1,2,3,".indexOf("," + dirType + ",") < 0) dirType = "1"; } // 上传文件数据处理过程 @SuppressWarnings( { "unchecked" }) public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); String err = ""; String newFileName = ""; if ("application/octet-stream".equals(request.getContentType())) { //HTML 5 上传 try { String dispoString = request.getHeader("Content-Disposition"); int iFindStart = dispoString.indexOf("name=\"")+6; int iFindEnd = dispoString.indexOf("\"", iFindStart); iFindStart = dispoString.indexOf("filename=\"")+10; iFindEnd = dispoString.indexOf("\"", iFindStart); String sFileName = dispoString.substring(iFindStart, iFindEnd); int i = request.getContentLength(); byte buffer[] = new byte[i]; int j = 0; while(j < i) { //获取表单的上传文件 int k = request.getInputStream().read(buffer, j, i-j); j += k; } if (buffer.length == 0) { //文件是否为空 printInfo(response, "上传文件不能为空", ""); return; } if (maxSize > 0 && buffer.length > maxSize) { //检查文件大小 printInfo(response, "上传