Java File Upload

Source: Internet
Author: User

Java upload files If you do not rely on the framework to use several jar files in Apache to handle 1, the form Set Enctype property, the value is "Multipart/form-data", in addition, the way must be submitted by post. 2, on the server side, can not directly use the Request.getparameter method to obtain data. Only InputStream Request.getinputstream () can be used, and data is obtained by analyzing the byte stream. It is usually analyzed by some tools. 3, to use several jar files in Apache processing to analyze the file stream is mainly used by several jar packages.
Commons-fileupload-1.2.2.jarcommons-io-2.0.1.jarcommons-lang-2.5.jar
  package download; import java.io.file;import Java.io.ioexception;import java.util.Iterator;import Java.util.list; import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse; import Org.apache.commons.fileupload.fileitem;import Org.apache.commons.fileupload.fileuploadexception;import Org.apache.commons.fileupload.disk.diskfileitemfactory;import Org.apache.commons.fileupload.servlet.servletfileupload; public class Fileunload extends HttpServlet{    @Override  protected void DoPost (HttpServletRequest request, httpservletresponse response)     Throws Servletexception, IOException {       string path = Request.getsession (). Getservletcontext () . Getrealpath ("FileDownload");   if (servletfileupload.ismultipartcontent (Request)) {    Diskfileitemfactory factory = new Diskfileitemfactory (); Disk file-based project creationOne Factory object    factory.setrepository (Factory.getrepository ());   servletfileupload unload = new Servletfileupload (Factory); Create a new File upload object    int fileSize = 10*1024*1024; Maximum upload file size <= 10m   list formlists = null;   fileitem formitem;     try {  &nbs P Formlists = unload.parserequest (request);//parse upload       } catch (Fileuploadexception e) {    } finally {   }     iterator iter = Formlists.iterator (); Enumeration methods      while (Iter.hasnext ()) {      FormItem = (Fileitem) iter.next ();    &NB Sp if (!formitem.isformfield ()) {       if (Formitem.getname ()!=null &&!formitem.getname (). Equals ("")) {        Long unloadsize = Formitem.getsize ();        if (unloadsize&gt ; fileSize) {        &NBSP;SYSTEM.OUT.PRINTLN ("File size exceeds 10m!");          return;       }       }      String unloadfilename = Formitem.getname ();       String newfilename = path+file.separator+unloadfilename;      File File = new file (new FileName);      Try {     formitem.write (file);   } catch (Exception e) {  & nbsp  e.printstacktrace ();   }          }         &NBS P;}    }    } }

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.