Reputation: 201
I want to save the file which is uploaded using struts 2 with different filename
orginal filename is like xyz.xls to xyz-jan-01.xls
Upvotes: 0
Views: 438
Reputation: 13734
The code shall look something like this
public String execute() {
try {
String filePath = ServletActionContext.getServletContext().getRealPath("/uploads");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, "NewFileName");
FileUtils.copyFile(this.userImage, fileToCreate); //userImage is a File
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
You can find the complete example here.
Upvotes: 0
Reputation: 160301
Uploaded files are available as File
s in the action. Move it using Commons IO's moveFile
method.
See the file upload FAQ entry and file upload interceptor docs for details.
Upvotes: 1