一、文件上傳的原理分析
1、文件上傳的必要前提
a、表單的method必須是post
b、表單的enctype屬性必須是multipart/form-data類型的。
enctype默認值:application/x-www-form-urlencoded
作用:告知服務器,請求正文的MIME類型
application/x-www-form-urlencoded : username=abc&password=123
ServletRequest.getParameter(String name);該方法是專門讀取該類型的方法
multipart/form-data:
2、借助commons-fileupload組件實現文件的上傳
a、拷貝jar包:commons-fileupload.jar commons-io.jar
b、實現原理
3、亂碼問題
a、普通字段的亂碼
FileItem.getString(String charset);編碼要和客戶端一致
b、上傳的中文文件名亂碼
request.setCharacterEncoding("UTF-8");編碼要和客戶端一致
4、具體實現
前台upload.jsp代碼如下 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>文件上傳</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="${pageContext.request.contextPath}/servlet/UploadServlet3" method="post" enctype="multipart/form-data"> name:<input name="name"/><br/> file1:<input type="file" name="f1"/><br/> file2:<input type="file" name="f2"/><br/> <input type="submit" value="上傳"> </form> </body> </html> 後台servlet代碼 package com.itheima.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.List; 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.FileItem; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.FilenameUtils; //詳解 public class UploadServlet3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); System.out.print(request.getRemoteAddr()); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(!isMultipart){ throw new RuntimeException("請檢查您的表單的enctype屬性,確定是multipart/form-data"); } DiskFileItemFactory dfif = new DiskFileItemFactory(); ServletFileUpload parser = new ServletFileUpload(dfif); // parser.setFileSizeMax(3*1024*1024);//設置單個文件上傳的大小 // parser.setSizeMax(6*1024*1024);//多文件上傳時總大小限制 List<FileItem> items = null; try { items = parser.parseRequest(request); }catch(FileUploadBase.FileSizeLimitExceededException e) { out.write("上傳文件超出了3M"); return; }catch(FileUploadBase.SizeLimitExceededException e){ out.write("總文件超出了6M"); return; }catch (FileUploadException e) { e.printStackTrace(); throw new RuntimeException("解析上傳內容失敗,請重新試一下"); } //處理請求內容 if(items!=null){ for(FileItem item:items){ if(item.isFormField()){ processFormField(item); }else{ processUploadField(item); } } } out.write("上傳成功!"); } private void processUploadField(FileItem item) { try { String fileName = item.getName(); //用戶沒有選擇上傳文件時 if(fileName!=null&&!fileName.equals("")){ fileName = UUID.randomUUID().toString()+"_"+FilenameUtils.getName(fileName); //擴展名 String extension = FilenameUtils.getExtension(fileName); //MIME類型 String contentType = item.getContentType(); if(contentType.startsWith("image/")){ //分目錄存儲:日期解決 // Date now = new Date(); // DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // // String childDirectory = df.format(now); //按照文件名的hashCode計算存儲目錄 String childDirectory = makeChildDirectory(getServletContext().getRealPath("/WEB-INF/files/"),fileName); String storeDirectoryPath = getServletContext().getRealPath("/WEB-INF/files/"+childDirectory); File storeDirectory = new File(storeDirectoryPath); if(!storeDirectory.exists()){ storeDirectory.mkdirs(); } System.out.println(fileName); item.write(new File(storeDirectoryPath+File.separator+fileName));//刪除臨時文件 } } } catch (Exception e) { throw new RuntimeException("上傳失敗,請重試"); } } //計算存放的子目錄 private String makeChildDirectory(String realPath, String fileName) { int hashCode = fileName.hashCode(); int dir1 = hashCode&0xf;// 取1~4位 int dir2 = (hashCode&0xf0)>>4;//取5~8位 String directory = ""+dir1+File.separator+dir2; File file = new File(realPath,directory); if(!file.exists()) file.mkdirs(); return directory; } private void processFormField(FileItem item) { String fieldName = item.getFieldName();//字段名 String fieldValue; try { fieldValue = item.getString("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("不支持UTF-8編碼"); } System.out.println(fieldName+"="+fieldValue); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5、關於臨時文件問題
a、DiskFileItemFactory
public void setRepository(File repository):設置臨時文件的存放目錄
public void setSizeThreshold(int sizeThreshold):設置緩存的大小
b、
文件上傳時,自己用IO流處理,一定要在流關閉後刪除臨時文件。FileItem.delete()
建議使用:FileItem.writer(File f).會自動刪除臨時文件。
6、限制文件的大小
a、
ServletFileUpload.setFileSizeMax(3*1024*1024);//設置單個文件上傳的大小
b、
ServletFileUpload.setSizeMax(6*1024*1024);//多文件上傳時總大小限制