springmvc文件上传404、500之类的问题

1.jar包忘记导入或者导入的jar包不全。报啥错自己去查。

2.form表单提交时用的是post,因为get是传输数据量小的,如果你的文件太大,就有可能失败。所以用post好,post封装成报文的形式,对用户不可见,更加安全。会报404的错。

3.form表单的路径出错比如:action="upload.do",但你把它写成action="upload2.do",如果是这个,会报404的错。

4.在spring-mvc.xml里面配置:文件上传不能少

<!-- 文件上传 -->
		<bean id="multipartResolver"
			class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	
			<property name="defaultEncoding" value="utf-8"/>
			<!-- one of the properties available; the maximum file size in bytes -->
			<property name="maxUploadSize" value="10000000"/>
	
		</bean>

特别是上面的value值要注意,3个0是kb,6个0是M,上面的是10M,也就是上传的不能超过10M。不然直接报500这个错。

5.上传时总该有路径,就是上传的地方,必须仔细检查你上传的地方是否有你想要的文件夹,看仔细了。这个报错很明显,直接就是找不到路径.

@RequestMapping("/upload")
	public String upload(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println("wwwwwww11111111"+filePath);
		file1.transferTo(new File((filePath)+"upload/"+file1.getOriginalFilename()));
		return "redirect:success.html";
		
	}

file1.transferTo(new File((filePath)+"upload/"+file1.getOriginalFilename()));这句话就是用来确定你上传文件的地址的,放在那个文件。比如上面的

"upload/"
这个代表一个文件夹,少了就不行。

6.警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spring002' did not find a matching property.

这上面的这个source警告可以不用管。不影响,有的人说把服务器停了(stop),再把项目移除(remove),双击服务器进入服务器配置页面,把server options下的Publish module contexts to separate XML files勾上就可以了。但对有的人不行,那就不管他,不影响的,有警告照样可以跑。

7.大约就这些错误了吧。有问题可以在评论区留言,不要忘记点赞哦



posted @ 2018-03-26 19:56  紫紫幽  阅读(297)  评论(0编辑  收藏  举报