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