I have a jsp file in which i am uploading a file using ajax file upload method. For backend handling of file i made a contoller in spring. But i could not find that how can i handle file in spring 2.5 in this condition ? My Code is -
我有一個jsp文件,我使用ajax文件上傳方法上傳文件。對於文件的后端處理我在春天制作了一個控制器。但我無法找到在這種情況下如何處理彈簧2.5中的文件?我的代碼是 -
JSP FILE
function saveMedia() {
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
console.log("form data " + formData);
$.ajax({
url : 'ajaxSaveMedia.do',
data : formData,
processData : false,
contentType : false,
type : 'POST',
success : function(data) {
alert(data);
},
error : function(err) {
alert(err);
}
});
}
1 个解决方案
#1
4
There are two main steps:
主要有兩個步驟:
1) add an instance of multipart resolver to the Spring context
1)將一個多部分解析器的實例添加到Spring上下文中
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
2) add a handler method
2)添加一個處理程序方法
// I assume that your controller is annotated with /ajaxSaveMedia.do
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody String doUpload(@RequestParam("file") MultipartFile multipartFile) {
return "Uploaded: " + multipartFile.getSize() + " bytes";
}
要從org.springframework.web.multipart.MultipartFile獲取java.io.File的實例:
File file = new File("my-file.txt");
multipartFile.transferTo(file);