Simple implementation of Javaweb not using frames to upload files

Source: Internet
Author: User
Tags html form save file

Seems to support a minimum of 3.0 javaweb projects;

Front page code:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "GBK">5 <title>Test upload</title>6 </Head>7 <Body>8     <formAction= "/fileup/uptest"enctype= "Multipart/form-data"9 Method= "POST">Ten  One         <inputtype= "File"name= "File01"> <inputtype= "Submit" A value= "Up"> -  -     </form> the </Body> - </HTML>

Background:

1 Importjava.io.IOException;2 3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.annotation.MultipartConfig;5 ImportJavax.servlet.http.HttpServlet;6 Importjavax.servlet.http.HttpServletRequest;7 ImportJavax.servlet.http.HttpServletResponse;8 ImportJavax.servlet.http.Part;9 Ten@MultipartConfig//must be set One  Public classUptestextendsHttpServlet { A  -     Private Static Final LongSerialversionuid = 1L; -  the     protected voidDoPost (httpservletrequest request, httpservletresponse response) -             throwsservletexception, IOException { -Part part = Request.getpart ("file01");//the Neme property value of the file in the HTML form.  -String Header = Part.getheader ("Content-disposition"); +         intLastIndexOf = Header.lastindexof ("=");//Get file name -Header =header.substring (LastIndexOf, Header.length ()); +Header = header.substring (2, Header.length ()-1); APart.write ("f:/" + header);//Save File at  -     } -  -}

The simplest implementation uploads a file without using a frame. Upload file size can be set in the parameters of the annotations;

Simple implementation of Javaweb not using frames to upload files

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.