File upload and download
1, upload files
Ⅰ, there must be a tag form, method = post request.
Ⅱ, encType form tag attribute values must be multipart / form-data value.
Ⅲ, using the input type = file tag in the form to add upload files.
IV, write server code (Servlet program) received by the data upload process.
encType = multipart / form-data represents data submitted in the form of multi-stitching (each table item of a data segment), and then sent to the server as a binary stream.
New upload.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>upload</title> 5 </head> 6 <body> 7 <form action="http://localhost:8080/EL_JSTL/uploadServlet" method="post" enctype="multipart/form-data"> 8 用户名:<input type="text" name="username" id="username"><br /> 9 头像:<input type="file" name="photo" id="photo"><br /> 10 <input type="submit" value="上传"> 11 </form> 12 </body> 13 </html>
New UploadServlet program (Servlet and arranged in web.xml)
1 package com.gychen.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.ServletInputStream; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 public class UploadServlet extends HttpServlet { 11 12 /** 13 * 处理文件上传的数据 14 * @param req 15 * @param resp 16 * @throws ServletException 17 * @throws IOException 18 */ 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 22 System.out.println("文件已上传"); 23 ServletInputStream inputStream = req.getInputStream(); 24 byte[] buffer = new byte[1024000]; 25 int read = inputStream.read(buffer); 26 System.out.println(new String(buffer,0,read)); 27 } 28 }
2, the server parses upload files
Ⅰ, introducing two jar package
Ⅱ, two common classes jar package
ServletFileUpload class, for uploading data analysis.
FileItem class, each table item represents.
3, fileupload library use
1 package com.gychen.servlet; 2 3 import org.apache.commons.fileupload.FileItem; 4 import org.apache.commons.fileupload.FileItemFactory; 5 import org.apache.commons.fileupload.FileUploadException; 6 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 7 import org.apache.commons.fileupload.servlet.ServletFileUpload; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletInputStream; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import java.io.File; 15 import java.io.IOException; 16 import java.util.List; 17 18 public class UploadServlet extends HttpServlet { 19 20 /** 21 * 处理文件上传的数据 22 * @param req 23 * @param resp 24 * @throws ServletException 25 * @throws IOException 26 is * / 27 @Override 28 protected void the doPost (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException { 29 30 31 is // determines whether to upload data to multiple pieces of data (a plurality of pieces of data is only uploaded file) 32 IF (ServletFileUpload.isMultipartContent (REQ)) { 33 is // Create a class factory implementation FileItemFactory 34 is FileItemFactory FileItemFactory = new new DiskFileItemFactory (); 35 // create for parsing data upload tools ServletFileUpload 36 ServletFileUpload ServletFileUpload =new new ServletFileUpload (FileItemFactory); 37 [ // parse the uploaded data, to obtain a table for each item the FileItem 38 is the try { 39 List <the FileItem> List = servletFileUpload.parseRequest (REQ); 40 // cycle is determined for each common type or form item uploaded document 41 is for (the FileItem FileItem: List) { 42 is IF (fileItem.isFormField ()) { 43 is // common form item 44 is System.out.println ( "form item name attribute value:" + fileItem.getFieldName () ); 45 // parameters utf-8, to solve the problem of distortion 46 is System.out.println ( "individual table value value:" + fileItem.getString ( "utf- 8")); 47 } the else { 48 // uploaded file 49 System.out.println ( "file name attribute value:" + fileItem.getFieldName ()); 50 System.out.println ( "name of a file:" + fileItem.getName ()); 51 is // the file is written to the disk 52 is fileItem.write ( new new file ( "D: \\ \\ MyStudy" + 53 is "SpringBootStudy the EL \\ \\ & JavaWebStudy the JSTL \\" + 54 is "Web the Upload File \\ \\ \\ "+ fileItem.getName ())); 55 } 56 } 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 } 61 }