Java 使用Spring将文件保存到资源目录

Java 使用Spring将文件保存到资源目录,java,spring,tomcat,Java,Spring,Tomcat,我有这个项目结构: /webapp /res /img /profile.jpg /WEB-INF 我需要将文件保存到res/img/目录。这一次,我有以下代码: public String fileUpload(UploadedFile uploadedFile) { InputStream inputStream = null; OutputStream outputStream = null; Multipa

我有这个项目结构:


/webapp
  /res
    /img
      /profile.jpg
  /WEB-INF
我需要将文件保存到
res/img/
目录。这一次,我有以下代码:


public String fileUpload(UploadedFile uploadedFile) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        MultipartFile file = uploadedFile.getFile();
        String fileName = file.getOriginalFilename();
        File newFile = new File("/res/img/" + fileName);

        try {
            inputStream = file.getInputStream();

            if (!newFile.exists()) {
                newFile.createNewFile();
            }
            outputStream = new FileOutputStream(newFile);
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return newFile.getAbsolutePath();
    }
但它会将文件保存到
user.dir
目录,即
~/Work/Tomcat/bin/

那么我如何将文件上传到
res
目录呢?

你不应该真的在那里上传文件

如果您正在使用war,重新部署将删除它们。如果它们是临时的,则使用操作系统指定的临时位置

如果要在以后发布这些文件,请选择在服务器上存储文件的位置,让应用程序知道该位置,并从该位置保存和加载文件

如果您试图动态替换资源,例如HTML或CSS模板中引用的图像,则考虑单独发布外部位置,可以使用MVC:资源:例如:

<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir"/>

请记住,您需要将/absolute/path/to/image/dir更改为实际存在的路径,同时我建议查看,以找到更好的方法来处理文件和资源。

请参阅文件上载控制器,以将文件保存到指定目录

public String fileUpload(UploadedFile uploadedFile) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    MultipartFile file = uploadedFile.getFile();

    String rootPath = System.getProperty("user.dir");
    File dir = new File(rootPath + File.separator + "webapp"+File.separator+"res"+File.separator+"img");
    if (!dir.exists())
        dir.mkdirs();
    String fileName = file.getOriginalFilename();
    File serverFile = new File(dir.getAbsolutePath() + File.separator + fileName);

    try {
        inputStream = file.getInputStream();

        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        outputStream = new FileOutputStream(newFile);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return newFile.getAbsolutePath();
}

不要在那里上传文件。选择一个单独的专用目录。@SotiriosDelimanolis为什么
res
目录不合适?因为它不一定存在。
Servlet
容器可能选择不扩展您的
.war
@SotiriosDelimanolis谢谢您的回答。你能写下上传和保存一些用户内容(如头像、文档等)的最佳实践吗。谢谢谢谢你能告诉我,我如何在我的代码中调用
/absolute/path/。
来存储这些数据吗?`我是说另一个。是否有机会通过方法调用在控制器中接收此地址?还是我要一直自己写这条路?你在说什么?将文件放在项目(而不是tomcat)的webapp文件夹中与此处讨论的内容不同。完全离题
private String imagesFolder;
public void setImagesFolder(String imagesFolder) {
    this.imagesFolder = imagesFolder;
}
public String fileUpload(UploadedFile uploadedFile) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    MultipartFile file = uploadedFile.getFile();
    String fileName = file.getOriginalFilename();
    File newFile = new File(imagesFolder + fileName);

    try {
        inputStream = file.getInputStream();

        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        outputStream = new FileOutputStream(newFile);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return newFile.getAbsolutePath();
}
public String fileUpload(UploadedFile uploadedFile) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    MultipartFile file = uploadedFile.getFile();

    String rootPath = System.getProperty("user.dir");
    File dir = new File(rootPath + File.separator + "webapp"+File.separator+"res"+File.separator+"img");
    if (!dir.exists())
        dir.mkdirs();
    String fileName = file.getOriginalFilename();
    File serverFile = new File(dir.getAbsolutePath() + File.separator + fileName);

    try {
        inputStream = file.getInputStream();

        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        outputStream = new FileOutputStream(newFile);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return newFile.getAbsolutePath();
}