文件上传下载
⽂件上传下载
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(); } } }