springMvc上传下载
来源:互联网 发布:照片打印软件 编辑:程序博客网 时间:2024/06/11 20:10
配置spring-mvc.xml配置文件
上传下载只需要配置一个bean
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="20000000"/> </bean>
然后是上传页面
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body><form action="upload.html" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="Submit" /></form> </body></html>
上传的conntroller
UploadController.java
packagenet.spring.controller; import java.io.File;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;importorg.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile; @Controllerpublicclass UploadController {@RequestMapping("/upload")public String upload(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request,HttpServletResponse response, ModelMap model) throws IOException{ System.out.println("开始"); String path =request.getSession().getServletContext().getRealPath("upload"); String fileName =file.getOriginalFilename();// String type=fileName.substring(fileName.lastIndexOf("."),fileName.length());// System.out.println(fileName);// String fileNames = new Date().getTime()+type; System.out.println(path); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } // model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName); model.addAttribute("fileUrl", "/upload/"+fileName); return "result"; } }然后是返回的页面result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'result.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body>文件列表 <ul> <li>${fileUrl}<a href="download.html?fileName=${fileUrl}">下载</a></li> </ul> </body></html>
接着就是下载的方法
DownloadController.java
packagenet.spring.controller; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView; @Controllerpublicclass DownloadController {@RequestMapping("/download")public ModelAndView download(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( "/") + "\\"; String downLoadPath = ctxPath + fileName; System.out.println(downLoadPath); try { long fileLength = new File(downLoadPath).length(); response.setContentType("application/x-msdownload;"); String type=fileName.substring(fileName.lastIndexOf("/")+1,fileName.length()); System.out.println(type); fileName = type; 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; }}
0 0
- springMVC上传下载
- SpringMVC上传下载.
- SpringMVC上传下载
- springMvc上传下载
- springmvc上传下载
- SpringMVC上传下载
- springmvc上传下载
- SpringMVC上传下载
- SpringMVC 上传下载
- springMVC上传下载
- SpringMVC中文件上传下载
- Springmvc上传下载附件
- springmvc文件上传下载
- springmvc文件上传下载
- SpringMVC 文件上传下载
- springmvc文件上传下载
- SpringMVC 上传下载(1)
- springmvc实现文件上传下载
- JavaScript 如何计算2个时间差 工作时间为8小时
- [python 爬虫学习]利用cookie模拟网站登录
- 图像遍历
- 1084. Broken Keyboard (20)
- 2015C++教学日记
- springMvc上传下载
- grep
- Add Two Numbers
- Paint及Canvas的简单应用
- 多校 hdu 5325
- 运用proguard进行代码混淆
- 巧用Java关键字
- app_query.append用法
- Servlet实现用户请求次数的统计