文件上传总结:
1、jsp 提交方法必须为post
2、在form标签中添加属性:enctype=“multipart/form-data”
在mvc的handler方法中定义MultipartFile类型来接收用户上传的文件
3、在应用上下文中配置Multipart解析器
jsp
<form action="${pageContext.request.contextPath}/fileUpload" enctype="multipart/form-data" method="post">
<input type="text" name="userName"><br>
<input type="file" name="avatar"><br>
<input type="submit" value="提交">
</form>
controller层
@Controller
public class FileUploadAction {
@RequestMapping("/fileUploadPage")
public String fileUploadPage() {
return "fileupload/fileupload";
}
@RequestMapping("/fileUpload")
public String fileUpload(MultipartFile avatar, String userName) {
//判断是否为空
if(avatar.isEmpty()) {
return "error";
}
//获取源文件名称
String orginName = avatar.getOriginalFilename();
String name = avatar.getName();
System.out.println("===================>" + name);
String destFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(orginName);
File destFile = new File("D:\\DATA\\IMG\\" + destFileName);
try {
avatar.transferTo(destFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
resources 中配置xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件的最大值, 单位为:字节 50*1024*1024
<property name="maxUploadSize" value="52428800"></property>
-->
<property name="maxUploadSize" value="#{50*1024*1024}"/>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<property name="maxUploadSize" value="52428800"></property>
此写法易出错 不推荐使用
json数据交互
若要获取List对象 需要配置上下文
<mvc:annotation-driven>
<mvc:message-converters>
<!-- register-defaults="true"表示使用默认的消息转换器 -->
<!-- FastJson(Spring4.2x以上版本设置) -->
<!-- 使用@responsebody注解并且返回值类型为String时,返回的string字符串带有双引号"{'user':'songfs'}",其原因是直接将string类型转成了json字符串,应该在json解析器之前添加字符串解析器-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<!-- FastJsonHttpMessageConverter4 使@ResponseBody支持返回Map<String,Object>等类型,它会自动转换为json-->
<!-- 需要返回json时需要配置 produces = "application/json"。不需要再指定utf-8了 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 加入支持的媒体类型 -->
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然IE执行AJAX时,返回JSON会出现下载文件 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<list>
<value>AllowArbitraryCommas</value>
<value>AllowUnQuotedFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
</mvc:message-converters></mvc:annotation-driven>
在Controller中
@RequestMapping("userWsList")
@ResponseBody//返回给用户的java对象会被mvc转换成Json格式的数据返回给用户。
public List<UserInfo> userWsList() {
System.out.println("-------------------经过handler方法==============");
return userService.getSUList();
}
@RequestMapping("getData")
@ResponseBody//提交数据
public String getData(@RequestBody List<UserInfo> userList) {
System.out.println("======" + userList);
return "success!";
}
@ResponseBody:注解用于将Controller的方法返回的对象,通过springmvc提供的HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端
@RequestBody:用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json数据)转换为java对象并绑定到Controller方法的参数上。
可以用应用postman测试提交与接收