我的代码节选(Spring 3.2,使用AngularJS上传blueimp文件):
/**
* Handles chunked file upload, when file exceeds defined chunked size.
*
* This method is also called by modern browsers and IE >= 10
*/
@RequestMapping(value = "/content-files/upload/", method = RequestMethod.POST, headers = "content-type!=multipart/form-data")
@ResponseBody
public UploadedFile uploadChunked(
final HttpServletRequest request,
final HttpServletResponse response) {
request.getHeader("content-range");//Content-Range:bytes 737280-819199/845769
request.getHeader("content-length"); //845769
request.getHeader("content-disposition"); // Content-Disposition:attachment; filename="Screenshot%20from%202012-12-19%2017:28:01.png"
request.getInputStream(); //actual content.
//Regex for content range: Pattern.compile("bytes ([0-9]+)-([0-9]+)/([0-9]+)");
//Regex for filename: Pattern.compile("(?<=filename=\").*?(?=\")");
//return whatever you want to json
return new UploadedFile();
}
/**
* Default Multipart file upload. This method should be invoked only by those that do not
* support chunked upload.
*
* If browser supports chunked upload, and file is smaller than chunk, it will invoke
* uploadChunked() method instead.
*
* This is instead a fallback method for IE <=9
*/
@RequestMapping(value = "/content-files/upload/", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
@ResponseBody
public httpentity<UploadedFile> uploadMultipart(
final HttpServletRequest request,
final HttpServletResponse response,
@RequestParam("file") final multipartfile multiPart) {
//handle regular multipartfile
// IE <=9 offers to save file, if it is returned as json, so set content type to plain.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new httpentity<>(new UploadedFile(), headers);
}
这应该使您入门。在IE8,IE9,IE10,Chrome,FF上进行的最低测试。当然可能存在问题,并且可能有一种提取内容范围的简便方法,但是..对我有用。