Java Program to Upload and Download a File

In the evolution of Web application system, file upload and download functions are very common functions. Today, let's talk about the implementation of file upload and download functions in Java Web.

For file upload, browsers submit files as streams to the server in the process of uploading. If it is more troublesome to get the input stream of uploaded files direct by using Servlet and and so parse the request parameters, it is by and large preferred to use apache'due south open source tool mutual-fileupload as the file upload component. The jar bundle of the mutual-fileupload upload component tin be downloaded on apache's official website or found under the lib folder of struts. The function of struts upload is based on this. Comm-fileupload relies on the common-io parcel, so it needs to be downloaded.

i. Structure of Development Environment

Create a FileUpload AndDownLoad project and bring together Apache'southward eatables-fileupload file upload component's Jar package (note to put it under WebRoot-WEB-INF-lib, not build path), as shown below:

2. Implementing file upload

2.1. File upload page and bulletin prompt folio

The code for upload.jsp page is equally follows:

<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html>   <head>     <title>File upload</title>   </head>      <body>     <form action="${pageContext.request.contextPath}/servlet/UploadHandleServlet" enctype="multipart/grade-data" method="post">         //Upload user: <input blazon="text" name="username"><br/>         //Upload file 1: <input type="file" name="file1"><br/>         //Upload file 2: <input type="file" name="file2"><br/>         <input blazon="submit" value="Submission">     </grade>   </trunk> </html>

The code for message.jsp is equally follows:

<%@ folio language="java" pageEncoding="UTF-viii"%> <!DOCTYPE HTML> <html>   <caput>     <championship>Message hint</title>   </caput>      <body>         ${bulletin}   </body> </html>

2.2. Servlet Processing File Upload

The code for the Upload Handle Servlet is equally follows:

bundle me.gacl.web.controller;  import coffee.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Listing; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.eatables.fileupload.FileItem; import org.apache.commons.fileupload.deejay.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;  public form UploadHandleServlet extends HttpServlet {      public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {                 //Get the saved directory of uploaded files, shop the uploaded files in WEB-INF directory, practice non allow exterior direct admission, ensure the security of uploaded files.                 String savePath = this.getServletContext().getRealPath("/Web-INF/upload");                 File file = new File(savePath);                 //Determine whether the saved directory of the uploaded file exists                 if (!file.exists() && !file.isDirectory()) {                     Organization.out.println(savePath+"The directory does not exist and needs to exist created");                     //Create directory                     file.mkdir();                 }                 //Bulletin hint                 String bulletin = "";                 try{                     //Processing file upload steps using Apache file upload component:                     //1. Create a DiskFile ItemFactory                     DiskFileItemFactory factory = new DiskFileItemFactory();                     //2. Create a file upload parser                     ServletFileUpload upload = new ServletFileUpload(mill);                      //Resolve Chinese Scrambling of Uploaded File Names                     upload.setHeaderEncoding("UTF-8");                      //iii. Determine whether the submitted data is uploaded form data?                     if(!ServletFileUpload.isMultipartContent(request)){                         //Obtaining data in a traditional way                         return;                     }                     //four. Using the ServletFileUpload parser to parse the uploaded data, the parsing event returns a List < FileItem > collection, each FileItem corresponds to an input detail of a Grade class.                     Listing<FileItem> list = upload.parseRequest(request);                     for(FileItem item : listing){                         //If the data encapsulated in fileitem is normal input detail                         if(item.isFormField()){                             Cord name = item.getFieldName();                             //Solving the Chinese scrambling problem of the information of mutual input items                             Cord value = item.getString("UTF-viii");                             //value = new String(value.getBytes("iso8859-ane"),"UTF-8");                             System.out.println(name + "=" + value);                         }else{//If the uploaded file is encapsulated in fileitem                             //Go the name of the uploaded file.                             Cord filename = item.getName();                             Organization.out.println(filename);                             if(filename==nil || filename.trim().equals("")){                                 go on;                             }                             //Notation: File names submitted by dissimilar browsers are different. Some browsers submit file names with paths, such equally: c: a b 1.txt, while others are simply file names, such as: 1.txt.                             //Processing the path role of the file name of the caused uploaded file, leaving simply the file proper noun office                             filename = filename.substring(filename.lastIndexOf("\\")+1);                             //Get the input stream of the uploaded file in particular                             InputStream in = item.getInputStream();                             //Create a file output stream                             FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);                             //Create a buffer                             byte buffer[] = new byte[1024];                             //Identification to determine whether the data in the input stream has been read out                             int len = 0;                             //The loop reads the input stream into the buffer, (len = in. read (buffer) > 0 means that there is data in the buffer.                             while((len=in.read(buffer))>0){                                 //Use the FileOutputStream output stream to write buffer data to the specified directory (savePath + "\" + filename)                                 out.write(buffer, 0, len);                             }                             //Close the input stream                             in.close();                             //Close the output stream                             out.close();                             //Delete temporary files generated when processing file uploads                             item.delete();                             message = "Document upload success!";                         }                     }                 }catch (Exception e) {                     bulletin= "File upload failed!";                     east.printStackTrace();                                      }                 request.setAttribute("message",bulletin);                 asking.getRequestDispatcher("/message.jsp").forward(asking, response);     }      public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {          doGet(request, response);     } }

Register Upload Handle Servlet in Web.xml file

<servlet>     <servlet-name>UploadHandleServlet</servlet-proper noun>     <servlet-class>me.gacl.web.controller.UploadHandleServlet</servlet-class> </servlet>  <servlet-mapping>     <servlet-name>UploadHandleServlet</servlet-proper name>     <url-blueprint>/servlet/UploadHandleServlet</url-pattern> </servlet-mapping>

The operation results are as follows:

Subsequently successful file upload, the upload file is saved in the upload directory of WEB-INF directory, equally shown in the following effigy:

two.3. Details of file upload

Although the above code can successfully upload files to the specified directory on the server, the file upload office has many pocket-sized details to pay attention to. The following points need special attention

1. In society to ensure the security of the server, uploaded files should exist placed in directories that tin non exist accessed straight past the outside world, such as WEB-INF directory.

2. To forestall file overwriting, a unique file proper name should be created for uploading files.

three. In order to prevent too many files from appearing under a directory, hash algorithm should exist used to scatter the storage.

iv. Limit the maximum value of uploaded files.

5. To limit the type of uploaded file, judge whether the suffix name is legal when the uploaded file name is received.

To address the five details mentioned above, permit'southward improve the Upload Handle Servlet. The improved code is every bit follows:

package me.gacl.web.controller;  import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.UUID;  import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.eatables.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.ProgressListener; import org.apache.commons.fileupload.deejay.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;  /** * @ClassName: UploadHandleServlet * @Description: TODO(Here is a sentence describing the office of this grade. * @author: Aloof and proud wolf * @appointment: 2015-one-three 11:35:50 p.m. * */  public form UploadHandleServlet extends HttpServlet {      public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {                 //Get the saved directory of uploaded files, store the uploaded files in Web-INF directory, practise not allow outside direct access, ensure the security of uploaded files.                 String savePath = this.getServletContext().getRealPath("/Spider web-INF/upload");                 //Temporary file saved directory generated when uploading                 String tempPath = this.getServletContext().getRealPath("/Spider web-INF/temp");                 File tmpFile = new File(tempPath);                 if (!tmpFile.exists()) {                     //Create temporary directories                     tmpFile.mkdir();                 }                                  //Message hint                 Cord message = "";                 try{                     //Processing file upload steps using Apache file upload component:                     //1. Create a DiskFile ItemFactory                     DiskFileItemFactory manufacturing plant = new DiskFileItemFactory();                     //Ready the size of the factory buffer. When the uploaded file size exceeds the size of the buffer, a temporary file will be generated and stored in the specified temporary directory.                     mill.setSizeThreshold(1024*100);//Fix the size of the buffer to 100KB. If not specified, the default size of the buffer is 10KB.                     //Set the save directory of temporary files generated when uploading                     factory.setRepository(tmpFile);                     //two. Create a file upload parser                     ServletFileUpload upload = new ServletFileUpload(factory);                     //Monitor file upload progress                     upload.setProgressListener(new ProgressListener(){                         public void update(long pBytesRead, long pContentLength, int arg2) {                             Organisation.out.println("The file size is:" + pContentLength + ",Currently processed:" + pBytesRead);                             /**                              * File size: 14608, currently processed: 4096                                 File size: 14608, currently processed: 7367                                 File size: 14608, currently processed: 11419                                 File size: 14608, currently processed: 14608                              */                         }                     });                      //Resolve Chinese Scrambling of Uploaded File Names                     upload.setHeaderEncoding("UTF-8");                      //3. Determine whether the submitted information is uploaded form data?                     if(!ServletFileUpload.isMultipartContent(request)){                         //Obtaining data in a traditional way                         return;                     }                                          //Set the maximum size for uploading a single file, currently set to 1024 * 1024 bytes, or 1MB                     upload.setFileSizeMax(1024*1024);                     //Set the maximum value of the full number of uploaded files, the maximum value = the sum of the maximum sizes of multiple files uploaded at the aforementioned fourth dimension, currently set to 10MB                     upload.setSizeMax(1024*1024*10);                     //iv. Using the ServletFileUpload parser to parse the uploaded data, the parsing outcome returns a List < FileItem > drove, each FileItem corresponds to an input particular of a Form form.                     List<FileItem> listing = upload.parseRequest(asking);                     for(FileItem item : list){                         //If the data encapsulated in fileitem is normal input detail                         if(particular.isFormField()){                             String name = detail.getFieldName();                             //Solving the Chinese scrambling trouble of the data of common input items                             String value = detail.getString("UTF-8");                             //value = new String(value.getBytes("iso8859-1"),"UTF-viii");                             System.out.println(name + "=" + value);                         }else{//If the uploaded file is encapsulated in fileitem                             //Get the name of the uploaded file.                             String filename = item.getName();                             Organisation.out.println(filename);                             if(filename==nothing || filename.trim().equals("")){                                 proceed;                             }                             //Note: File names submitted by dissimilar browsers are unlike. Some browsers submit file names with paths, such as: c: a b 1.txt, while others are simply file names, such as: 1.txt.                             //Processing the path part of the file name of the acquired uploaded file, leaving simply the file name role                             filename = filename.substring(filename.lastIndexOf("\\")+i);                             //Get the extension of the uploaded file                             String fileExtName = filename.substring(filename.lastIndexOf(".")+i);                             //If yous need to limit the type of file y'all upload, y'all can use the extension of the file to determine whether the uploaded file blazon is legitimate.                             System.out.println("The extension of the uploaded file is:"+fileExtName);                             //Get the input stream of the uploaded file in item                             InputStream in = item.getInputStream();                             //Get the proper noun of the file saved                             String saveFilename = makeFileName(filename);                             //Get the saved directory of the file                             Cord realSavePath = makePath(saveFilename, savePath);                             //Create a file output stream                             FileOutputStream out = new FileOutputStream(realSavePath + "\\" + saveFilename);                             //Create a buffer                             byte buffer[] = new byte[1024];                             //Identification to determine whether the data in the input stream has been read out                             int len = 0;                             //The loop reads the input stream into the buffer, (len = in. read (buffer) > 0 ways that there is information in the buffer.                             while((len=in.read(buffer))>0){                                 //Use the FileOutputStream output stream to write buffer information to the specified directory (savePath + "\" + filename)                                 out.write(buffer, 0, len);                             }                             //Close the input stream                             in.close();                             //Close the output stream                             out.close();                             //Delete temporary files generated when processing file uploads                             //item.delete();                             message = "Document upload success!";                         }                     }                 }catch (FileUploadBase.FileSizeLimitExceededException e) {                     east.printStackTrace();                     asking.setAttribute("message", "Single file exceeds maximum!!!");                     request.getRequestDispatcher("/bulletin.jsp").forward(request, response);                     return;                 }catch (FileUploadBase.SizeLimitExceededException e) {                     e.printStackTrace();                     request.setAttribute("message", "The total size of uploaded files exceeds the maximum limit!!!");                     request.getRequestDispatcher("/message.jsp").forward(asking, response);                     return;                 }grab (Exception e) {                     message= "File upload failed!";                     e.printStackTrace();                 }                 request.setAttribute("message",message);                 request.getRequestDispatcher("/message.jsp").forward(request, response);     }          /**     * @Method: makeFileName     * @Description: Generate the file proper name of the uploaded file with the original name of uuid+""+file     * @Anthor:Aloof and proud wolf     * @param filename The original name of the file     * @return uuid+"_"+The original name of the file     */      individual Cord makeFileName(String filename){  //2.jpg         //To preclude file overwriting, a unique file name is generated for uploading files.         return UUID.randomUUID().toString() + "_" + filename;     }          /**      * To prevent likewise many files from appearing under a directory, hash algorithm is used to scatter storage.     * @Method: makePath     * @Description:      * @Anthor:Aloof and proud wolf     *     * @param filename File name, to generate storage directory based on file proper name     * @param savePath File Storage Path     * @render New storage directory     */      private String makePath(String filename,String savePath){         //Get the hashCode value of the file name, and become the address of the string object filename in memory.         int hashcode = filename.hashCode();         int dir1 = hashcode&0xf;  //0--15         int dir2 = (hashcode&0xf0)>>4;  //0-15         //Construct a new saved directory         String dir = savePath + "\\" + dir1 + "\\" + dir2;  //upload\two\iii  upload\3\5         //File can stand for both files and directories         File file = new File(dir);         //If the directory does not be         if(!file.exists()){             //Create directory             file.mkdirs();         }         render dir;     }      public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {          doGet(request, response);     } }

III. File Download

iii.1. List file resources for download

We want to provide file resource in Web awarding arrangement for users to download. Kickoff of all, nosotros need a folio to listing all files in the uploaded file directory. When users click on the file download hyperlink, we download the files and write a ListFile Servlet to listing all downloaded files in Web application system.

The code for ListFileServlet is as follows:

package me.gacl.web.controller;  import java.io.File; import java.io.IOException; import java.util.HashMap; import coffee.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  /** * @ClassName: ListFileServlet * @Clarification: List all downloaded files in the Spider web system * @author: Aristocratic and proud wolf * @date: 2015-1-4 9:54:twoscore p.m. * */  public class ListFileServlet extends HttpServlet {      public void doGet(HttpServletRequest asking, HttpServletResponse response)             throws ServletException, IOException {         //Get the directory of the uploaded file         String uploadFilePath = this.getServletContext().getRealPath("/Web-INF/upload");         //Shop the file name to download         Map<Cord,String> fileNameMap = new HashMap<String,Cord>();         //Recursively traverse all files and directories in the filepath directory, and store the file name of the file in the map drove         listfile(new File(uploadFilePath),fileNameMap);//File tin can stand for either a file or a directory         //Send the Map drove to the listfile.jsp page for display         request.setAttribute("fileNameMap", fileNameMap);         request.getRequestDispatcher("/listfile.jsp").forrard(request, response);     }          /**     * @Method: listfile     * @Description: Recursively traverses all files in a specified directory     * @Anthor:Aristocratic and proud wolf     * @param file Represents both a file and a file directory.     * @param map Map Collection for Storing File Names     */      public void listfile(File file,Map<String,String> map){         //If the file represents not a file, but a directory         if(!file.isFile()){             //List all files and directories in this directory             File files[] = file.listFiles();             //Traverse files [] arrays             for(File f : files){                 //recursion                 listfile(f,map);             }         }else{             /**              * Processing file names. Uploaded files are renamed in the form of uuid_file names, and the uuid_part of file names is removed.                 file.getName().indexOf("_")Call up the location of the starting time occurrence of the "" character in the string if the filename is like to: 9349249849-88343-8344_A_Vanda.avi                 And then after file. getName (). substring (file. getName (). indexOf ("")+1) processing, y'all can get the Avanda. avi part.              */             String realName = file.getName().substring(file.getName().indexOf("_")+1);             //file.getName() gets the original name of the file, which is unique, and so it tin be used as a cardinal. realName is the proper name later on processing and may be repeated.             map.put(file.getName(), realName);         }     }          public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         doGet(asking, response);     } }

listfile method in ListFileServlet is used to list all files in the directory. Recursion is used in listfile method. In practice, we will create a table in the database, which will store the uploaded file proper name and the specific storage directory of the file. We can know the details of the file by querying the table. There is no need to apply recursive operation to shop directories. This example is considering the database is non used to store the uploaded file names and the specific storage location of files, and the storage location of uploaded files is scattered by hashing algorithm. Therefore, recursion is needed. When recursion occurs, the obtained file names are stored in the Map set passed from exterior to the file method. In this way, you lot can ensure that all files are stored in the same Map collection.

Configure ListFileServlet in Web.xml file

<servlet>      <servlet-name>ListFileServlet</servlet-name>      <servlet-class>me.gacl.web.controller.ListFileServlet</servlet-class> </servlet>   <servlet-mapping>      <servlet-proper noun>ListFileServlet</servlet-name>     <url-pattern>/servlet/ListFileServlet</url-pattern> </servlet-mapping>

The listfile.jsp page showing the download file is as follows:

<%@ folio linguistic communication="coffee" import="java.util.*" pageEncoding="UTF-viii"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML> <html>   <caput>     <title>Download File Display Folio</title>   </caput>      <body>       <!-- ergodic Map aggregate -->     <c:forEach var="me" items="${fileNameMap}">         <c:url value="/servlet/DownLoadServlet" var="downurl">             <c:param name="filename" value="${me.fundamental}"></c:param>         </c:url>         ${me.value}<a href="${downurl}">download</a>         <br/>     </c:forEach>   </trunk> </html>

Accessing the ListFileServlet, y'all can display the file resources provided for users to download in the listfile.jsp page, as shown in the post-obit figure:


3.2. Realizing File Download

Write a Servlet for processing file downloads. The code for DownLoad Servlet is every bit follows:

packet me.gacl.web.controller;  import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  public grade DownLoadServlet extends HttpServlet {           public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         //Get the file proper name to download         String fileName = request.getParameter("filename");  //23239283-92489-Avatar.avi         fileName = new String(fileName.getBytes("iso8859-ane"),"UTF-8");         //Uploaded files are stored in subdirectories in the / Web-INF/upload directory         Cord fileSaveRootPath=this.getServletContext().getRealPath("/Web-INF/upload");         //Find the directory of the file by its name         Cord path = findFileSavePathByFileName(fileName,fileSaveRootPath);         //Get the file to download         File file = new File(path + "\\" + fileName);         //If the file does non exist         if(!file.exists()){             request.setAttribute("message", "The resources yous want to download have been deleted!!");             asking.getRequestDispatcher("/bulletin.jsp").forrard(request, response);             return;         }         //Processing File Names         String realname = fileName.substring(fileName.indexOf("_")+i);         //Set the response header to control the browser to download the file         response.setHeader("content-disposition", "zipper;filename=" + URLEncoder.encode(realname, "UTF-viii"));         //Read the file to download and save it to the file input stream         FileInputStream in = new FileInputStream(path + "\\" + fileName);         //Create an output stream         OutputStream out = response.getOutputStream();         //Creating Buffers         byte buffer[] = new byte[1024];         int len = 0;         //The loop reads the contents of the input stream into the buffer         while((len=in.read(buffer))>0){             //Output Buffer Content to Browser to Realize File Download             out.write(buffer, 0, len);         }         //Shut File Input Stream         in.shut();         //Close the output stream         out.close();     }          /**     * @Method: findFileSavePathByFileName     * @Description: Find the path of the file to download through the file name and the root directory of the uploaded file     * @Anthor:Aloof and proud wolf     * @param filename File name to download     * @param saveRootPath The root directory where the upload file is saved, that is, / Web-INF/upload directory     * @render Storage directory of files to download     */      public String findFileSavePathByFileName(String filename,String saveRootPath){         int hashcode = filename.hashCode();         int dir1 = hashcode&0xf;  //0--15         int dir2 = (hashcode&0xf0)>>four;  //0-15         String dir = saveRootPath + "\\" + dir1 + "\\" + dir2;  //upload\2\3  upload\3\five         File file = new File(dir);         if(!file.exists()){             //Create directory             file.mkdirs();         }         render dir;     }          public void doPost(HttpServletRequest asking, HttpServletResponse response)             throws ServletException, IOException {         doGet(request, response);     } }

Configure DownLoadServlet in Web.xml file

<servlet>       <servlet-proper noun>DownLoadServlet</servlet-name>       <servlet-class>me.gacl.spider web.controller.DownLoadServlet</servlet-course> </servlet>   <servlet-mapping>       <servlet-name>DownLoadServlet</servlet-name>       <url-pattern>/servlet/DownLoadServlet</url-pattern> </servlet-mapping>

Click on the hyperlink and submit the request to DownLoad Servlet for processing. The file tin can be downloaded. The effect is as follows:

From the running results, we can see that our file download function tin download files normally.

There'southward so much about file upload and download in Java Web.

(This article is from: http://www.cnblogs.com/xdp-gacl/p/4200090.html)

strongflusuch.blogspot.com

Source: https://programmer.group/java-web-file-upload-and-download.html

0 Response to "Java Program to Upload and Download a File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel