FreeMarker模板进行文件的上传和下载 在src/main/resources/templates下新建一个index.ftl文件,内容分别利用表单提交的方式写了两个表单 单个上传与批量上传,并且提供超链接的方式提供了一个下载方法 index.ftl:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>${msg}</title> </head> <body> <p>单文件上传</p> <form action="upload" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="file"/> <input type="submit"/> </form> <hr/> <p>文件下载</p> <form action="batch" method="post" enctype="multipart/form-data"> <p>文件1:<input type="file" name="file"/></p> <p>文件2:<input type="file" name="file"/></p> <p><input type="submit" value="上传"/></p> </form> </body> </html>创建页面跳转Controller方法
package com.zcw.zcw_springboot.zcw_springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; /** * @author zhaocunwei * @ClassName: MultipartController * @Description: 文件上传 * @date 2019/11/1 15:45 */ package com.zcw.zcw_springboot.zcw_springboot.controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author zhaocunwei * @ClassName: MultipartController * @Description: 文件上传 * @date 2019/11/1 15:45 */ @Controller public class MultipartController { @GetMapping("/index_file") public String index(Model modelMap){ modelMap.addAttribute("msg","文件上传下载"); return "index"; } } public class MultipartController { @GetMapping("/index_file") public String index(Model modelMap){ modelMap.addAttribute("msg","文件上传下载"); return "index"; } }编写文件上传下载的Controller:
package com.zcw.zcw_springboot.zcw_springboot.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.List; /** * @author zhaocunwei * @ClassName: FileController * @Description: 提供文件上传与下载 * @date 2019/11/1 15:49 */ @RestController @Slf4j public class FileController { private static final String filePath="D:/08zhaocunwei/downloads"; @RequestMapping(value="/upload") public String upload(@RequestParam("file") MultipartFile file){ if(file.isEmpty()){ return "文件为空"; } try { //获取文件名 String fileName = file.getOriginalFilename(); log.info("上传的文件名称为》》》,{}",fileName); //设置文件存储路径 String path=filePath+fileName; File dest = new File(path); //检测是否存在目录 if(!dest.getParentFile().exists()){ //新建文件夹 dest.getParentFile().mkdirs(); } //文件写入 file.transferTo(dest); return "文件上传成功"; } catch (IOException e) { e.printStackTrace(); } return "上传失败"; } @PostMapping("/batch") public String handleFileUpload(HttpServletRequest request){ List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files"); MultipartFile file = null; BufferedOutputStream stream = null; for(int i=0;i<files.size();++i){ file=files.get(i); if(!file.isEmpty()){ try { byte [] bytes =file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream( //设置文件的路径及名字 new File(filePath+file.getOriginalFilename()) )); //写入 stream.write(bytes); stream.close(); } catch (IOException e) { stream=null; return "第"+i+"个文件上传失败=====>"+e.getMessage(); } }else { return "第"+i+"个文件上传失败,因为文件为空"; } } return "上传成功"; } @GetMapping("/download") public String downloadFile(HttpServletResponse response){ //文件名称 String fileName="zcw.jpg"; if(fileName !=null){ //设置文件路径 File file = new File(filePath+fileName); if(file.exists()){ response.setContentType("application/force-download"); //设置强制下载不打开 response.addHeader("Content-Disposiution","attachment;fileName="+fileName); byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis=new FileInputStream(file); bis =new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i !=i){ os.write(buffer,0,i); i=bis.read(buffer); } return "下载成功"; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(bis !=null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis !=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } return "下载失败"; } }配置文件:
#spring: # thymeleaf: # mode: HTML # prefix: classpath:/templates/ # suffix: .ftl spring: freemarker: request-context-attribute: req suffix: .ftl content-type: text/html enabled: true template-loader-path: classpath:/templates/ charset: utf-8 cache: falsepom文件:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>测试: