1 package biz; 2 3 import java.io.File; 4 import java.io.UnsupportedEncodingException; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 14 import org.apache.commons.fileupload.FileItem; 15 import org.apache.commons.fileupload.FileUploadException; 16 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 17 import org.apache.commons.fileupload.servlet.ServletFileUpload; 18 19 /** 20 * commons fileupload 包装类 21 * @author kiant 22 * @version Sep 9, 2008 23 */ 24 public class MutiFileUpload extends HttpServlet { 25 private Map<String, String> parameters; //保存普通 form 表单域 26 private Map<String,FileItem> files; //保存上传文件 27 28 private String encoding; //设置编码格式,推荐 jsp 和 处理类 均为 UTF-8 29 private Long sizeMax; //设置上传数据的最大数据 30 private int sizeThreshold; //设置内存缓冲区的阀值 31 private String repositoryPath; //文件超出缓冲区大小时的临时存放目录 32 33 private String uploadDir; //上传文件保存文件夹 34 35 36 /* 37 * methods 38 */ 39 /** 40 * 对 request 进行处理 41 * @throws UnsupportedEncodingException 42 */ 43 public void handleRequest(HttpServletRequest request) throws ServletException, UnsupportedEncodingException { 44 parameters = new HashMap<String, String>(); 45 files = new HashMap<String, FileItem>(); 46 47 //DiskFileItem工厂,主要用来设定上传文件的参数 48 DiskFileItemFactory factory = new DiskFileItemFactory(); 49 factory.setSizeThreshold(getSizeThreshold()); //设置内存缓冲区的阀值 50 if (getRepositoryPath() != null && getRepositoryPath() != "") { 51 factory.setRepository(new File(getRepositoryPath())); //临时目录,默认 52 } 53 54 // 使用fileItemFactory为参数实例化一个ServletFileUpload对象 55 ServletFileUpload upload = new ServletFileUpload(factory); 56 upload.setHeaderEncoding(getEncoding()); //设置编码格式,推荐 jsp 和 处理类 均为 UTF-8 57 upload.setSizeMax(getSizeMax()); //设置上传数据的最大数据 58 59 // 开始处理表单请求 60 try { 61 List items = upload.parseRequest(request); 62 Iterator it = items.iterator(); 63 while (it.hasNext()) { 64 FileItem item = (FileItem) it.next(); 65 if (item.isFormField()) { //如果是表单字段 66 String name = item.getFieldName(); 67 String value = item.getString(getEncoding()); 68 parameters.put(name, value); 69 } else { //如果是文件字段 70 String name = item.getFieldName(); 71 files.put(name, item); 72 } 73 } 74 75 } catch (FileUploadException e) { 76 e.printStackTrace(); 77 } 78 79 } 80 81 82 /* 83 * property accessors 84 */ 85 public Map<String, String> getParameters() { 86 return parameters; 87 } 88 public void setParameters(Map<String, String> parameters) { 89 this.parameters = parameters; 90 } 91 public Map<String, FileItem> getFiles() { 92 return files; 93 } 94 public void setFiles(Map<String, FileItem> files) { 95 this.files = files; 96 } 97 public String getEncoding() { 98 return encoding; 99 } 100 public void setEncoding(String encoding) { 101 this.encoding = encoding; 102 } 103 public Long getSizeMax() { 104 return sizeMax; 105 } 106 public void setSizeMax(Long sizeMax) { 107 this.sizeMax = sizeMax; 108 } 109 public int getSizeThreshold() { 110 return sizeThreshold; 111 } 112 public void setSizeThreshold(int sizeThreshold) { 113 this.sizeThreshold = sizeThreshold; 114 } 115 public String getRepositoryPath() { 116 return repositoryPath; 117 } 118 public void setRepositoryPath(String repositoryPath) { 119 this.repositoryPath = repositoryPath; 120 } 121 122 public String getUploadDir() { 123 return uploadDir; 124 } 125 126 public void setUploadDir(String uploadDir) { 127 this.uploadDir = uploadDir; 128 } 129 130 }
1 ** 2 * Method execute 3 * 4 * @param mapping 5 * @param form 6 * @param request 7 * @param response 8 * @return ActionForward 9 * @throws IOException 10 */ 11 public ActionForward execute(ActionMapping mapping, ActionForm form, 12 HttpServletRequest request, HttpServletResponse response) throws IOException { 13 14 try { 15 //利用组件读取 "multipart/form-data" 传输流文件的具体参数 16 getMutiFileUpload().handleRequest(request); 17 18 String opusName = getMutiFileUpload().getParameters().get("opusName"); 19 String comments = getMutiFileUpload().getParameters().get("comments"); 20 Integer cid = Integer.parseInt(getMutiFileUpload().getParameters().get("categorie0")); 21 int fullWidht = Integer.parseInt(getMutiFileUpload().getParameters().get("fullView")); 22 23 FileItem fileItem = getMutiFileUpload().getFiles().get("picFile"); 24 EcAccount account = (EcAccount)request.getSession().getAttribute("account"); 25 String fileDir = getMutiFileUpload().getUploadDir() + account.getLoginId(); // 保存目录 26 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS"); 27 String fileName = format.format(new Date()); // 保存文件名 28 fileName = fileDir + "/" + fileName; 29 String[] strType = fileItem.getContentType().split("/"); 30 String extName = strType[1]; //文件扩展名 31 32 //这里我省略了有效性的验证 33 34 //填充 opus实体 35 EcOpus opus = new EcOpus(); 36 opus.setEcAccount(account); 37 opus.setEcOpusCategorie(getEcOpusBiz().findCategorieID(cid)); 38 opus.setOpusName(opusName); 39 opus.setOpusComments(comments); 40 opus.setOriginalView(fileName + "o." + extName); 41 opus.setLinkView(fileName + "l." + extName); 42 opus.setSmallView(fileName + "s." + extName); 43 opus.setFullView(fileName + "f." + extName); 44 opus.setSubmitted(new Date()); 45 opus.setImageSize(0); 46 opus.setWidth(0); 47 opus.setHeight(0); 48 opus.setComment(0); 49 opus.setFavourite(0); 50 opus.setTodayView(0); 51 opus.setTotalView(0); 52 opus.setDeleteFlag(Byte.parseByte("0")); 53 54 // 获得写入路径 55 String absoluteDir = servlet.getServletContext().getRealPath("/").replaceAll("\\\\", "/"); 56 57 // 开始写入 58 getEcOpusBiz().addOpus(opus, absoluteDir, fileItem, fullWidht, extName); 59 60 return mapping.findForward("success"); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 return mapping.findForward("fail"); 64 } 65 }
1 // 原文件保存 2 File file = new File(absoluteDir + opus.getOriginalView()); 3 fileItem.write(file);
原文:https://www.cnblogs.com/biaogejiushibiao/p/9348045.html