标签:ada success contex man max tran catch 上传 highlight
CommonsMultipartResolver是基于Apache的Commons FileUpload来实现文件上传功能的,主要作用是配置文件上传的一些属性。
1)依赖Apache的Commons FileUpload,所以Maven的pom文件添加依赖;
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
2)applicationContext.xml配置bean
<!-- 文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="209715200"/> <property name="maxInMemorySize" value="40960"/> </bean>
3)id的来源,查看springMVC的核心控制器DispatcherServlet,有一些bean名字常量的定义
lic class DispatcherServlet extends FrameworkServlet {
public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";
public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver";
public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver";
public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping";
public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter";
public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver";
public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator";
public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver";
public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager";
1)流程:
客户端请求必须是MultipartHttpServletRequest(HttpServletRequest的一个子类)请求 ---> spring的DispatcherServlet会调用方法checkMultipart()验证 ---> multipartResolver(会检验配置文件里面配置的一些属性:比如文件的最大值)
2)检验后,代码对文件的处理例子
@RequestMapping("/fileUpload")
public AjaxList fileUpload(HttpServletRequest request,HttpServletResponse response){
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
MultipartFile file = multiRequest.getFile("fileName");
if(file!=null){
File localFile = new File("D:/"+file.getOriginalFilename());
try {
file.transferTo(localFile); //将上传文件写到服务器上指定的文件
} catch (Exception e) {
e.printStackTrace();
return AjaxList.createError("服务器异常");
}
}
return AjaxList.createSuccess("上传成功");
}
标签:ada success contex man max tran catch 上传 highlight
原文地址:https://www.cnblogs.com/knsbyoo/p/9047055.html