In the previous section, we completed the functions of adding and updating products. These two parts involve uploading product pictures, and there is no detailed explanation. To this end, this article introduces the function of Struts2 to implement file upload.
1. Encapsulate file information
First we have to have a Model to encapsulate the file information. This Model needs three attributes: file, file type and file name. For the picture we want to pass, 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; } public void setUpload(File file) { //The set method can not be the same as the attribute name, but the parameters when passed in from the foreground must be the same as the set method name. That is, the parameter passed in the foreground is fileImage.upload this.file = file; } public void setUploadContentType(String contentType) { this.contentType = contentType; } public void setUploadFileName(String filename) { this.filename = filename; } }In this way, the Model is written. Considering that the logic of file upload is not unique to a single Action, we write the logic of file upload into the tool class, so that all Action calls can be made. So we create a new file upload tool class (for interface programming, we also extract the tool class interface):
2. Complete file upload tool class
//File upload tool class interface public interface FileUpload { //Implement the function of file upload, return the new file name after upload public abstract String uploadFile(FileImage fileImage); } //File upload tool class specific implementation @Component("fileUpload") public class FileUploadUtil implements FileUpload { private String filePath; @Value("#{prop.filePath}") //@Value means to find the bean with id="prop" in the beans.xml file. It reads the properties configuration file through annotation, and then reads the value of key=filePath in the corresponding configuration file public void setFilePath(String filePath) { System.out.println(filePath); this.filePath = filePath; } //1. Get the extension private String getFileExt(String fileName) { return FilenameUtils.getExtension(fileName); } //2. Generate a random UUID number as the new file name private String newFileName(String fileName) { String ext = getFileExt(fileName); return UUID.randomUUID().toString() + "." + ext; } //Implement the function of file upload, return the new file name after upload @Override public String uploadFile(FileImage fileImage) { //Get the new unique file name String pic = newFileName(fileImage.getFilename()); try { FileUtil.copyFile(fileImage.getFile(), new File(filePath, pic)); //The first parameter is the uploaded file, and the second parameter is to copy the file to the new path return pic; } catch (Exception e) { throw new RuntimeException(e); } finally { fileImage.getFile().delete(); } } }There is a @Value annotation above, which is to obtain the path to which the file is to be stored from the properties file. For details, please refer to: Spring to obtain configuration file information.
3. Inject encapsulated file classes and tool classes in Action
After writing the file encapsulation class and the file upload tool class, we need to inject these two objects into our Action, so that the file upload function can be implemented in the Action:
@Controller("baseAction") @Scope("prototype") public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { //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 irrelevant code... } 4. Implement file upload
Okay, now we can implement file upload in ProductAction. If the tool class is written, the amount of code in the Action will be very small, which is also the advantage brought by encapsulation.
@Controller("productAction") @Scope("prototype") public class ProductAction extends BaseAction<Product> { //Omit other irrelevant code... public void save() throws Exception { //The fileUpload tool class is extracted, the uploadFile method directly accepts a fileImage object and returns the new image name String pic = fileUpload.uploadFile(fileImage); model.setPic(pic); model.setDate(new Date()); System.out.println(model); //Product information is stored in 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); } }In this way, we complete the function of uploading files from the front desk.
Original address: http://blog.csdn.net/eson_15/article/details/51366384
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.