Java File Upload

Source: Internet
Author: User

Package servlet;import java.io.file;import java.io.ioexception;import java.util.iterator;import java.util.List; Import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;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;import Org.apache.commons.fileupload.FileItem; Import Org.apache.commons.fileupload.disk.diskfileitemfactory;import Org.apache.commons.fileupload.servlet.servletfileupload;import Entit. News;import service. Newservice;import Service.impl.NewServiceImpl;/** * Servlet implementation class Newsservlet*/@WebServlet ("/uploadfileservlet") Public classNewsservlet extends HttpServlet {Private StaticFinalLongSerialversionuid =1L;  PublicNewsservlet () {super (); }    protected voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {    DoPost (request, response); }    protected voidDoPost (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {reques T.setcharacterencoding ("Utf-8"); String Uplaodpath=NULL; Response.setcharacterencoding ("Utf-8"); News News=NewNews (); Diskfileitemfactory Factory=Newdiskfileitemfactory (); Servletfileupload Upload=Newservletfileupload (Factory); Boolean flag=upload.ismultipartcontent (Request); if(flag) {//proof File Upload request            Try {                //gets all of the form elements including normal elements and file elementslist<fileitem> items =upload.parserequest (Request); //is list<fileitem> an element? It's a 4?? It's possible that n??? Traverse!!!! iterator<fileitem> its =Items.iterator ();  while(Its.hasnext ()) {//iterating through the collection//gets an element of oneFileitem item =Its.next (); if(Item.isformfield ()) {//normal fieldString fileName = Item.getfieldname ();//get the Name property value                        if(Filename.equals ("ID") {news.setnewsid (Integer.parseint (item.getstring ("Utf-8"))); } Else if(Filename.equals ("title") {news.setnewstitle (item.getstring ("Utf-8")); } Else if(Filename.equals ("author") {news.setnewssummary (item.getstring ("Utf-8")); }                    } Else{//file Elements//get the location of your serverUplaodpath = Request.getsession (). Getservletcontext (). Getrealpath ("uploadfiles/"); File File=NewFile (Uplaodpath); if(!file.exists ()) {//If you create a folder without miscellaneous, createFile.mkdirs (); }                        //get the name of the uploaded fileString FileName =Item.getname (); //users may not upload files                        if(!filename.equals ("") && FileName! =NULL) {File UploadFile=NewFile (FileName);//Get upload fileFile SaveFile =NewFile (Uplaodpath, Uploadfile.getname ());//Stitching File Location//the real uploadItem.write (SaveFile); News.setnewspic (Uploadfile.getname ()); //Assigning a value to an object}}} NewService service=NewNewserviceimpl (); intnum =service.addnew (News); if(Num >0) {System. out. println (Uplaodpath); } Else{System. out. println ("New Failure"); }            } Catch(Exception e) {e.printstacktrace (); }        } Else{System. out. println ("not a file upload request! "); }    }}

You need to import two jars when you use them.

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.