SoFunction
Updated on 2025-03-08

SSH Framework Online Mall Project 13th Battle of Struts2 File Upload Function

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 does not need to be the same as the attribute name, but the parameters when transmitted in the foreground must be the same as the set method name.  That is, the parameters transmitted in the front desk are   = file; 
 } 
  
 public void setUploadContentType(String contentType) { 
   = contentType; 
 } 
  
 public void setUploadFileName(String 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 interfacepublic interface FileUpload { 
 
 //Implement the function of file upload and return the new file name after upload public abstract String uploadFile(FileImage fileImage); 
 
} 
 
//Detailed implementation of file upload tool@Component("fileUpload") 
public class FileUploadUtil implements FileUpload { 
  
 private String filePath; 
 @Value("#{}") 
 //@Value means to go to the bean found in the 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) { 
  (filePath); 
   = filePath; 
 } 
 
 //1. Get the extension through the file name private String getFileExt(String fileName) { 
  return (fileName); 
 } 
  
 //2. Generate UUID random number as new file name private String newFileName(String fileName) { 
  String ext = getFileExt(fileName); 
  return ().toString() + "." + ext; 
 } 
  
 //Implement the function of file upload and return the new file name after upload @Override 
 public String uploadFile(FileImage fileImage) { 
  //Get new unique file name  String pic = newFileName(()); 
  try { 
   ((), 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 { 
   ().delete(); 
  } 
 } 
} 

There is a @Value annotation above, which is to obtain the path to the file to be stored from the properties file. For details, please refer to: Spring Get 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> { 
 
 //The class that encapsulates image information protected FileImage fileImage; 
  
 //Upload file tool class @Resource 
 protected FileUpload fileUpload; 
 
 public FileImage getFileImage() { 
  return fileImage; 
 } 
 public void setFileImage(FileImage fileImage) { 
   = fileImage; 
 } 
 //Omit other irrelevant codes...} 

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 codes...  
 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 = (fileImage); 
   
  (pic); 
  (new Date()); 
  (model); 
  //Product information is in the database  (model); 
 } 
  
 public void update() { 
  String pic = (fileImage); 
  (pic); 
  (new Date()); 
  (model); 
  //Update the product  (model); 
 } 
} 

In this way, we complete the function of uploading files from the front desk.

Original address:/eson_15/article/details/51366384

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.