spring boot 常用文件上传下载操作

文件上传下载controller

@Controller
public class FileController {

    @Autowired
    UploadConfig uploadConfig;

    @RequestMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file){

        if(file == null){
            return Result.error("文件上传为空");
        }
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString();
        if(originalFilename.contains(".")){
            newName +=originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        FileOutputStream fos=null;
        try {
            fos = new FileOutputStream(uploadConfig.getFileSavePath()+newName);
            IOUtils.copy(file.getInputStream(),fos);
            return Result.success(uploadConfig.getWebPath()+newName);
        }catch (Throwable e){
            return Result.error("上传文件失败");
        }finally {
            cloaseStream(fos);
        }

    }

    @RequestMapping("/webFile/**")
    public void webFile(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String uri = request.getRequestURI();
        String filePath = uri.replaceAll(uploadConfig.getWebPath(), uploadConfig.getFileSavePath());
        File file = new File(filePath);
        if(!file.exists()){
            response.sendError(404,"文件不存在");
            return;
        }
        ServletOutputStream outputStream = response.getOutputStream();
        InputStream inputStream = new FileInputStream(file);
        try {
            IOUtils.copy(inputStream,outputStream);
        }catch (Exception e){

        }finally {
            cloaseStream(outputStream);
            cloaseStream(inputStream);
        }

    }


    private void cloaseStream(Closeable closeable){
        try {
            if(closeable!=null){
                closeable.close();
            }
        }catch (Exception e){

        }
    }
    @RequestMapping("/download/webFile/{fileName}")
    public void download(@PathVariable("fileName") String fileName,HttpServletResponse response) throws IOException{

        if(StringUtils.isEmpty(fileName)){
            response.sendError(404,"文件不存在");
            return;
        }
        String savePath = uploadConfig.getFileSavePath()+fileName;
        File file = new File(savePath);
        if(!file.exists()){
            response.sendError(404,"文件不存在");
            return;
        }
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        InputStream in = null;

        try {
                in= new FileInputStream(savePath);
                IOUtils.copy(in,response.getOutputStream());
        }catch (Exception e){

        }finally {
            cloaseStream(response.getOutputStream());
            cloaseStream(in);
        }


    }
}

prifiles 配置

common.properties

spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=1000MB
#保存路径
#虚拟路径
file.web.path=/webFile/

spring.thymeleaf.prefix=classpath:templates

mac profile

file.save.path=/Users/zyz/uploads/

win profile

file.save.path=D:/Users/zyz/uploads/
spring.profiles.active=win
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容