Java File Upload

Source: Internet
Author: User

JSP Interface Code:

<body>
  <form action= "Servlet/uploadservlet" enctype= " Multipart/form-data " method=" post "
     <table>
       <tr>
       <td> file upload: </td>
        <td>
        <input type= "file" name= "File1" >
       </td>
      </tr>
       <tr>
       <td>
         <input type= "Submit" value= "Submission";
       </td
      </tr>
     </table>
     </form>
  </body>

Background code:

public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

Response.setcontenttype ("text/html");

File file1 = null;

Diskfileupload diskfileupload = new Diskfileupload ();

try {

list<fileitem> list = diskfileupload.parserequest (request);

for (Fileitem fileitem:list) {

if (Fileitem.isformfield ()) {

}

else{

File RemoteFile = new file (new String (Fileitem.getname (). GetBytes (), "UTF-8")); Server Project Deployment Path _e:\tomcat\tomcat\webapps\fileuploaddemo\attachment

File1 = new File (This.getservletcontext (). Getrealpath ("attachment"), Remotefile.getname ());

File1.getparentfile (). Mkdirs ();

File1.createnewfile ();

InputStream INP = Fileitem.getinputstream ();

OutputStream OUP = new FileOutputStream (file1);

byte[] buffer = new byte[1024];

int len = 0;

while (len = inp.read (buffer)) >-1) {

Oup.write (buffer); }

Oup.close ();

Inp.close ();

}

}

} catch (Fileuploadexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

Java 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.