上传使用Spring Framework和jQuery上传文件的插件问题的文件 [英] Uploading files using the Spring Framework and jquery-upload-file plugin issue

查看:123
本文介绍了上传使用Spring Framework和jQuery上传文件的插件问题的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过有麻烦上传文件的 AJAX 从我的Web客户端给我的服务器。我使用下面的的jQuery 的库在客户端做文件上传:的 https://github.com/hayageek/jquery-upload-file

I am having trouble uploading files via AJAX from my web-client to my Server. I am using the following jQuery library in the client-side to do the file upload: https://github.com/hayageek/jquery-upload-file

在服务器端,我使用的 Spring Framework的的,我都遵循以下的的教程建立我的服务:的 https://spring.io/guides/gs/uploading-files/

In the server-side, I'm using Spring Framework and I have followed the following Spring Tutorial to build my Service: https://spring.io/guides/gs/uploading-files/

起初,我的服务器的方法看起来像这样(的文件的定义为 @RequestParam ):

At first, my server method looked like this (file was defined as @RequestParam):

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file){
    //functionality here
}

但每次我提出我有一个错误的请求从服务器信息上传表单,我的 handleFileUpload()方法时,从未被称为

but every time I submitted the Upload form I got a Bad Request message from the Server, and my handleFileUpload() method was never called.

在这之后,我才意识到该文件在送交作为请求参数,所以我定义的文件 @RequestBody ,现在我的方法看起来是这样的:

After that, I realized the file was not being sent as a Request Parameter so I defined file as @RequestBody, and now my method looks like this:

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestBody("file") MultipartFile file){
    //functionality here
}

现在 handleFileUpload()被称为每次上传表单提交的时间,但我得到一个 NullPointerException异常每一次,我要处理的文件的。

Now handleFileUpload() is called every time the Upload form is submitted, but I am getting a NullPointerException every time I want to manipulate file.

我想避免提交默认的形式,我只是想通过AJAX做直接到服务器。有谁知道什么可以发生在这里?

I want to avoid submitting the form by default, I just want to do it through AJAX straight to the Server. Does anybody know what could be happening here?

推荐答案

您可以尝试改变方法的签名,以

you may try changing the signature of the method to

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(MultipartHttpServletRequest request){
    Iterator<String> iterator = request.getFileNames();
    while (iterator.hasNext()) {
        String fileName = iterator.next();
        MultipartFile multipartFile = request.getFile(fileName);
        byte[] file = multipartFile.getBytes();
        ...
    }
    ...
}

这工作与jQuery文件上传在我们的web应用。如果由于某种原因,这不 你通过检查HTTP无法正常工作,您可以尝试找出问题, 通过了jQuery文件上载发出(例如,使用Fiddler)请求,和调试从春季开始的响应 DispatcherServlet的。

this works with jQuery File Upload in our webapp. If for some reason this does not work for you, you may try to isolate the problem, by inspecting the HTTP request issued by the jQuery File Upload (for example, with Fiddler), and debugging the response starting from Spring DispatcherServlet.

这篇关于上传使用Spring Framework和jQuery上传文件的插件问题的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆