Simple implementation of File upload principle

Source: Internet
Author: User
Uploading to implement file uploads, we must first understand the HTTP request to upload the file. The following simple application demonstrates how to upload a file and write the original data of an HTTP request to a file. View the file in a text editor to understand the format of the request, and on that basis we can extract the name of the uploaded file, the contents of the file, and other information that was originally mixed together.

Here we write a simple HTML page up.html that provides a form where users select files from here and upload files to the server.



<title> File Upload </title>


<body>

<form action= "upjsp.jsp" enctype= "Multipart/form-data" method=post>

<br/>

Company: <input type= "text" name= "Companies"/>

<br/>

Select the file to upload <input type= "file" name= "filename"/>

<br/>

<input type= "Submit" value= "Upload"/>

</form>

</body>


Note that the,<form> tag has a enctype attribute, and the property value is "Multipart/form-data". There is also a type file input box is used to select the upload file.

The form's Action property value is upjsp.jsp, which sends the request to the upjsp.jsp file. Upjsp.jsp called the JavaBean named Upbean.

The procedure is as follows:

<jsp:usebean id= "Thebean" scope= "page" class= "Upbean"/>

<%

Thebean.doupload (Request);

%>



Here is the implementation code for Upbean:

Import java.io.*;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import Javax.servlet.ServletInputStream;



public class Upbean {



public void Doupload (HttpServletRequest request) throws

IOException {

PrintWriter pw = new PrintWriter (

New BufferedWriter (New FileWriter ("test.txt"));

ServletInputStream in = Request.getinputstream ();



int i = In.read ();

while (I!=-1) {

Pw.print ((char) i);

i = In.read ();

}

Pw.close ();

}

}

This JavaBean writes the HttpServletRequest object's form raw data to the Test.txt file.

Choose to upload a text or a Web page file. After clicking on the "Upload" button, the form is sent to the upjsp.jsp file, together with the uploaded file. The upjsp.jsp file does not send any answers to the browser, but it generates a Test.txt file. Open the Test.txt file, we can see the contents of the uploaded file and some uploaded information.

Null


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.