我有一个简单的形式,同时可以选择上传图片,但上传的文件,我不使用这种方法问题春AJAX文件上传
<form:input path="logoData" id="image" type="file" />
,而不是我使用ajax upload jquery pulgin。 问题是upload.parseRequest(请求)在下面的代码返回null:日志
@RequestMapping(value = "/upload.htm", method = RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request) throws FileUploadException{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List<FileItem> items = upload.parseRequest(request);
System.out.println("====ITEMS====" + items.size());
System.out.println("----REQUEST---" +request.getParameter("uploadImg"));
System.out.println("-----SIZE----" +request.getParameterMap().size());
Map<String, String> map = request.getParameterMap();
for(Map.Entry<String, String> entry : map.entrySet()){
System.out.println("----KEY---" + entry.getKey() + "----value---" + entry.getValue());
}
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("----IS MULTIPART---" +isMultipart);
return "hello";
}
输出是:
==== ITEMS ==== 0
---- REQUEST ---空
----- SIZE ---- 0
---- IS MULTIPART ---真正
我的JavaScript代码:
new AjaxUpload('#upload', {
action : my_url+ 'methodName/upload.htm',
name : 'uploadImg',
autoSubmit : true,
responseType: 'html',
onChange: function(file, extension){ },
onSubmit: function(file, extension) {
},
onComplete: function(file, html) {
alert(file);
alert(html);
}
});
IS MULTIPART显示为真,但如何获取文件名以及如何存储它。我已经尝试了一个没有Ajax的例子,它使用数据类型CommonsMultipartFile工作正常。 另外我已经在PHP中使用了ajaxupload,并且我得到的文件名为$ _FILES ['image'] ['name']但在java中没有任何想法,因为我是java新手。 我在这个网站上关注了我的类似问题,但没有成功。
谢谢。
嘿感谢您的帮助....其实我也曾经同样豆...但我认为@RequestParam只适用于GET请求.. !! – Dharmesh 2011-03-25 09:52:45
请你给出完整的工作代码,因为我挣扎了两个多星期才完成这项工作。 – 2013-02-07 14:13:31
Hi Bozho!你能告诉我如何用RestTemplate测试这个吗?非常感谢。 – Channa 2013-04-09 06:33:01