美文网首页
spring boot 常用文件上传下载操作

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

作者: 丁春秋永浩 | 来源:发表于2018-08-22 21:29 被阅读0次

文件上传下载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

相关文章

网友评论

      本文标题:spring boot 常用文件上传下载操作

      本文链接:https://www.haomeiwen.com/subject/ipkuiftx.html