In the last section we finished adding and updating the function of the product, these two parts are related to the upload of the product picture, and there is no detailed explanation. To this end, this article describes in detail Struts2 implementation file upload function.
1. Package file Information
we first have to have a model to encapsulate the file information, this model needs to have three attributes: file, file type and file name. For the pictures we want to send, we create a new model as follows:
public class Fileimage {
private file file;
Private String ContentType;
private String filename;
Public File GetFile () {return
file;
}
Public String getContentType () {return
contentType;
}
Public String GetFileName () {return
filename;
}
The public void setupload (file file) {//set method may not be the same as the property name, but the parameters in the foreground pass are the same as the set method name. The parameters of the foreground are fileimage.upload
this.file = file;
}
public void Setuploadcontenttype (String contentType) {
this.contenttype = ContentType;
}
public void Setuploadfilename (String filename) {
this.filename = filename;
}
}
So model is written, considering that the logic of file upload is not unique to individual action, so we write the logic of file upload to the tool class, so that all the action calls. So we create a new file Upload Tool class (for interface programming, we also pull the tool class out of the interface):
2. Completing the File Upload Tool class
File Upload Tool class Interface public interface FileUpload {//Implement file Upload function, return upload new file name public abstract String uploadfile (fileimage file
Image); }//File Upload Tool class specifically implemented @Component ("FileUpload") public class Fileuploadutil implements FileUpload {private String file
Path; @Value ("#{prop.filepath}")//@Value means to beans.xml the file to find a id= "prop" Bean, which reads the properties configuration file through annotations, and reads it in the appropriate configuration file key=
The value of FilePath is public void SetFilePath (String filePath) {System.out.println (FilePath);
This.filepath = FilePath; }//1.
Gets the extension private string Getfileext (string filename) {return filenameutils.getextension (filename) by file name; }//2.
Generates a UUID random number, as the new filename private string NewFileName (string filename) {string ext = Getfileext (fileName);
Return Uuid.randomuuid (). toString () + "." + ext; //Implement file Upload function, return upload new file name @Override public String uploadfile (fileimage fileimage) {//Get new unique filename String pic =
NewFileName (Fileimage.getfilename ()); try {fileutil.copyfile (Fileimage.getfile (), New File (FilePath, pic);//The first parameter is uploaded files, the second parameter is to copy the file to the new path under the return pic;
catch (Exception e) {throw new RuntimeException (e);
finally {fileimage.getfile (). Delete ();
}
}
}
There is a @value annotation that gets the path that the file is to be saved from the properties file, see also: Spring gets the profile information.
3. Injection of encapsulated file classes and tool classes in action
having written the file encapsulation class and uploading the file tool class, we need to inject these two objects into our action, so that we can implement the file upload function in the action:
@Controller ("Baseaction")
@Scope ("prototype") Public
class Baseaction<t> extends Actionsupport Implements requestaware,sessionaware,applicationaware,modeldriven<t> {
//encapsulates the class of picture information
protected Fileimage Fileimage;
Upload file Tool class
@Resource
protected fileupload fileupload;
Public Fileimage Getfileimage () {return
fileimage;
}
public void Setfileimage (Fileimage fileimage) {
this.fileimage = fileimage;
}
Omit other extraneous code ...
}
4. Implementation of File Upload
OK, now we can implement the file upload in the productaction, the tool class is written, the amount of code in the action is very small, which is the advantages of encapsulation.
@Controller ("Productaction")
@Scope ("prototype") Public
class Productaction extends Baseaction<product > {
//Omitting other extraneous code
... The public void Save () throws Exception {
//fileupload tool class is extracted, UploadFile method directly accepts a Fileimage object, returns a new picture name
String pic = Fileupload.uploadfile (fileimage);
Model.setpic (pic);
Model.setdate (New Date ());
SYSTEM.OUT.PRINTLN (model);
Commodity information Warehousing
productservice.save (model);
}
public void Update () {
String pic = fileupload.uploadfile (fileimage);
Model.setpic (pic);
Model.setdate (New Date ());
SYSTEM.OUT.PRINTLN (model);
Update Product
productservice.update (model);
}
This completes the function of uploading files from the foreground.
Original address: http://blog.csdn.net/eson_15/article/details/51366384
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.