Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving file to resource directory using Spring

I have this project structure:


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

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

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

like image 667
Petr Shypila Avatar asked Jan 11 '14 13:01

Petr Shypila


2 Answers

You shouldn't really be uploading files there.

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.

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.

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

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.

like image 75
Ned Avatar answered Oct 18 '22 22:10

Ned


Please refer FileUploadController from here to save file to the specified directory.

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();
}
like image 4
user3556304 Avatar answered Oct 18 '22 23:10

user3556304