File Upload (form file upload)

Source: Internet
Author: User
Tags cos

File upload is one of the most basic requirements for developing a website.

Settings for the front page:

Import= "java.util.*" pageencoding= "UTF-8"%><%== request.getscheme () + "://" + Request.getservername () + ":" +request.getserverport () +path+ "/"; %><form action= "Upload.do?method=upload" method= "post" enctype= "Multipart/form-data" >     Photo: <input type= "file" name= "image"/><br>     Name:<input type= "text" name= "name"/><br>    <input type= "Submit"/></form>

Points:

1, the way to submit must be the post mode, because the Get method is through the URL address bar for data transfer, so can not choose the Get methods

2, in the form tag needs to add Enctype= "Multipart/form-data" property, if not add the information needed to upload the content can not be passed to the background, will only pass the file name of the past. Using this property, you must use the tool in the background to separate the data

Background settings:

Create a Com.servlet package under the SRC directory, and then add a Uploadservlet.java class and register the class in Web. xml

Copy Cos.jar-->https://github.com/darknessshadow/datamaven Bag in the Lib directory of the Web project (Cos.jar the main function is to separate the foreground passes the past data, and then writes the detached file to the specified path)

 PackageCom.servlet;Importjava.io.IOException;ImportJava.util.Map;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importcom.oreilly.servlet.MultipartRequest;ImportCom.util.Upload; Public classUploadservletextendshttpservlet{@Overrideprotected voidService (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); String Method= Request.getparameter ("method"));        System.out.println (method); if("Upload". Equals (method))        {doupload (request,response); }        }    Private voidDoupload (HttpServletRequest request, httpservletresponse response)throwsIOException {//in the way that the original servlet was received, the data was not received and the data must be separated using a tool//String image = Request.getparameter ("image");//System.out.println (image);//String name = Request.getparameter ("name");//System.out.println (name); //the same Request object gets the true path of the stored pictureString path = Request.getservletcontext (). Getrealpath ("/images"); SYSTEM.OUT.PRINTLN (path);//gets the object that stores the data and sets its corresponding encoding, pathMultipartrequest Mr =NewMultipartrequest (Request, Path, "Utf-8"));//get the original name of the file sent by the foreground.String image = Mr.getoriginalfilename ("image"); SYSTEM.OUT.PRINTLN (image);//get the normal data sent by the foreground form .String name = Mr.getparameter ("name");            SYSTEM.OUT.PRINTLN (name); }}    

BUG: Renaming problem, Chinese display problem

File Upload (form file upload)

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.