Om M
Om M

Reputation: 201

File upload and save with different file name using struts2

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

Answers (2)

coding_idiot
coding_idiot

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

Dave Newton
Dave Newton

Reputation: 160301

Uploaded files are available as Files 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

Related Questions