说明:SpringMVC框架搭建和上传多文件上传请参考之前的播客
SpringMVC框架搭建 http://blog.youkuaiyun.com/liqingwei168/article/details/79137801
文件上传 http://blog.youkuaiyun.com/liqingwei168/article/details/79148388
多文件上传 http://blog.youkuaiyun.com/liqingwei168/article/details/79148989
文件下载比较简单 Controller中代码
@RequestMapping(value="/downFile") public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("id") String id, Model model)throws Exception { UpDownQuery query = new UpDownQuery(); query.setId(id); List<UpDown> upDownList = upDownService.findUpDownAll(query); if(upDownList.size()<0){ return null; } //下载文件路径 String path = upDownList.get(0).getUrl()+"/"; String allName = upDownList.get(0).getAllName(); File file = new File(path + File.separator + allName); HttpHeaders headers = new HttpHeaders(); //下载显示的文件名,解决中文名称乱码问题 String downloadFielName = new String(allName.getBytes("UTF-8"),"iso-8859-1"); //通知浏览器以attachment(下载方式)打开图片 headers.setContentDispositionFormData("attachment", downloadFielName); //application/octet-stream : 二进制流数据(最常见的文件下载)。 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }
jsp页面代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <title>下载功能在线调试</title> </head> <body> <c:forEach items="${upDownList}" var="upDownList"> <a href="/upload/downFile?id=${upDownList.id}">${upDownList.firstName}---下载</a> </c:forEach> </body> </html>
写的简单一点