使用Spring将文件保存到资源目录 [英] Saving file to resource directory using Spring

查看:481
本文介绍了使用Spring将文件保存到资源目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个项目结构:


/webapp
  /res
    /img
      /profile.jpg
  /WEB-INF

我需要将文件保存到 res / img / 目录。这次我有这个代码:

And I need to save file to res/img/ directory. This time I have this code:


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 目录?

But it saving files to user.dir directory, which is ~/Work/Tomcat/bin/. So how I can upload files to res directory?

推荐答案

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

You shouldn't really be uploading files there.

如果你正在使用战争,重新部署将删除它们。如果它们是临时的,那么使用os分配的临时位置。

If you are using a war, redeploying will delete them. If they are intended to be temporary then use an os assigned temporary location.

如果您打算在之后发布它们,那么选择在您的服务器上存储文件的位置,使该位置为应用程序所知,并从该位置保存和加载文件。

If you intend to publish them afterwards then choose a location in which to store the files on your server, make this location known to the application and save and load files from the location.

如果您尝试动态替换资源,例如html中引用的图像或css模板,然后考虑单独发布外部位置,你可以使用mvc:资源,例如:

If you are trying to replace resources dynamically such as an image which is referenced in the html or css templates, then consider publishing the external location separately, you can use mvc:resources for this e.g:

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

您可以将文件保存到该位置。这将使部署之间更加永久。

and you would save your files to that location. This will make it more permanent between deployments.

要使用您的代码将图像保存到该位置,您需要将其添加到bean定义中(假设您使用的是xml)没有注释的配置):

To save an image to that location using your code you will need to add this into your bean definition (assuming you are using xml configuration without annotations):

<property name="imagesFolder" value="/absolute/path/to/image/dir"/>

并保持代码尽可能相似,将其更改为:

and keeping your code as similar as possible change it to:

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();
}

请记住,您需要更改/ absolute / path / to / image / dir到存在的实际路径,我也建议看一下 Spring Resources文档,以便更好地处理文件和资源。

Please bear in mind that you need to change /absolute/path/to/image/dir to an actual path that exists, also I would recommend to look at the Spring Resources documentation for a better way to deal with files and resources.

这篇关于使用Spring将文件保存到资源目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆