首页>代码>SpringBoot上传文件简单实例>/springboot-upload-file/src/main/java/com/cicoding/storage/FileSystemStorageService.java
package com.cicoding.storage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

@Service
public class FileSystemStorageService implements StorageService {

    private final Path rootLocation;

    @Autowired
    public FileSystemStorageService(StorageProperties properties) {
        this.rootLocation = Paths.get(properties.getLocation());
    }

    @Override
    public void store(MultipartFile file) {
        try {
            if (file.isEmpty()) {
                throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
            }
            Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
        } catch (IOException e) {
            throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
        }
    }

    @Override
    public Stream<Path> loadAll() {
        try {
            return Files.walk(this.rootLocation, 1)
                    .filter(path -> !path.equals(this.rootLocation))
                    .map(path -> this.rootLocation.relativize(path));
        } catch (IOException e) {
            throw new StorageException("Failed to read stored files", e);
        }

    }

    @Override
    public Path load(String filename) {
        return rootLocation.resolve(filename);
    }

    @Override
    public Resource loadAsResource(String filename) {
        try {
            Path file = load(filename);
            Resource resource = new UrlResource(file.toUri());
            if(resource.exists() || resource.isReadable()) {
                return resource;
            }
            else {
                throw new StorageFileNotFoundException("Could not read file: " + filename);

            }
        } catch (MalformedURLException e) {
            throw new StorageFileNotFoundException("Could not read file: " + filename, e);
        }
    }

    @Override
    public void deleteAll() {
        FileSystemUtils.deleteRecursively(rootLocation.toFile());
    }

    @Override
    public void init() {
        try {
            Files.createDirectory(rootLocation);
        } catch (IOException e) {
            throw new StorageException("Could not initialize storage", e);
        }
    }
}
最近下载更多
微信网友_7134912998903808  LV15 2024年9月11日
xiaokang1  LV10 2024年4月25日
ssh123  LV10 2024年4月24日
Qolmen  LV12 2024年1月3日
黄小熙  LV7 2023年10月13日
lcqlcl  LV11 2023年8月29日
ship007  LV10 2023年8月9日
3159792465  LV10 2023年4月29日
regedit418  LV12 2022年9月1日
天险无涯  LV15 2022年8月30日
最近浏览更多
微信网友_6377331253415936  LV3 2024年11月5日
微信网友_7134912998903808  LV15 2024年9月2日
chn-lei  LV2 2024年8月24日
shuangfu  LV25 2024年5月17日
welcome丶  LV8 2024年4月26日
xiaokang1  LV10 2024年4月25日
ssh123  LV10 2024年4月24日
13521878735  LV3 2024年3月27日
zhaixunfei  LV8 2024年2月8日
Qolmen  LV12 2024年1月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友