使用spring上传文件,文件太大时,出现如下错误:
the request was rejected because its size (104857000) exceeds the configured maximum (1024000)
解决方法:
spring-contxt.xml 修改maxUploadSize的值
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="1024000"/>
</bean>
测试代码
@Controller
public class Upload {
@RequestMapping(value = "uploadToFile", method = RequestMethod.POST)
@ResponseBody
public void uploadToUser(@RequestParam("file") MultipartFile file) {
long start = new Date().getTime();
DateFormat df = new SimpleDateFormat("YY-MM-dd HH:mm:ss");
String filePath = "G:\\testfile\\" + fileName;
File f = new File(filePath);
if (!f.exists()) {
f.mkdirs();// 不存在路径则进行创建
}
//保存文件
file.transferTo(f);
long end = new Date().getTime();
System.out.println("文件上传耗时:" + (end - start));
} catch (IOException e) {
return "文件上传失败";
}
}