使用struts2以不同的文件名上载和保存文件

使用struts2以不同的文件名上载和保存文件,struts2,Struts2,我想用不同的文件名保存使用struts 2上传的文件 原始文件名类似于xyz.xls到xyz-jan-01。xls上载的文件在操作中作为文件提供。用鼠标移动它 有关详细信息,请参见和。代码应如下所示 public String execute() { try { String filePath = ServletActionContext.getServletContext().getRealPath("/uploads"); S

我想用不同的文件名保存使用struts 2上传的文件


原始文件名类似于xyz.xls到xyz-jan-01。xls

上载的文件在操作中作为
文件提供。用鼠标移动它


有关详细信息,请参见和。

代码应如下所示

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;
    }
你可以找到