Section 14 of the second semester [file upload and download]

catalogue

  1. How to upload files
  2. API related to file upload
  3. Realize file upload
  4. Realize file download

1, How to upload files

To realize the file upload function in Web development, it usually needs to complete two steps: one is to add the upload input item in the Web page; the other is to read the data of the uploaded file in the Servlet and save it to the local hard disk. Next, this section will explain these two steps in detail.

Due to the of most documents Uploads are submitted to the server in the form of forms. Therefore, to realize the function of file upload in the program, first create a form page for submitting uploaded files. In the page, you need to use the < inputtype = file "> tag to add a file upload input item to the Web page.

The following two points should be paid attention to in the use of labels

The name attribute of the input entry must be set, otherwise the browser will not send the data of the uploaded file.

The method attribute of the form page must be set to post mode, and the enctype attribute must be set to "multipar / form data" type.

The sample code is as follows.

<%-- Specifies the of the form data enctypel!Properties and submission method--%>
<form enctype- "multipart/torm-datamethod="post">
<%--Specifies the type of tag and the name of the file field--%>
Select upload file: <input type-"file" name-"myfile"/><br />

When the browser submits and uploads a file through a form, because the file data is attached to the HTTP request message body and described by MIME type (Multipurpose Internet mail extension type), the data submitted by the client can be read in the background by using the getInputStream() method provided by the request object. However, because users may upload multiple files at the same time, it is very troublesome to directly read the uploaded data at the Serlet end and analyze the corresponding file data respectively. In order to facilitate the processing of user uploaded data, the Apache organization provides an open source component Commons- FileUpload. This component can easily parse various form fields in "ultipart / form data" type requests, upload one or more files, and limit the size of uploaded files. Its performance is very excellent and its use is extremely simple.

It should be noted that when using the FileUpload component, you need to import two JAR packages: Commons FileUpload JAR and Commons IO JAR. These two JAR packages can go to the Apache official website htp://ommo … apache.org / "download" (after entering the website page, find FileUpload and 10 in the Components column of the table under Apache Commons Proper, and click enter to find the download link).

The FileUpload component realizes the file upload function through Servlet. The workflow is shown in the figure below. As can be seen from the figure, several strange classes are involved in uploading files. These classes are the core classes for Apache components to upload files.

2, API related to file upload

2.1 Fileltem interface

The Fileltem interface is implemented in the Commons fileUpload component. It is mainly used to encapsulate the data of a single form field element. A form field element corresponds to a filetem object. In the process of file upload, the common fileUpload component encapsulates each form field (including ordinary text form field and file field) in a Fileltem object. For ease of explanation, the implementation class of Filete interface is called Filtem class. Filetem class implements the serialable interface. Therefore, serialization operation is supported. Many methods for obtaining form field elements are defined in Fileltem class, as follows. (1) boolean isFormField() method

The isFormField() method is used to determine whether the data encapsulated by the Filetem class object is a normal text form field or a file form field. If it is a normal text form field, it returns true, otherwise it returns false.

(1) String getName() method
The geltName() method is used to obtain the file name in the file upload field. If the fieldlem class object corresponds to an ordinary text form field, the gelName() method will return nll; Otherwise, as long as the reader passes the field information of the file to the server, the getName() method will return a string type result, such as: "C:ASunsetjpg"

It should be noted that the complete paths and names obtained from files uploaded through different browsers are different. For example, when users upload files using E browser, they get the complete path "C:Sunsetipg"; If you use other browsers, such as Firefox, you only get the file name without a path, such as "sunsetig".

(2) String getFieldName() method
The glFeldNamel0 method is used to obtain the value of the name attribute of the description header of the form field element, which is also the value of the name attribute of the form label. For example, "file1" in "name=file1".

(3) Void write (file) method
The wrtel0 method is used to save the body content saved in the Flyn object to a specified file. If the main content of the Flem object is saved in a temporary file, the temporary file may be cleared after the method is successfully completed. In addition, this method can also write the contents of ordinary form fields to a file, but it is mainly used to save the uploaded file contents to the local file system.

(4) String getString() method
The getString() method is used to return the data stream content saved in the fly object as a string. It has two overloaded definition forms.
(1)public String getString()
(2)public String getString(java.lang.String encoding)

In the above overloaded two methods, the former uses the default character set encoding to convert the body content into a string, and the latter uses the character set encoding specified by the parameter to convert the body content into a string. It should be noted that if Chinese garbled code occurs when reading the contents of ordinary form field elements, please call the second getting () method and pass it the correct character set encoding name.

(5) String getContentType() method
The getContentType() method is used to obtain the type of uploaded file, that is, the value of the description header attribute "content type" of the form field element, such as "image/jpeg". If the fieldem class object corresponds to an ordinary form field, the method will return null

(6) boolean isInMemory() method
The isInMemory() method is used to determine whether the data content encapsulated by the Fileltem object is stored in memory or in a temporary file. If it is stored in memory, it returns true; otherwise, it returns false.

(7) void delete() method
The delete() method is used to empty the main content stored in the Filtem class object. If the main content is saved in a temporary file, the delete() method will delete the temporary file. It should be noted that although Fileltem objects are collected by the garbage collector

The temporary files are automatically cleared, but the delete () method should be called in time to clear the temporary files, so as to release the system storage resources to prevent

An exception occurred in the system, causing the temporary file to be permanently saved in the hard disk.

(8) InputStream getlnputStream() method
The getlnputStream() method returns the data content of the uploaded file in the form of a stream.

(9) long getSize() method
The gelSize() method returns the size of the uploaded file (in bytes) o

2.2 DiskFileltemFactory class
The DiskleltenFactory class is used to encapsulate each file in the request message entity into a separate Filele object. If the uploaded file is small, it will be saved directly in memory. If the uploaded file is large, it will be saved in the temporary folder of the disk in the form of temporary file. By default, the critical value for saving files in memory or hard disk temporary folder is 10240, that is, 10KB. The DiskFileItemFactory class contains two constructor methods. Next, we will explain the common methods of the DielemFactary class in detail, as shown below.

(1Filtem ceteltem(Sring fieldName, String contentType,boolean isFormField, StringfileName) method

This method is used to create the request message entity as an instance object of type Filtem. It should be noted that this method is automatically called by the FileUpload component when parsing the request, which does not need our management.

(2) setSizeThreshold(int sizeThreshold) and getSizeThreshold() methods

The seiSizThreshold() method is used to set the threshold value of whether to save the uploaded file on disk as a temporary file. When the Apache file upload component parses the uploaded data, it needs to save the parsed data temporarily for further data processing. Since the memory space available to the Java virtual machine is limited, the file storage location needs to be determined according to the size of the uploaded file. For example, an 800MB file cannot be temporarily saved in memory. At this time, the Apache file upload component can save these data in the form of temporary files. However, if the uploaded file is very small, only 600KB, obviously saving it in memory is a better choice. In addition, the corresponding getSizeThreshold() method is used to obtain this critical value.

(3) setrepository (file repository) and getRepository() methods

If the size of the uploaded file is larger than the critical value set by the setSizeThreshold() method, you can use the setRepository() method to save the uploaded file in the specified directory in the form of temporary file. By default, the system default temporary file path is used, which can be obtained in the following ways.

System. getProperty("java. io. tmpdir")

In addition, the corresponding getRepository() method is used to obtain temporary files.

2.3 ServletFileUpload class
The SerlelFileUpload class is the core advanced class for Apache components to handle file upload. By using the pseaussertetreqes) method, the data submitted by each form in HTIML can be encapsulated into a Filelem object, and then returned in the form of List. The Sr/lFieUplaldo class contains two constructor methods.
The common methods of the SrlelFleUpload class are as follows.

(1) Selsizemaxlong sizemax() and geizeMax() methods

The selSzeMax method inherits from the FlUplaBase class and is used to set the maximum size limit of the entity content of the request message to prevent the client from maliciously uploading large files to waste the storage space of the server. The parameter sizeMax is in bytes. In addition, the corresponding getsizeMax method is used to read the maximum value allowed by the entity content of the request message.

(2) Seleszeaxlongg (flsiemax) and gefieiemax (method

The setFileSizeMax() method inherits from the FileUploadBase class and is used to set the maximum size limit of a single uploaded file to prevent the client from uploading large files maliciously and wasting the storage space on the server. The parameter flieMlx is in bytes. In addition, the corresponding getFileSizeMax() method is used to obtain the maximum value allowed for a single uploaded file.

(3) parseRequestjavax. selett. HtSereRequet req)

Parserequest () method is an important method of the SerletFileUpload class. It is the entry to parse the content of the HTTP request message body. It parses the data of each field in the Form, wraps them into independent Filetem objects, and then adds these Filtem objects into a List type collection object to return.

( 4) getltemlterator(HtpServletRequest request)

The functions of the getltemllter () method and the parseRequest() method are basically the same, but the gettemlter () method returns
ert is an iterator that stores not Fileltem objects but FiltemStream objects. If readers want to further improve the performance, they can directly obtain the data input stream of each file item by using the gettemlter () method, which is the underlying processing; If the performance is not a problem and you want the code to be simple, you can use the parseRequest() method.

( 5 ) isMultipartContentHtpServletRequest req)

isMultipartContent() method is used to judge whether the content in the request message is of "multipart / form data" type. If so, it returns true, otherwise it returns false. It should be noted that isMultipartContent() method is a static method and can be called without creating an instance object of ServletFileUpload class.

(6) getFileltemFactory() and setfileltemfactory (fileltemfactory)

These two methods inherit from the FileUpload class and are used to read and set the flteFactory property respectively.

(7) setheaderencoding (string encoding) method and getHeaderEncoding() method

These two methods inherit from the FileUploadBase class and are used to set and read character encoding. It should be noted that if seheaderending () is not used to set the character encoding, the geheaderending () method returns nul, and the upload component 1 will adopt the character encoding set by HtSerlelReqes. However, if the character code of HtSrletReque is also null, the upload component will adopt the system default character code. The method of obtaining the system default character encoding is as follows. System.getProperty(" file .encoding"));

[task 12-1] upload files

[implementation steps]

1. Create the project and import the JAR package

Create a Web project named chapter12 in Eclipse, and import the JAR package commos-fileupload-1.3.1. In the WEB-INF/lib directory of the project JAR and comons-io-2.4 JAR and publish it to the close path. The project structure of the imported JAR package is shown in the figure below:

2. Create upload page
Create a jsp page named form under the WebContent directory of the Web project in chapter12, which is used to provide the form form for file upload.

```cpp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File upload</title>
</head>
<body>
 	<form action="UploadServlet" method="post" 
      enctype="multipart/form-data">
 		<table width="600px">
 			<tr>
 				<td>Uploader</td>
 				<td><input type="text" name="name" /></td>
 			</tr>
 			<tr>
 				<td>Upload file</td>
 				<td><input type="file" name="myfile" /></td>
 			</tr>
 			<tr>
 				<td colspan="2"><input type="submit" value="upload" /></td>
 			</tr>
 		</table>
 	</form>
</body>
</html>
  1. Create Servlet
    Create a file named CN. In the src directory of the project itcast. FileUpload package, in which a class named UploadServlet is written. This class is mainly used to obtain the information of forms and their uploaded files. The details are as follows:
package cn.itcast.fileupload;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet("/UploadServlet")
//Servlet class for uploading files
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
		    //Set ContentType field value
		    response.setContentType("text/html;charset=utf-8");
			// Create DiskFileItemFactory factory object
			DiskFileItemFactory factory = new DiskFileItemFactory();
			//Set the file cache directory. If the directory does not exist, create a new one
			File f = new File("d:\\TempFolder");
			if (!f.exists()) {
				f.mkdirs();
			}
			// Set the cache path of the file
			factory.setRepository(f);
			// Create a ServletFileUpload object
			ServletFileUpload fileupload = new ServletFileUpload(factory);
			//Set character encoding
			fileupload.setHeaderEncoding("utf-8");
			// Parse the request to get the FileItem object of the uploaded file
			List<FileItem> fileitems = fileupload.parseRequest(request);
			//Get character stream
			PrintWriter writer = response.getWriter();
			// Traversal set
			for (FileItem fileitem : fileitems) {
				// Determine whether it is a normal field
				if (fileitem.isFormField()) {
					// Get field name and field value
					String name = fileitem.getFieldName();
					if(name.equals("name")){
						//If the file is not empty, save it in value
						if(!fileitem.getString().equals("")){
							String value = fileitem.getString("utf-8");
							writer.print("Uploaded by:" + value + "<br>");							
						}
					}
				} else {
					// Get the uploaded file name
					String filename = fileitem.getName();
					//Processing uploaded files
					if(filename != null && !filename.equals("")){
						writer.print("The name of the uploaded file is:" + filename + "<br>");						
						// Intercept the file name
						filename = filename.substring(filename.lastIndexOf("\\") + 1);						
						// File name needs to be unique
						filename = UUID.randomUUID().toString() + "_" + filename;
						// Create a file with the same name on the server
						String webPath = "/upload/";
						//Combine the folder path and file name in the server to form a complete server-side path
						String filepath = getServletContext().getRealPath(webPath + filename);
						// create a file
						File file = new File(filepath);
						file.getParentFile().mkdirs();
						file.createNewFile();
						// Get uploaded file stream
						InputStream in = fileitem.getInputStream();
						// Use FileOutputStream to open the uploaded file on the server side
						FileOutputStream out = new FileOutputStream(file);
						// Stream torture
						byte[] buffer = new byte[1024];//Read 1 byte at a time
						int len;
						//Start reading the bytes of the uploaded file and output them to the uploaded file output stream of the server
						while ((len = in.read(buffer)) > 0)
							out.write(buffer, 0, len);
						// Close flow
						in.close();
						out.close();
						// Delete temporary file
						fileitem.delete();
						writer.print("Upload file succeeded!<br>");
					}										
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
  1. Start the project and view the running results
    Publish the Chapter12 project to the Tomcat server, start the server, and access the address through the browser“ http://localhost:9999/chapter12/form.jsp ”, the browser displays the following results:

3, File download

For file download, I believe readers will not be unfamiliar, because usually the pictures, documents and videos under the Internet are the scope of file download. Now many websites provide the function of downloading various resources. Therefore, in the process of learning Web development, it is necessary to learn the implementation of file download.

The file download function is relatively simple. Generally, it does not need to be implemented by a third-party component, but directly using the Serlet class and input / output stream. Different from accessing server files, to download files, you not only need to specify the path of the file, but also need to set two response headers in the HTTP protocol, as follows.

//Sets how the receiver processes data
Content-Disposition: attachment; filename
//Set MIME type of entity content
Content-Type: application/x-msdownload

The browser usually directly processes the entity content of the response. Two response header fields need to be set in the HTTP response message to specify that the receiving program processes the data content as the download method. When clicking [download J hyperlink], the system submits the request to the corresponding server. In this servlet, first obtain the address of the downloaded file, and create a file byte input stream according to the address, then read the downloaded file content through the stream, and finally write the read content to the target file through the output stream.

[task 12-2] realize file download

[implementation steps]

1. Create a download page
Create a download page in the WebContent directory of the chapter12 project JSP, which writes a link for download.

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.net.URLEncoder"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File download</title>
</head>
<body>
	    <!-- <a href="/chapter12/DownloadServlet?filename=<%=URLEncoder.encode("cat.jpg", "utf-8")%>">File download </a>-->
	 
	  <a href="/chapter12/DownloadServlet?filename=cat.jpg">file</a> 
	  
	 <!--  <a href="download/cat.jpg">File download</a> -->
</body>
</html>

2. Create a Servlet
On CN itcast. A DownloadServlet class is created in the fileUpload package. This class is mainly used to set the download to download and the opening method of files in the browser.

package cn.itcast.fileupload;
import java.io.*;

import java.net.URLEncoder;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse 
		response) throws ServletException, IOException {
		//Set ContentType field value
		response.setContentType("text/html;charset=utf-8");
		//Set corresponding message code
		response.setCharacterEncoding("utf-8");
		//Set request message encoding
		request.setCharacterEncoding("utf-8"); 				
		//Get the name of the file to download
		String filename = request.getParameter("filename");		
		//Encode file name
		filename = new String(filename.trim().getBytes("iso8859-1"),"UTF-8");  
        //Directory where files are downloaded
        String folder = "/download/";
        // Notify the browser to open as a download
		response.addHeader("Content-Type", "application/octet-stream");
		response.addHeader("Content-Disposition", 
        		"attachment;filename="+URLEncoder.encode(filename,"utf-8"));
		// Read files through file stream
		InputStream in = getServletContext().getResourceAsStream(
				folder+filename);
		// Gets the output stream of the response object
		OutputStream out = response.getOutputStream();
		byte[] buffer = new byte[1024];
		int len;
		//Loop out the data in the stream
		while ((len = in.read(buffer)) != -1) {
			out.write(buffer, 0, len);
		}

	}
  	public void doPost(HttpServletRequest request, HttpServletResponse 
  		response) throws ServletException, IOException {
		doGet(request, response);
	}
}

3. Create download directory and files
Create a folder named download in the WebContent directory of the project and place a jpg file in the file.

4. Start the project and view the results
Publish the Chapter12 project to the Tomcat server, start the server, and access the address through the browser“ http://localhost:9999/chapter12/download.jsp ”, the browser displays the following results:

[task 12-3] solve the problem of garbled code in downloading Chinese files

[implementation steps]

1. Modify Servlet class

2. Modify the download page

3. Run the project and view the results

Student No.: 2020080605014

Keywords: Java Apache server

Added by ycoleman on Sat, 08 Jan 2022 12:01:36 +0200