最新文章


springboot2.x文件上传下载
springboot集成了springMvc,所以springboot的文件上传与springMvc的文件上传方式基本相同。项目构建项目依赖<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><!--thymeleaf模板引擎 --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-devtools</artifactId>    <scope>runtime</scope>    <optional>true</optional></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>    <scope>test</scope></dependency> yml配置文件spring:  thymeleaf:    cache: false  servlet:    multipart:      max-file-size: 1MB #文件上传单文件限制大小      max-request-size: 10MB #文件上传一次请求总大小file:  upload_path: D:\F\img\upload-test\ #文件上传目标目录单文件上传上传HTML单个文件上传:<form action="/file/uploadOneFile" method="post" enctype="multipart/form-data">    <input type="file" name="file"/>    <input type="submit" value="上传"/></form> 上传Controller//文件上传路径@Value("${file.upload_path}")private String uploadPath; /** * 当文件上传页面 * * @return */@GetMapping("/oneFilePage")public String oneFilePage() {    return "/file/oneFilePage";}/** * 单文件上传 * * @param file * @return */@PostMapping("/uploadOneFile")@ResponseBodypublic String uploadOneFile(MultipartFile file) {    //判断是否提交文件    if (file.getSize() == 0) {        return "请选择上传文件";    }    File uploadPathObj = new File(uploadPath);    //判断上传目录是否存在    if (!uploadPathObj.exists()) {        uploadPathObj.mkdirs();    }    //获取文件名    String originalFilename = file.getOriginalFilename();    //上传文件全路径名称    String filePathName = uploadPath + originalFilename;    //上传文件对象    File uploadFileObj = new File(filePathName);    try {        //上传文件        file.transferTo(uploadFileObj);    } catch (IOException e) {        e.printStackTrace();        return "上传失败";    }    return "上传成功";} 选择文件后点击“上传”按钮上传文件,上传成功的提示: 多文件上传上传HTML多个文件上传:<form action="/file/uploadFiles" method="post" enctype="multipart/form-data">    文件1 : <input type="file" name="files"/><br/>    文件2 : <input type="file" name="files"/><br/>    <!--文件3 : <input type="file" name="files"/><br/>-->    <input type="submit" value="上传"/></form> 上传Controller//文件上传路径@Value("${file.upload_path}")private String uploadPath; /** * 多文件上传页面 * * @return */@GetMapping("/manyFilePage")public String manyFilePage() {    return "/file/manyFilePage";}/** * 多文件上传 * * @param files * @return */@PostMapping("/uploadFiles")@ResponseBodypublic String uploadFiles(MultipartFile[] files) {    //判断是否提交文件    if (files == null || files.length == 0) {        return "请选择上传文件";    }    File uploadPathObj = new File(uploadPath);    //判断上传目录是否存在    if (!uploadPathObj.exists()) {        uploadPathObj.mkdirs();    }    //遍历上文件传    for (MultipartFile file : files) {        //获取文件名        String originalFilename = file.getOriginalFilename();        //上传文件全路径名称        String filePathName = uploadPath + originalFilename;        //上传文件对象        File uploadFileObj = new File(filePathName);        try {            //上传文件            file.transferTo(uploadFileObj);        } catch (IOException e) {            e.printStackTrace();            return "上传失败";        }    }    return "上传成功";} 选择文件后点击“上传”按钮上传文件,上传成功的提示: 单文件下载下载HTML模拟文件下载:<form action="/file/downloadFile" method="GET">    下载文件:<input type="text" name="file" value="D:\F\img\upload-test\微信截图_20190826143608.png" readonly/><br>    <input type="submit" value="下载"/></form> 下载Controller/** * 单文件下载 * * @param response * @param file * @return */@GetMapping("/downloadFile")@ResponseBodypublic String downloadFile(HttpServletResponse response, String file) {    if (file == null) {        return "不存在的文件";    }    //下载文件对象    File downloadFileObj = new File(file);    //文件名    String fileName = downloadFileObj.getName();    //获取字节输出流    try {        OutputStream outputStream = response.getOutputStream();        // 自动判断下载文件类型        response.setContentType("multipart/form-data");        //相应编码        response.setCharacterEncoding("utf-8");        // 设置文件名称        response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("gbk"), "iso8859-1"));        //写入输入流        outputStream.write(FileCopyUtils.copyToByteArray(downloadFileObj));        //刷新柳        outputStream.flush();        //关闭流        outputStream.close();    } catch (IOException e) {        e.printStackTrace();        return "下载失败";    }    return "下载成功";}点击“下载”按钮便可以下载对应路径的文件。
Aug 29, 2019 6:37:48 PM
236
3
个人名片

  欢迎来到“浩瀚星尘”的个人博客!
  首先,该博客用于分享本人的生活事迹与兴趣爱好; 此外,该博客的主要作用便是与广大的小伙伴一起分享探讨开发技术, 希望大家多多关照。

网名: 浩瀚星尘
城市: 重庆
工作: java