Java Struts2文件上载后是否将文件复制到其他文件夹?

Java Struts2文件上载后是否将文件复制到其他文件夹?,java,struts2,Java,Struts2,我正在通过struts2文件上传拦截器上传用户档案图片。上传图片后,我想将其保存在我的webapp中的webapp/upload/images文件夹下。我使用下面的代码来实现这一点 public String uploadPhoto() { try { String filePath = servletRequest.getContextPath() + "/uploads/images"; System.out.println("Server path:

我正在通过struts2文件上传拦截器上传用户档案图片。上传图片后,我想将其保存在我的webapp中的webapp/upload/images文件夹下。我使用下面的代码来实现这一点

public String uploadPhoto() {
    try {
        String filePath = servletRequest.getContextPath() + "/uploads/images";
        System.out.println("Server path:" + filePath);
        File fileToCreate = new File(filePath, this.userImageFileName);

        FileUtils.copyFile(this.userImage, fileToCreate);
    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }
    return SUCCESS;
}
但我有以下错误

Server path:/picvik/uploads/images
java.io.IOException: Destination '/picvik/uploads/images/one.jpg' directory cannot be created
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:777)
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:731)
    at com.picvik.action.ChangePhotoAction.uploadPhoto(ChangePhotoAction.java:28)

请纠正我做错了什么。以及如何做到这一点。我基本上想将所有用户的个人资料图片保存在我的webapp下:webapp/uploads/images。

这很简单,你不能使用相对路径创建文件,你应该使用它的真实路径:

String filePath = servletRequest.getContextPath().getRealPath("/uploads/images");

那应该没问题。

但有一点需要注意,出于各种原因,您几乎肯定不应该将图像保存到web应用程序中的目录中。