使用IDEA實現文件上傳


1、文件上傳

文件上傳的注意事項:

  1. 為保證服務器的安全,上傳文件應該放在外界無法直接訪問的目錄下,比如放於WEB-INF目錄下
  2. 為防止文件覆蓋的現象發生,要為上傳文件產生一個唯一的文件名(可使用-時間戳,-uuid,-MD5,-位運算加密算法等)
  3. 要限制上傳文件的最大值
  4. 可以限制上傳文件的類型,在收到上傳文件名時,判斷后綴名是否合法(可限制為.mp4,.txt,.doc...,.jpg,.png,.bmp...)

需要用到的類詳解:

ServletFileUpload負責處理上傳的文件數據,並將表單中每個輸入項封裝成一個FileItem對象,在使用ServletFileUpload對象解析請求時,需要DiskFileItemFactory對象。所以,我們需要在進行解析工作前構造好DiskFileItemFactory對象,通過ServletFileUpload對象的構造方法或setFileItemFacotry()方法設置ServletFileUpload對象的fileItemFactory屬性。

Tomcat項目發布路徑:

D:\environment\apache-tomcat-9.0.58-windows-x64\apache-tomcat-9.0.58\webapps\t9

項目代碼:

准備文件:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.kuang</groupId>
  <artifactId>file</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>6</source>
          <target>6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!--Servlet 依賴-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <!--JSP依賴-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
    </dependency>
    <!--standard標簽庫-->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <!--JSTL表達式的依賴-->
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
    </dependency>
    <!--連接數據庫-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.79</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.11.0</version>
    </dependency>
  </dependencies>
</project>
======================================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>
======================================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>$Title$</title>
    </head>
    <body>
    <%--通過表單上傳文件
        get: 上傳文件大小有限制
        post: 上傳文件大小沒有限制
    --%>
    <%--${pageContext.request.contextPath}獲取服務器路徑/file/index--%>
    <form action="${pageContext.request.contextPath}/upload.do" enctype="multipart/form-data" method="post">
        上傳用戶:<input type="text" name="username"><br/>
        <p><input type="file" name="file1"></p>
        <p><input type="file" name="file1"></p>
        <p><input type="submit"> | <input type="reset"></p>
    </form>
    </body>
</html>

源代碼:

public class FileServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //判斷上傳的文件是普通表單還是帶文件的表單
        if (!ServletFileUpload.isMultipartContent(request)){
            return; //中止方法運行,說明這是一個普通的表單,直接返回
        }

        //創建上傳文件的保存路徑,建議在WEB-INF路徑下,安全,用戶無法直接訪問上傳的文件;
        String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
        File uploadFile = new File(uploadPath);
        if (!uploadFile.exists()){
            uploadFile.mkdir(); //創建這個目錄
        }

        //緩存,臨時文件
        //臨時路徑,假如文件超過了預期的大小,我們就把他放到一個臨時文件中,過幾天自動刪除,或者提醒用戶轉存為永久
        String tmpPath = this.getServletContext().getRealPath("/WEB-INF/tmp");
        File file = new File(tmpPath);
        if (!file.exists()){
            file.mkdir(); //創建這個臨時目錄
        }

        //處理上傳的文件,一般都需要通過流來獲取,我們可以使用request.getInputStream(),原生態的文件上傳流獲取,十分麻煩
        //但是我們都建議使用Apache的文件上傳組件來實現,common-fileupload,它需要依賴於commons-io組件;

        /*
        ServletFileUpload負責處理上傳的文件數據,並將表單中每個輸入項封裝成一個FileItem對象,
        在使用ServletFileUpload對象解析請求時需要DiskFileItemFactory對象。
        所以,我們需要在進行解析工作前構造好DiskFileItemFactory對象,
        通過ServletFileUpload對象的構造方法或setFileItemFactory()方法設置ServletFileUpload對象的fileItemFactory屬性。
         */


        try {
            //1.創建DiskFileItemFactory對象,處理文件上傳路徑或者大小限制的;
            DiskFileItemFactory factory = getDiskFileItemFactory(file);
            //2.獲取ServletFileUpload
            ServletFileUpload upload = getServletFileUpload(factory);
            //3.處理上傳文件
            String msg = uploadParseRequest(upload,request,uploadPath);

            //servlet請求轉發消息
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("info.jsp").forward(request,response);

        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }

    public static DiskFileItemFactory getDiskFileItemFactory(File file){
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //通過這個工廠設置一個緩沖區,當上傳的文件大於這個緩沖區的時候,將他放到臨時文件中;
        factory.setSizeThreshold(1024 * 1024);//緩存區大小為1M
        factory.setRepository(file);//臨時目錄的保存目錄,需要一個File
        return factory;
    }

    public static ServletFileUpload getServletFileUpload(DiskFileItemFactory factory){
        ServletFileUpload upload = new ServletFileUpload(factory);

        //監聽文件上傳進度;
        upload.setProgressListener(new ProgressListener() {

            @Override
            //pBytesRead(l):已經讀取到的文件大小
            //pContentLength(l1):文件大小
            public void update(long l, long l1, int i) {
                System.out.println("總大小:"+l1+"已上傳:"+l);
            }
        });

        //處理亂碼問題
        upload.setHeaderEncoding("UTF-8");
        //設置單個文件的最大值
        upload.setFileSizeMax(1024 * 1024 * 10);
        //設置總共能夠上傳文件的大小
        //1024 = 1kb * 1024 = 1M * 10 = 10M
        upload.setSizeMax(1024 * 1024 * 10);
        return upload;
    }

    public static String uploadParseRequest(ServletFileUpload upload,HttpServletRequest request,String uploadPath) throws FileUploadException, IOException{

        String msg = "";

        //把前端請求解析,封裝成一個FileItem對象,需要從ServletFileUpload對象中獲取
        List<FileItem> fileItems = upload.parseRequest(request);
        //fileItem 每一個表單對象
        for (FileItem fileItem : fileItems) {
            //判斷上傳的文件是普通的表單還是帶文件的表單
            if (fileItem.isFormField()){
                //getFieldName指的是前端表單控件的name;
                String name = fileItem.getFieldName();
                String value = fileItem.getString("UTF-8");//處理亂碼
                System.out.println(name+":"+value);
            }else { //文件

                //================================處理文件=============================//
                String uploadFileName = fileItem.getName();
                //可能存在文件名不合法的情況
                if (uploadFileName.trim().equals("")||uploadFileName==null){
                    continue;
                }

                //獲得上傳的文件名 /images/girl/paojie.png
                String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/") + 1);
                //獲得文件的后綴名
                String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
                    /*
                    如果文件后綴名 fileExtName 不是我們所需要的
                    就直接return,不處理,告訴用戶文件類型不對。
                     */

                System.out.println("文件信息[文件名:"+fileName+"---文件類型"+fileExtName+"]");

                //可以使用UUID(唯一識別的通用碼),保證文件名唯一;
                //UUID.randomUUID(),隨機生成一個唯一識別的通用碼;

                //網絡傳輸中的東西,都需要序列化
                //POJO , 實體類 , 如果想要在多個電腦上運行,傳輸===>需要把對象都序列化了
                // JNI = Java Native Interface
                //implements Serializable : 標記接口,JVM會識別,如果實現了該接口則會進行一些操作
                //JVM---> Java棧 本地方法棧 native--->C++

                String uuidPath = UUID.randomUUID().toString();

                //===============================存放地址==============================//
                //存到哪? uploadPath
                //文件真實存在的路徑 realPath
                String realPath = uploadPath + "/" + uuidPath;
                //給每個文件創建一個對應的文件夾
                File realPathFile = new File(realPath);
                if (!realPathFile.exists()){
                    realPathFile.mkdir();
                }

                //===============================文件傳輸==============================//
                //獲得文件上傳的流
                InputStream inputStream = fileItem.getInputStream();

                //創建一個文件輸出流
                //realPath = 真實的文件夾;
                //查了一個文件;加上輸出文件的名字+"/"+uuidFileName
                FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName);

                //創建一個緩沖區
                byte[] buffer = new byte[1024*1024];

                //判斷是否讀取完畢
                int len = 0;
                //如果大於0說明還存在數據;
                while ((len=inputStream.read(buffer))>0){
                    fos.write(buffer,0,len);
                }

                //關閉流
                fos.close();
                inputStream.close();

                msg = "文件上傳成功!";
                fileItem.delete();//上傳成功,清除臨時文件
                //=============================文件傳輸完畢=========================//
            }
        }
        return msg;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM