Small white to: Upload files to the server using Request.getpart () in the Web

Source: Internet
Author: User

Being uploaded by the file to make a bad day, sure enough the path and various settings in the web really annoying =

The following is the super small white, and its simple "detailed" solution

1. Clear Purpose:

The user uploads the 1.txt file to the server (a folder under Web Engineering)

2. Clear Process

The file upload process involves two files, a. jsp file, and a servlet file.

. jsp file (upload.jsp): User Action page = Display file name of text box + File selection button (Browse ... ) + Confirm Upload button

servlet file (Uploadservlet.java): Process file uploads according to the file selected in the. jsp

3. Start tapping the code (lift the table!) ). jsp file
pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Key statements in the. JSP:

Key sentence 1:<form action= "upload.do" enctype= "Multipart/form-data " method= "POST" >//form data in binary data transfer, can be transmitted text Image..

Key sentence 2:<input type= "file" name= "FileName" size= "" "/>//type set to" file "is the document selection button, automatically generate a text box + Select button

servlet file:
ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.MultipartConfig;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJavax.servlet.http.Part; @WebServlet (name= "Uploadservlet", Urlpatterns = {"/upload.do" }) Public classUploadservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L;  PublicUploadservlet () {Super(); }    protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html;charset=utf-8"); String Path= This. Getservletcontext (). Getrealpath ("/"); //path is the project root directory: (Web project named Test)//D:\tomcat\apache-tomcat-8.0.50-windows-x64\apache-tomcat-8.0.50\webapps\test\ Part part= Request.getpart ("FileName");//Part represents a file
String H= Part.getheader ("Content-disposition"); //h is the header of the file to be uploaded: The following (upload the Account.txt file on the desktop)//form-data; name= "FileName"; filename= "C:\Users\ASUS\Desktop\account.txt"//Name the Account.txt file as "NewFile" saved in the serverString fname = "NewFile"; //substring is getting the suffix of the file, renaming but not changing the suffixfname = fname + h.substring (H.lastindexof ("."), H.length ()-1); //Upload files by path (modify path to change where files are stored in the server)Part.write (path + "\ \" +fname); //Prompt for upload successPrintWriter out =Response.getwriter (); Out.println ("); Out.println ("<title> Upload file </title>); Out.println ("<body>"); Out.println ("); Out.println ("</body>); }}

Summarize

I think the most critical statement is part, = Request.getpart ("FileName"); I understand the part interface as the file itself, part.write (path + "\ \" + fname); It is understood that the part follows the path in parentheses, where the path is the name of the file, the equivalent of a box, while part is the actual file content in the box.

The method of obtaining a file suffix is noteworthy.

Small white to: Upload files to the server using Request.getpart () in the Web

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.