[英]problem with spring ajax file upload
我有一个简单的表单,可以选择上传图片,但是上传文件,我没有使用此方法
<form:input path="logoData" id="image" type="file" />
相反,我正在使用ajax上传jquery pulgin 。 问题是upload.parseRequest(request)在以下代码中返回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";
}
日志输出为:
==== ====项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显示为true,但如何获取文件名以及如何存储它 。 我尝试了一个没有ajax的示例,并且使用数据类型CommonsMultipartFile可以正常工作。 另外,我在PHP中使用了ajaxupload,并且将文件名命名为$ _FILES ['image'] ['name'],但在Java中却不知道,因为我是Java新手。 我已经在这个网站上追踪了类似的问题,但是没有成功。
谢谢。
您可以将其缩短。 您需要一个multipart-resolver:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
接着:
@Controller
public class FileUpoadController {
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:uploadSuccess";
} else {
return "redirect:uploadFailure";
}
}
}
@RequestMapping(value = "/imageUpload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
System.out.println("File name:"+ file.getOriginalFilename());
//byte[] bytes = file.getBytes();
System.out.println("Content type:"+ file.getContentType());
String [] contentType = file.getContentType().split("/");
String fileType = contentType[contentType.length-1];
System.out.println("File type:"+ fileType);
System.out.println("File size:"+ (file.getSize()/1024) + "KB");
String path = context.getRealPath( "/resources/images/");
System.out.println("Path: " + path);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream = new FileOutputStream(path + file.getOriginalFilename());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int readBytes = 0;
byte[] buffer = new byte[8192];
try {
while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// store the bytes somewhere
return "theme";
}
else
return "uploadError";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.