Spring MVC的使用Ajax文件上传和MultipartFile [英] Spring MVC with ajax file upload and MultipartFile

查看:983
本文介绍了Spring MVC的使用Ajax文件上传和MultipartFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Ajax的上传与Spring MVC的3个问题。我明白我必须配置multipartResolver豆在Spring配置,这是我所做的一切。比我能有控制器,这样

I have an issue using Ajax upload with Spring 3 MVC. I understand that I have to configure multipartResolver bean in spring config, which I've done. Than I can have controller like this

@RequestMapping(value ="/settingsSim")
@ResponseBody
public Map uploadSimSettings(@RequestParam(value="qqfile", required=true) MultipartFile settings) {
 Map<String, Object> ret = new HashMap<String, Object>();
 return ret;
}

现在的问题是,当我实际发送请求到服务器(实际上valums阿贾克斯文件上载做到这一点对我来说),我得到一个内部服务器错误响应,并没有显示在日志中。我真的抓我的头现在,因为我想不通的问题。

The problem is that when I actually send the request to the server (actually valums Ajax file upload does this for me), I get an Internal server error response and nothing is shown in the logs. I am really scratching my head now, as I cannot figure out the problem.

推荐答案

在使用 valums 插件我用 @RequestBody 春季标注解决了这个问题。 你可以重写你的code如下:

When using valums plugin I solved this problem by using @RequestBody Spring annotation. You could rewrite your code as follows:

@RequestMapping(value ="/settingsSim",method=RequestMethod.POST)
@ResponseBody
public Map uploadSimSettings(@RequestBody String body) {
 /*
 some controller logic 
 */
}

注意变量将包含上传文件的内容。也有在你的例子中,没有方法声明,这意味着你的方法将被映射到GET请求。

Note that the variable body will contain the contents of the uploaded file. Also there is no method declaration in your example which means that your method will be mapped to GET request.

P.S。我解析与Apache共享请求时,也有过这种无边界多部分问题。 HttpServletRequest的#的getParts()返回只是一个空的集合。

P.S. I also had this "no multipart boundary" problem when parsing request with Apache Commons. HttpServletRequest#getParts() returns just an empty collection.

这篇关于Spring MVC的使用Ajax文件上传和MultipartFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆