files.FileStorage.java Source code

Java tutorial

Introduction

Here is the source code for files.FileStorage.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package files;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author Rice Pavel
 */
@Component
public class FileStorage {

    public final static String filePath = "/usr/local/vuz/files/";

    public void saveFile(Long id, MultipartFile file) throws IOException {
        File newFile = new File(filePath + id);
        FileUtils.writeByteArrayToFile(newFile, file.getBytes());
    }

    public void saveFile(Long id, InputStream is) throws IOException {
        File newFile = new File(filePath + id);
        FileUtils.copyInputStreamToFile(is, newFile);
    }

    public void saveFile(Long id, byte[] bytes) throws IOException {
        File newFile = new File(filePath + id);
        FileUtils.writeByteArrayToFile(newFile, bytes);
    }

    public void deleteFile(Long id) {
        File file = new File(filePath + id);
        file.delete();
    }

    public File getFile(Long id) {
        return new File(filePath + id);
    }

}