首页 > 代码库 > Springmvc文件上传与下载
Springmvc文件上传与下载
(上传)
1.jar包与springmvc.xml
将request包装成MultipartHttpServletRequest.
2.页面
注意method为post
3.action
方法一解析(request)
方法二(参数)
(下载)
- @RequestMapping("/download/{fileName}")
- public ModelAndView download(@PathVariable("fileName")
- String fileName, HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- response.setContentType("text/html;charset=utf-8");
- request.setCharacterEncoding("UTF-8");
- java.io.BufferedInputStream bis = null;
- java.io.BufferedOutputStream bos = null;
- String ctxPath = request.getSession().getServletContext().getRealPath(
- "/")
- + "\\" + "images\\";
- String downLoadPath = ctxPath + fileName;
- System.out.println(downLoadPath);
- try {
- long fileLength = new File(downLoadPath).length();
- response.setContentType("application/x-msdownload;");
- response.setHeader("Content-disposition", "attachment; filename="
- + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
- response.setHeader("Content-Length", String.valueOf(fileLength));
- bis = new BufferedInputStream(new FileInputStream(downLoadPath));
- bos = new BufferedOutputStream(response.getOutputStream());
- byte[] buff = new byte[2048];
- int bytesRead;
- while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
- bos.write(buff, 0, bytesRead);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bis != null)
- bis.close();
- if (bos != null)
- bos.close();
- }
- return null;
- }
- }
Springmvc文件上传与下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。