藉助commons-fileupload實現檔案上傳,commonsfileupload

來源:互聯網
上載者:User

藉助commons-fileupload實現檔案上傳,commonsfileupload

一、檔案上傳的原理分析

    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);//多檔案上傳時總大小限制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.