Just published a Java send e-mail, before really did not notice, Commons so many common items, ashamed, until now review, to learn really too much, or lack of real learning ability ...
Commons-fileupload.jar and commons-io.jar;http://commons.apache.org/are used here.
This is a Web project, so the first to create a new JSP under the Webroot, in fact, directly with the index.jsp on the line; The following is the front-end code:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @taglib prefix= "C" uri= "/HTTP/ Java.sun.com/jsp/jstl/core "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Here the form requests a servlet, creating a servlet in Src, named Fileuploadservlet.java
The code is implemented as follows:
Package Com.wf.web;import Java.io.file;import Java.io.ioexception;import java.text.simpledateformat;import Java.util.date;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.disk.diskfileitemfactory;import Org.apache.commons.fileupload.servlet.servletfileupload;public class Fileuploadservlet extends HttpServlet {private Static final Long serialversionuid = 1L; @Override protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexcep tion, ioexception {file File=new file ("D:/upload");//upload file save location file Tempfile=new file ("D:/tempload");//upload Cache Pro When folder if (!file.exists ()) {File.mkdir (); } if (!tempfile.exists ()) {Tempfile.mkdir (); }//determine whether to upload the file form, that is, whether there is a enctype= in the form"Multipart/form-data" entry; must post commit, get commit has a limit on content length if (servletfileupload.ismultipartcontent (Request)) {Diskfil Eitemfactory factory=new diskfileitemfactory (); Factory.setsizethreshold (1024*512);//Set Buffer size factory.setrepository (tempfile);//Set Temp folder Servletfileup Load Fileupload=new servletfileupload (factory); Fileupload.setfilesizemax (10*1024*1024);//Set Maximum file size try {@SuppressWarnings ("Unchecke D ") list<fileitem> items=fileupload.parserequest (request);//Get All forms for (Fileitem item:i tems) {//Determines whether the current form control is a normal control if (!item.isformfield ()) {//is a file control String Name=item.getname (); System.out.println ("Upload file name:" +name); SYSTEM.OUT.PRINTLN ("File Size:" +item.getsize ()); String extname=name.substring (name.lastindexof(".")); String newname=new SimpleDateFormat ("Yyyymmddhhmmssms"). Format (new Date ()); System.out.println ("extension" +extname); Item.write (New File ("d:/upload/" +newname+extname)); }else{//is a normal control when String desc=item.getstring ("UTF-8"); SYSTEM.OUT.PRINTLN ("Document description" +desc); }}} catch (Exception e) {e.printstacktrace (); }}else{throw new RuntimeException ("Set the enctype attribute of Form form"); } response.sendredirect ("index.jsp?code=001"); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexcepti On, IOException {doget (req, resp); }}Here, the name of the uploaded file has been changed to prevent uploading the same name file (if the same name, before overwriting);
It is also important to note that if you put these upload files in Webroot and directory, file path can not be "d:/upload"
Pass
String Webrootpath = Getservletcontext (). Getrealpath ("/");
System.out.println ("Webrootpath:" + Webrootpath);
File File = new file (webrootpath+ "upload");
Get Webroot, and finally change item.write (* * * *) to
Item.write (New File (webrootpath+ "upload/" +newname+extname));
This will save the uploaded file to the upload folder under Webroot.
Java Code Implementation file upload (reprint)