1. 程式人生 > 實用技巧 >2020.11.28

2020.11.28

⽂件上傳下載

JSP

1、input 的 type 設定為 file

2、form 表單的 method 設定 post,get 請求會將⽂件名傳給服務端,⽽不是⽂件本身

3、form 表單的 enctype 設定 multipart/form-data,以⼆進位制的形式傳輸資料

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>Title</title>
</head>
<body>
 <form enctype="multipart/form-data" action="/upload" method="post">
 <input name="desc" type="text"/><br/>
 <input name="text" type="file"/><br/>
 <input type="submit" value="上傳"/>
 </form>
</body>
</html>

Servlet

fileupload 元件可以將所有的請求資訊都解析成 FileIteam 物件,可以通過對 FileItem 物件的操作完成 上傳,⾯向物件的思想。

package com.southwind.servlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.List; @WebServlet("/upload") public class UploadServlet extends
HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // //通過輸⼊流獲取客戶端傳來的資料流 // InputStream inputStream = req.getInputStream(); // Reader reader = new InputStreamReader(inputStream); // BufferedReader bufferedReader = new BufferedReader(reader); // //通過輸出流將資料流輸出到本地硬碟 // //獲取⽂件夾的絕對路徑 // String path = req.getServletContext().getRealPath("file/copy.txt"); // OutputStream outputStream = new FileOutputStream(path); // Writer writer = new OutputStreamWriter(outputStream); // BufferedWriter bufferedWriter = new BufferedWriter(writer); // String str = ""; // while((str = bufferedReader.readLine())!=null){ // System.out.println(str); // bufferedWriter.write(str); // } // bufferedWriter.close(); // writer.close(); // outputStream.close(); // bufferedReader.close(); // reader.close(); // inputStream.close(); try { DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory); List<FileItem> list = servletFileUpload.parseRequest(req); for(FileItem fileItem : list){ if(fileItem.isFormField()){ String name = fileItem.getFieldName(); String value = fileItem.getString("UTF-8"); System.out.println(name+":"+value); }else{ String fileName = fileItem.getName(); long size = fileItem.getSize(); System.out.println(fileName+":"+size+"Byte"); InputStream inputStream = fileItem.getInputStream(); // Reader reader = new InputStreamReader(inputStream); // BufferedReader bufferedReader = new BufferedReader(reader); String path = req.getServletContext().getRealPath("file/"+fileName); OutputStream outputStream = new FileOutputStream(path); // Writer writer = new OutputStreamWriter(outputStream); // BufferedWriter bufferedWriter = new BufferedWriter(writer); int temp = 0; while((temp = inputStream.read())!=-1){ outputStream.write(temp); } // bufferedWriter.close(); // writer.close(); outputStream.close(); // bufferedReader.close(); // reader.close(); inputStream.close(); System.out.println("上傳成功"); } } } catch (FileUploadException e) { e.printStackTrace(); } } }