Using JavaBean for File Upload (I) Request Analysis

Source: Internet
Author: User

 


Request Analysis
To upload a file, we must first understand the HTTP request for uploading the file. The following simple application demonstrates how to upload files and write original HTTP request data to files. You can view the file in a text editor to understand the request format. On this basis, you can extract the name, content, and other information of the uploaded file.
This simple application is the preparation for developing a real file upload JavaBean. It consists of three files: HTML file main.html, JSP page Jsp1.jsp, and simplean file SimpleBean. java.
Main.html provides a form where you can select a file and upload the file to the server. The main.html code is as follows:
<Html>
<Head>
<Title> File Upload </title>
</Head>
<Body>
<Form action = "jsp1.jsp" enctype = "MULTIPART/FORM-DATA" method = post>
Author: <input type = "text" name = "author"/>
<Br/>
Company: <input type = "text" name = "company"/>
<Br/>
Select the file to be uploaded <input type = "file" name = "filename"/>
<Br/>
<Input type = "submit" value = "Upload"/>
</Form>
</Body>
</Html> you can see that the <form> tag has an enctype attribute. The attribute value is "MULTIPART/FORM-DATA ". There are four input elements in the form, including the submit button. The first two input elements are common text elements, namely, author and company. The type attribute of the third input element is file, which is the element used to select the file.

FormThe property value of action is Jsp1.jsp, which means that the request (including the uploaded file) will be sent to the Jsp1.jsp file. Jsp1.jsp simply calls the simplean named SimpleBean.

<Jsp: useBean id = "TheBean" scope = "page" class = "SimpleBean"/>
<%
TheBean. doUpload (request );
%>

The following is the SimpleBean implementation code: import java. io .*;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. ServletInputStream;

Public class FileUploadBean {

Public void doUpload (HttpServletRequest request) throws
IOException {
PrintWriter pw = new PrintWriter (
New BufferedWriter (new FileWriter ("Demo. out ")));
ServletInputStream in = request. getInputStream ();

Int I = in. read ();
While (I! =-1 ){
Pw. print (char) I );
I = in. read ();
}
Pw. close ();
}
}

ThisJavaBean writes the original form data of the HttpServletRequest object to the Demo. out file. The application user interface is provided by the main.html file, as shown in.



The file we select is abisco.html. The HTML file is uploaded to make it easy to observe the uploaded format. Because the HTML file is essentially a text file, we can easily browse its content. The content of the abisco.html file is as follows:

<Html>
<Head>
<Title> Abisco </title>
</Head>
</Html>

After clicking upload and clicking upload, the table is sent to the jsp1.jspfile, And the abisco.html file is sent together. The Jsp1.jsp file does not send any response content to the browser, But it generates a Demo. out file.

OpenDemo. out file, we can see the following content:


----------------------------- 7d15340138
Content-Disposition: form-data; name = "Author"

A. Christie
----------------------------- 7d15340138
Content-Disposition: form-data; name = "Company"

Abisco
----------------------------- 7d15340138
Content-Disposition: form-data; name = "Filename"; filename = "C: 123dataabisco.html"
Content-Type: text/html

<Html>
<Head>
<Title> Abisco </title>
</Head>
</Html>

----------------------------- 7d15340138 --


As you can see, the HTTP Request body contains all form inputs, including uploaded files. The separation of these input data is implemented by a separator. A delimiter consists of a series of "-" characters and a random number. In the preceding example, the separator is "--------------------------- 7d15340138 ". The last separator ends the Request body. There are two "-" symbols after the separator.

For non-file-type input data, the Delimiter is followed by the following line:Content-Disposition: form-data; name = inputName. InputName is the name of the form element. For example, Content-Disposition: form-data; name = "Author ". After this line of content, follow two consecutive line breaks and form element values.

For file-type input fields, the separator is followed by two lines. The first line contains the name of the input element and the complete path of the uploaded file on the client. In the preceding example, the Content in this line is "Content-Disposition: form-data; name =" Filename "; filename = "C: 123dataabisco.html "". In this line, the file input element is named filename and the file path is "C: 123dataabisco.html ". Note that the Windows browser sets the file path, while the Unix/Linux and Mac browsers only send the file name.

The second line contains the file content type, so its specific content is related to the uploaded file. In this example, the Content in the second line is "Content-Type: text/html ".

Like non-file input elements, the file content starts after two consecutive line breaks.

Related Article

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.