文件上传、下载、查看

2020-12-02  本文已影响0人  与乐为乐

一、查看文件

@RestController
public class PDFController {

    @RequestMapping(value = {"/web/pdf"})
    public void download(HttpServletResponse response) throws Exception {

        FileInputStream is = new FileInputStream(new File("C:\\*", "*.pdf"));
       // attachent : 下载;  inline : 查看
        response.setHeader("content-disposition", "inline;fileName=" + URLEncoder.encode("*.pdf", "UTF-8"));
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(is, os);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }

}

二、下载文件

@RestController
public class PDFController {

    @RequestMapping(value = {"/web/pdf"})
    public void download(HttpServletResponse response) throws Exception {

        FileInputStream is = new FileInputStream(new File("C:\\*", "*.pdf"));
       // attachent : 下载;  inline : 查看
        response.setHeader("content-disposition", "attachent;fileName=" + URLEncoder.encode("*.pdf", "UTF-8"));
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(is, os);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }

}

三、上传文件

   @RequestMapping(value = {"/web/upload"})
    public String upload(@RequestBody MultipartFile file) throws Exception {

        File filePath = new File("C:\\path", "*.pdf");
        if(!filePath.exists()){
            filePath.mkdirs();
        }

        file.transferTo(filePath);
        return "上传成功";
    }

上一篇下一篇

猜你喜欢

热点阅读