我正在使用 plupload (plupload.com) jQuery 插件将图像文件 AJAX 发送到 Java Spring 服务器。我尝试了服务器端 RESTful Controller 端点的不同实现。我已附上处理文件上传网址的具体方法。任何帮助将不胜感激。谢谢。
@RequestMapping(value = "/pictureUpload", method = RequestMethod.POST )
public @ResponseBody
String productPictureUploadPost(@RequestBody MultipartFile multipartFile) {
HomeController.logger.info("In method productPictureUploadPost in SettingsPanelController. : Entering");
String orgName = multipartFile.getOriginalFilename();
String filePath = "/my_uploads/" + orgName;
File dest = new File(filePath);
try {
multipartFile.transferTo(dest);
} catch (IllegalStateException e) {
e.printStackTrace();
return "File uploaded failed:" + orgName;
} catch (IOException e) {
e.printStackTrace();
return "File uploaded failed:" + orgName;
}
HomeController.logger.info("In method productPictureUploadPost in SettingsPanelController. Exiting : " + "File uploaded:" + orgName);
return "File uploaded:" + orgName;
}
我还附加了 servlet .xml 多部分解析器声明。
<bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
在客户端,我有一个名为的插件文件,如下面所附。
$(document).ready(function() {
$("#uploader").plupload({
// General settings
runtimes: 'html5,flash,silverlight,html4',
url: "/pictureUpload",
// Maximum file size
max_file_size: '1000mb',
// User can upload no more then 20 files in one go (sets multiple_queues to false)
max_file_count: 3,
// Specify what files to browse for
filters: [
{ title: "Image files", extensions: "jpg,jpeg,gif,png" }
],
// Rename files by clicking on their titles
rename: true,
// Sort files
sortable: true,
// Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
dragdrop: true,
// Views to activate
views: {
list: true,
thumbs: true, // Show thumbs
active: 'thumbs'
},
// Flash settings
flash_swf_url : 'http://rawgithub.com/moxiecode/moxie/master/bin/flash/Moxie.cdn.swf',
// Silverlight settings
silverlight_xap_url : 'http://rawgithub.com/moxiecode/moxie/master/bin/silverlight/Moxie.cdn.xap'
});
});
最佳答案
在文件上传的上下文中,net::ERR_CONNECTION_ABORTED
当 HTTP 服务器未完全读取客户端的 HTTP 请求正文并在上传过程中中止连接时会发生这种情况。当上传的文件太大时,通常会发生这种情况,服务器继续读取请求并提前失败是没有意义的。
中止连接意味着客户端在收到响应之前不会浪费带宽上传文件,但会触发上述错误。
HTTP 有一个提前终止连接的规定,Expect: 100-continue
请求 header 和 100 Continue
响应状态,您可以在此处阅读:http://benramsey.com/blog/2008/04/http-status-100-continue/
遗憾的是,大多数浏览器不会在文件上传期间发送它 ( Which browsers send the expect: 100-continue header? )。
但是由于您在客户端使用 Flash/Silverlight 进行上传,我建议您考虑让您的上传小部件发送 Expect: 100-continue
到服务器。
关于java - 将文件上传到 Spring 时出现 net::ERR_CONNECTION_ABORTED 的可能原因是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277590/