Use apache-fileupload to process file upload and upload multiple files (2) (60 ),
1. Use apache-fileupload to process file uploads
Framework: it refers to code encapsulation of services that users often process. This allows you to conveniently call the API.
Currently, the file upload (framework) component:
Apache ---- fileupload-
Orialiy-cos-2008 ()-
Jsp-smart-upload-200 M.
Use fileupload to upload files:
To import a third-party package:
Apache-fileupload.jar-File Upload core package.
Apache-commons-io.jar-this package is the dependency package for fileupload. It is also a toolkit.
Core category:
DiskFileItemFactory-set disk space and save temporary files. It is just a class.
ServletFileUpload-core class for file upload. This class receives requests and parses reqeust.
ServletfileUpload. parseRequest (requdest)-List <FileItem>
Step 1: import the package
/*** Two parameters constructed by DiskFileItemFactory * First parameter: sizeThreadHold-sets the number of bytes of data stored in the cache (memory). The default value is 10 K * if a file is not greater than 10 K, you can directly use the memory to save it as a file. * If a file is larger than 10 KB, you need to save the file to the temporary directory first. * The second parameter File refers to the temporary directory location **/public class Up2Servlet extends HttpServlet {public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req. setCharacterEncoding ("UTf-8"); // get the project path String path = getServletContext (). getRealPath ("/up"); // The first step declares the diskfileitemfactory class, which is used to set a temporary directory DiskFileItemFactory disk = new DiskFileItemFactory (1024*10, new File ("d:/" ); // Step 2: declare ServletFileUpoload and receive the above temporary directory ServletFileUpload up = new ServletFileUpload (disk); // Step 3: parse request try {List <FileItem> list = up. parseRequest (req); // if the file is FileItem file = list. get (0); // get the file name, with the path String fileName = file. getName (); fileName = fileName. substring (fileName. lastIndexOf ("\") + 1); // obtain the object type String fileType = file. getContentType (); // get the object's bytecode InputStream in = file. getInputStr Eam (); // declare the output byte stream OutputStream out = new FileOutputStream (path + "/" + fileName); // file copy byte [] B = new byte [1024]; int len = 0; while (len = in. read (B ))! =-1) {out. write (B, 0, len);} out. close (); long size = file. getInputStream (). available (); // Delete the uploaded temporary file. delete (); // display data resp. setContentType ("text/html; charset = UTf-8"); PrintWriter op = resp. getWriter (); op. print ("File Uploaded <br/> file name:" + fileName); op. print ("<br/> file type:" + fileType); op. print ("<br/> file size (bytes)" + size);} catch (Exception e) {e. printStackTrace ();}}}
2. upload multiple files
Step 1: Modify the form on the page to multiple input types = "file"
<form action="<c:url value='/Up3Servlet'/>" method="post" enctype="multipart/form-data"> File1:<input type="file" name="txt"><br/> File2:<input type="file" name="txt"><br/> <input type="submit"/> </form>
Step 2: traverse list <fileitem>
Public class Up3Servlet extends HttpServlet {public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); String path = getServletContext (). getRealPath ("/up"); // declare disk DiskFileItemFactory disk = new DiskFileItemFactory (); disk. setSizeThreshold (1024*1024); disk. setRepository (new File ("d:/a"); // declare the servlet ServletFileUpload up = new ServletFileUpload (disk) for parsing requst ); try {// parse requst List <FileItem> list = up. parseRequest (request); // declare a list <map> encapsulate the data List of uploaded files <Map <String, String> ups = new ArrayList <Map <String, string >>(); for (FileItem file: list) {Map <String, String> mm = new HashMap <String, String> (); // obtain the file name String fileName = file. getName (); fileName = fileName. substring (fileName. lastIndexOf ("\") + 1); String fileType = file. getContentType (); InputStream in = file. getInputStream (); int size = in. available (); // use the FileUtils tool class. copyInputStreamToFile (in, new File (path + "/" + fileName); mm. put ("fileName", fileName); mm. put ("fileType", fileType); mm. put ("size", "" + size); ups. add (mm); file. delete ();} request. setAttribute ("ups", ups); // forward request. getRequestDispatcher ("/jsps/show. jsp "). forward (request, response);} catch (Exception e) {e. printStackTrace ();}}}