准备工作
Spring Web MVC也是使用Apache的common-fileupload,因此要在lib里面添加如下的jar包:
commons-fileupload-1.2.2.jar、commons-io-2.4.jar
为了处理中文文件名称的上传下载,把前面HelloWorld讲过的filter配置上
在Spring的配置文件中,配置处理文件上传的bean,如下:
<bean id= "multipartResolver" class= "org.springframework.web.multipart.commons.CommonsMultipartResolver "> <property name= "maxUploadSize" value= "104857600"/> </bean>
配置上就好了,Spring会自动找的
文件上传示例,先看Controller部分,这里只是一个最基本的演示:
@Controller @RequestMapping(value = "/fileope") public class FileUploadController { @RequestMapping(value = "upload", method = RequestMethod.POST) public String upload( HttpServletRequest request, @RequestParam(value = "myFile", required = false) MultipartFile[] files) { try { for(int i=0;i<files.length;i++){ FileUploadUtils.upload(request, files[i]); } } catch (Exception e) { e.printStackTrace(); } return "success"; } } 对应使用的FileUploadUtils: public class FileUploadUtils { //默认大小 50M public static final long DEFAULT_MAX_SIZE = 52428800; //默认上传的地址 public static String defaultBaseDir = "upload"; public static final String upload(HttpServletRequest request, MultipartFile file)throws Exception{ String filename = extractFilename(file, defaultBaseDir); File desc = getAbsoluteFile(extractUploadDir(request), filename); file.transferTo(desc); return filename; } private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException { if(uploadDir.endsWith("/")) { uploadDir = uploadDir.substring(0, uploadDir.length() - 1); } if(filename.startsWith("/")) { filename = filename.substring(0, uploadDir.length() - 1); } File desc = new File(uploadDir + "/" + filename); if(!desc.getParentFile().exists()) { desc.getParentFile().mkdirs(); } if(!desc.exists()) { desc.createNewFile(); } return desc; } public static final String extractFilename(MultipartFile file, String baseDir) throws UnsupportedEncodingException { String filename = file.getOriginalFilename(); int slashIndex = filename.indexOf("/"); if (slashIndex >= 0) { filename = filename.substring(slashIndex + 1); } filename = baseDir + "/" + filename; return filename; } public static final String extractUploadDir(HttpServletRequest request) { return request.getServletContext().getRealPath("/"); } }
上传的示例页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <form action="/mvcexample/fileope/upload" method="post" enctype="multipart/form-data"> file1:<input type="file" name="myFile"/> <br> file2:<input type="file" name="myFile"/> <br> <input type="submit" value="上传"> </form>
文件下载
下载的示例Controller的方法
@RequestMapping(value = "download", method = RequestMethod.GET) public static void download(HttpServletRequest request, HttpServletResponse response, String fileName) throws Exception { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); BufferedInputStream bis = null; BufferedOutputStream bos = null; String newFileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); String ctxPath = request.getSession().getServletContext() .getRealPath("/") + FileUploadUtils.defaultBaseDir; String downLoadPath = ctxPath +"/"+ newFileName; long fileLength = new File(downLoadPath).length(); response.setHeader("Content-disposition", "attachment; filename=" + new String(newFileName.getBytes("gb2312"), "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); } bis.close(); bos.close(); }
相关推荐
本书共计10章,分别介绍了快速搭建Spring Web应用、精通MVC结构、URL映射、文件上传与错误处理、创建Restful应用、保护应用、单元测试与验收测试、优化请求、将Web应用部署到云等内容,循序渐进地讲解了Spring MVC4...
Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发...
spring mvc 框架下的文件上传,代码有web.xml配置 ,spring的配置 ,以及控制器代码等
全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...
这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类, 包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 spring的核心类,提供了核心HTTP...
全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...
全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...
全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...
自己实现的spring ioc+mvc框架,具体流程参考的spring,可以加深对spring源码流程的理解
全书共计12章,分别从Spring框架、模型2和MVC模式、Spring MVC介绍、控制器、数据绑定和表单标签库、传唤器和格式化、验证器、表达式语言、JSTL、国际化、上传文件、下载文件多个角度介绍了Spring MVC。除此之外,...
版本管理,服务根路径工具类,文件上传工具类 1.0.10 集成ueditor在线编辑器 1.0.11 地址联动 1.0.12 Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) ...
Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...
Spring MVC是Spring框架中的一部分,全称是Spring Web MVC,主要用于实现MVC设计模式的Web框架。它分离了控制器、模型对象、过滤器以及处理程序...此外,Spring MVC还支持本地化/国际化(Locale)解析及文件上传等。
本书共计10章,分别介绍了快速搭建Spring Web应用、精通MVC结构、URL映射、文件上传与错误处理、创建Restful应用、保护应用、单元测试与验收测试、优化请求、将Web应用部署到云等内容,循序渐进地讲解了Spring MVC4...
通过小而简单的示例演示Spring MVC Web框架的功能。 在回顾了这个展示之后,您应该对Spring MVC可以做什么有一个很好的了解,并了解它的易用性。 包括项目代码以及支持的幻灯片显示和屏幕投射。 在此展示中,您将...