Using JavaBean to upload files (4)

Source: Internet
Author: User

 

 

Method
The preceding four public methods are used to return the private fields of the FileUploadBean object, including getFilepath, getFilename, getContentType, and getFieldValue.
Public String getFilepath ()
Returns the value of the filepath private field.
Public String getFilename ()
Returns the value of the filename private field.
Public String getContentType ()
Returns the value of the contentType private field.
Public String getFieldValue (String fieldName)
Return the value of the specified input element in the HTML form. The element name is specified by the fieldName parameter.
Public void setSavePath (String savePath)
Use this method to specify the name of the directory on which the uploaded files are saved on the server.

Public void doUpload (HttpServletRequest request) throws IOException

DoUpload is the most important method in the FileUploadBean class. It has two tasks: first, it extracts the name and value of the input field from the HTML form and saves it to the Dictionary object. Second, the doUpload method extracts the uploaded file, save the file to the path specified by savePath, and assign the file name, path, and content type to the filename, filepath, and contentType fields respectively.

Private void setContentType (String s)

Called by the doUpload method. The setContentType method extracts the content type of the uploaded file from the original byte data.

Private void setFilename (String s)

Called by the doUpload method. The setFilename method extracts the file path and name from the original byte data.

The parameter of the doUpload method is the HttpServletRequest object created by the Servlet/JSP Container. The HttpServletRequest object describes the HTTP requests that must be processed by the program in order to extract the name-value pairs of HTML form elements and upload files. The doUpload method first obtains the ServletInputStream object through the getInputStream method of the HttpServletRequest object.

As mentioned above, each form element is separated by a separator and a group of carriage return linefeeds. Therefore, we can read the content of the HttpServletRequest object in one row. The following code defines a byte array named line:

Then, we useThe readLine method of the ServletInputStream object reads the first line of the content of the HttpServletRequest object:

Int I = in. readLine (line, 0,128 );

The first line should be a delimiter, and its length should be greater than 3 if there is no error. If the length is less than 3, we can think that an error has occurred. The doUpload method should return immediately:

If (I <3)
Return;

The length of the delimiters and delimiters is very important. You can see this point later in this article. The delimiter ends with a set of carriage return linefeeds. Therefore, the actual length of the Delimiter is 2 less than the number of bytes returned by the readLine method.

Int boundaryLength = I-2;

Discard the last two line breaks of the byte array line to get the line break:

String boundary = new String (line, 0, boundaryLength );

Then, the fields domain is turned into a Hashtable object. This Hashtable object will be used to save the name/value pairs of HTML form elements.

Fields = new Hashtable ();

As the delimiters exist, we can start to extract the values of the form elements. The specific method is to use a while loop to read the content of the HttpServletRequest object by row until the readLine method returns-1 when the content ends. All form elements start with delimiters followed by the "Content-Disposition" line. This line starts with the following characters:

Content-Disposition: form-data; name =

There are two types of form elements: files, non-files (common form elements, such as TEXT or den elements ). The difference between the two form elements is that the file element contains the string "filename =" filename "". Therefore, we can use this information to differentiate the form input elements of files and non-files. The Code is as follows:

If (newLine. startsWith ("Content-Disposition: form-data; name = "")){
If (newLine. indexOf ("filename = "")! =-1 ){
// File form input element
// Add the code for extracting the file.

...
}
Else {
// Common form input element
// Add the code to extract the form element.

...
}
}

Now, let's first look at the code for extracting the file content.

The file path is included after "Content-Disposition. To extract the file path and name, The doUpload method calls the setFilename private method. The setFilename method extracts the file path and file name information, and assigns them to the filepath and filename fields. After the setFilename method is called, the filename field should no longer be null. If the filename field is still null at this time, it indicates that a problem is encountered and the doUpload method returns directly.

If (filename = null)
Return;

The next row after the "Content-Disposition" row is a Content-Type row. Therefore, the doUpload method then calls the readLine method, and then calls the setContentType private method. The setContentType method is similar to the setFilename method. It extracts the content type of the uploaded file from the original byte data and saves it to the contentType field.

The next row of the Content-Type row is empty, so the program calls the readLine method again.

I = in. readLine (line, 0,128 );

Next we started the real file content. We should first prepare to write files to the disk through the PrintWriter object.

PrintWriter pw = new PrintWriter (new BufferedWriter (
New FileWriter (
(SavePath = null? "": SavePath) + filename
)));

The location where the uploaded file is saved depends on whether the savePath domain has been set. If the savePath field is not set and its value is null, the file will be saved to the default directory. If the savePath field has been set, its value is not null, the uploaded file is saved to the specified directory.

Then we can extract the file content. The specific method is to use the while LOOP, read a row of content in each loop and write it to the disk through the PrintWriter output method. But we know that the last line of the file contains two line breaks, so the byte data saved to the disk should not contain these two characters. Therefore, if the row to be read is not the last row of the file, we write all the bytes of data read to the disk. If the row to be read is already the last row of the file, the bytes written to the disk minus the last two characters.

However, we do not know the file size. We only know that the next line of the file content is a separator; or, if the file is the lastHTML form element. The next line is the delimiter plus two dashes. Therefore, we only need to check whether the content of the next row is a delimiter, so we know when the while loop should end. This is why the delimiters are very important. Here we must use delimiters.

Although we can read the content of the next line and then use the startsWith method to check whether it is a delimiter, the overhead of string operations is very large. To reduce string operations, we compare the length of the byte array read by readLine. The latter should be equal to boundaryLength + 2; or, if it is the last line in the HttpServletRequest object, it should be equal to boundaryLength + 4 because it contains the last two dashes. Because a line of content can be as long as the delimiters even if it is not the delimiters, We will compare it with the delimiters after the length matches. This is why boundaryLength is very important.

The implementation code of the entire processing process is as follows:

While (I! =-1 &&! NewLine. startsWith (boundary )){
I = in. readLine (line, 0,128 );
If (I = boundaryLength + 2 | I = boundaryLength + 4)
& (New String (line, 0, I). startsWith (boundary )))
Pw. print (newLine. substring (0, newLine. length ()-2 ));
Else
Pw. print (newLine );
NewLine = new String (line, 0, I );
}

After saving the file content to the disk, we disable PrintWriter.

Pw. close ();

Non-file form elements can also be extracted using a similar method. The difference is that we do not write data to the disk at this time, but save the name-value pair to the Dictionary object.

Fields. put (fieldName, fieldValue. toString ());

Byte [] line = new byte [128];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.