I. The <form> method property of the submission form must be post and add Enctype= "Multipart/form-data" property
Front desk:
<td> Upload qualification: </td><td><input type= "file" name= "upload" > </td>
Uploadutils Tool Class
Package com.home.utils;import java.util.uuid;/** * File Bed tool class * @author Administrator * */public class Uploadutils {/** * incoming File name, return unique name * @param filename * @return */public static string Getuuidname (string filename) {//Find first search from backward forward int index = Filena Me.lastindexof ("."); /intercept suffix string lastname = filename.substring (Index,filename.length ());//system.out.println (filename);//Unique string UUID = Uuid.randomuuid (). toString (). Replace ("-", "");//default with-return Uuid+lastname;} public static void Main (string[] args) {String filename = "gril.jsp"; SYSTEM.OUT.PRINTLN (getuuidname (filename));}}
Add File upload path properties in domain and other files
Web layer code (together with adding customers)
/** * File Upload, you need to define the properties of the member in the Action class, the name is regular!! * Private File upload; File class * Private String uploadfilename that represents the files to be uploaded (consistent with the foreground name) IO package; Indicates the name of the uploaded file (no Chinese garbled--resolved) * Private String uploadcontenttype;//represents the MIME type of the uploaded file * Provides a set method, the Interceptor injects value */private file upload; private string Uploadfilename;private string uploadcontenttype;public void Setupload (File upload) {this.upload = upload;} public void Setuploadfilename (String uploadfilename) {this.uploadfilename = Uploadfilename;} public void Setuploadcontenttype (String uploadcontenttype) {this.uploadcontenttype = Uploadcontenttype;} Public String Save () throws ioexception{//file upload, indicating that the user has selected the uploaded file if (uploadfilename!=null) {//Print//system.out.println (" File name: "+uploadfilename"); System.out.println ("File type:" +uploadcontenttype);//Handle the name string uuidname = Uploadutils.getuuidname (uploadfilename) ;//upload files to d:\\tomcat8.0\\webapps\\uploadstring path = "d:\\tomcat8.0\\webapps\\upload\\";//Create file Object File File = new File (path+uuidname);//Simple way Fileutils.copyfile (upload, file);//ORG.APACHE.COmmons.io Package//File upload path, save to Customer table Customer.setfilepath (Path+uuidname);} Customerservice.save (customer); return "Save";}
* * Note
1. File size is very limited (default 2097152 is 2M), you can set your own
Add in the Struts.xml file
<!--set the total size of the upload file--><constant name= "struts.multipart.maxSize" value= "20971520" ></constant>
2. You can also set the allowed extensions
The upload file in the Struts.xml file is added in the action tag
<!--introduce the default interceptor--><interceptor-ref name= "Defaultstack" ><!--decide which type of file to upload--><param name= " Fileupload.allowedextensions ">.jpg,.txt</param></interceptor-ref>
Javaweb ssh File Upload