Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//get uploaded files to save the directory, the uploaded files in the Web-inf directory, do not allow direct access to the outside world, to ensure the security of uploading filesString Savepath = This. Getservletcontext (). Getrealpath ("/web-inf/upload"); File File=NewFile (savepath);//determine if the saved directory for the uploaded file existsif(!file.exists () &&!file.isdirectory ()) {System.out.println (Savepath+ "Directory does not exist, need to create");//Create a directoryFile.mkdir ();}//message PromptString message = "";Try{//Use the Apache File Upload component to process file upload steps://1. Create a diskfileitemfactory factoryDiskfileitemfactory factory =Newdiskfileitemfactory ();//2. Create a file Upload parserServletfileupload upload =Newservletfileupload (factory);//solve the Chinese characters of uploading file nameUpload.setheaderencoding ("UTF-8"); //3, determine whether the submitted data is the upload form dataif(!servletfileupload.ismultipartcontent (Request)) {//get data in the traditional wayreturn;}//4, using the Servletfileupload parser to parse the upload data, the results of the analysis returned is a list<fileitem> collection, each fileitem corresponding to a form form inputlist<fileitem> list =upload.parserequest (request); for(Fileitem item:list) {//if the data in the Fileitem is encapsulated in an ordinary entryif(Item.isformfield ()) {String name=item.getfieldname ();//solve the problem of Chinese garbled data of ordinary input itemsString value = item.getstring ("UTF-8");//value = new String (value.getbytes ("iso8859-1"), "UTF-8");SYSTEM.OUT.PRINTLN (name + "=" +value);}Else{//If the upload file is encapsulated in Fileitem//get the uploaded file name,String filename =Item.getname (); SYSTEM.OUT.PRINTLN (filename);if(filename==NULL|| Filename.trim (). Equals ("")){Continue;}//Note: Different browser submissions are not the same file name, some browsers submitted by the file name is with a path, such as: C:\a\b\1.txt, and some just simple file name, such as: 1.txt//handles the path portion of the file name of the uploaded files that are obtained, leaving only the file name sectionfilename = filename.substring (filename.lastindexof ("\ \") +1);//gets the input stream of the uploaded file in itemInputStream in =Item.getinputstream ();//Create a file output streamFileOutputStream out =NewFileOutputStream (Savepath + "\ \" +filename);//Create a bufferbyteBuffer[] =New byte[1024];//identifies whether the data in the input stream has been read outintLen = 0;//The loop reads the input stream into the buffer, and (len=in.read (buffer)) >0 indicates that there is data in it. while(Len=in.read (buffer)) >0){//writes the data of the buffer to the specified directory (savepath + "\ \" + filename) using the FileOutputStream output streamOut.write (buffer, 0, Len);}//close the input streamin.close ();//turn off the output streamout.close ();//Delete temporary files generated when processing file uploadsitem.delete (); Message= "File Upload succeeded!" ";}}}Catch(Exception e) {message= "File upload failed!" "; E.printstacktrace ();} Request.setattribute ("Message", message); Request.getrequestdispatcher ("/message.jsp"). Forward (request, response);} Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response);}
Implementing the test management system through Sping3 +hibernate4+strutsmvc file upload (servlet)