上传文件
@Controller @RequestMapping("/file") public class FileController { @PostMapping("/upload") @ResponseBody public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) { if (!file.isEmpty()) { long l = System.currentTimeMillis(); Date date = new Date(l); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss_"); String prefix = dateFormat.format(date); String saveFileName = prefix + file.getOriginalFilename(); File saveFile = new File(request.getSession().getServletContext().getRealPath("/upload/") +"/" +saveFileName); if (!saveFile.getParentFile().exists()) { saveFile.getParentFile().mkdirs(); } try { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile)); out.write(file.getBytes()); out.flush(); out.close(); return "success"; } catch (FileNotFoundException e) { e.printStackTrace(); return "failure"; } catch (IOException e) { e.printStackTrace(); return "failure"; } } else { return "failure"; } } }
下载文件
@Controller
@RequestMapping
public class ToFileListController {
@Autowired
private ServletContext servletContext;
@RequestMapping("/fileList")
public String fileList(Model model){
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> files = getFiles(servletContext.getRealPath("/upload/"));
for(int i = 0;i<files.size();i++) {
File tempFile =new File( files.get(i).trim());
String fileName = tempFile.getName();
list.add(fileName);
}
model.addAttribute("files", list);
return "fileList";
}
public static ArrayList<String> getFiles(String path) {
ArrayList<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
files.add(tempList[i].toString());
}
}
return files;
}
}
@Controller @RequestMapping public class DownloadFileController { @Autowired private ServletContext servletContext; @RequestMapping(value = "/testDownload", method = RequestMethod.GET) public void Download(HttpServletRequest req , HttpServletResponse res) { String fileName = req.getParameter("name"); res.setHeader("content-type", "application/octet-stream"); res.setContentType("application/octet-stream"); res.setHeader("Content-Disposition", "attachment;filename=" + fileName); byte[] buff = new byte[1024]; BufferedInputStream bis = null; OutputStream os = null; try { os = res.getOutputStream(); bis = new BufferedInputStream(new FileInputStream( new File(servletContext.getRealPath("/upload/") +"/"+fileName))); int i = bis.read(buff); while (i != -1) { os.write(buff, 0, buff.length); os.flush(); i = bis.read(buff); } } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("success"); } }
下载列表
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Title</title> <style type="text/css"> a{ text-decoration:none; } table , td ,th{ border:1px solid black; border-collapse:collapse;/*细线表格,合并边框*/ } </style> </head> <body> <div style="margin-top: 20px"></div> <table id="content" width="800" border="0" cellspacing="0" cellpadding="0"> <caption align="top">异常文件列表</caption> <c:forEach items="${files}" var="user" varStatus="status"> <tr align="center"> <td height="40px"> ${status.index}</td> <td> ${user}</td> <td><a href="/testDownload?name=${user}">下载</a></td> </tr> </c:forEach> </table> </body> </html>