SpringMVC-FileUpload - 客户端发送的请求在语法上是不正确的 [英] SpringMVC-FileUpload - The request sent by the client was syntactically incorrect

查看:655
本文介绍了SpringMVC-FileUpload - 客户端发送的请求在语法上是不正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个主题上看过几个qts。但是我没有找到任何关于这个错误的线索。

I've seen couple of qts on the same topic. But I didn't find any clue of this error.

我正在研究POC并按照下面的链接。
http://spring.io/guides/gs/uploading-files/

I am working on a POC and following the link below. http://spring.io/guides/gs/uploading-files/

正如上面的教程中所提到的,在独立模式[spring embeded Tomcat]中,它工作得非常好。
但我想将其部署为webapplication。所以,我创建了一个单独的SpringMVC项目并添加了以下控制器。

As mentioned in the above tutorial, in standalone mode[spring embeded Tomcat] it is working absolutely fine. But I want to deploy it as webapplication. So, I have created a separate SpringMVC project and added the following controller.

控制器文件

@Controller
public class FileUploadController {

    @RequestMapping(value="/upload", method=RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name, 
            @RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = 
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

}

我'我写了以下客户端(因为我不想在这里使用RestTemplate)。

I've written the following client ( As I don't want to use RestTemplate here).

服务客户端

private static final String URL_GET = "http://localhost:8080/SpringMVC/upload";
static String URL = "http://localhost:8080/SpringMVC/upload";

public static void main(String[] args) throws Exception {
    PropertyConfigurator.configure("C:/DevEnvProject/eclipse/workspace_exp/OCR/log4j.properties");
    testGet();
    testPOST();
}

private static void testGet() throws ClientProtocolException, IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(URL_GET);
    HttpResponse response = httpClient.execute(httpGet, localContext);

    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String sResponse = reader.readLine();

} 

static void testPOST() {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(URL);

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        entity.addPart("name", new StringBody("testIcon.png"));
        entity.addPart("file", new FileBody(new File("C:/testIcon.png")));
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost, localContext);


        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse = reader.readLine();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我无法成功拨打POST端点。每次,我都会遇到以下异常。

I couldn't make a successful call to the POST endpoint. Everytime, I'm getting the following exception.

400错误请求 - 客户发送的请求在语法上不正确

'GET'电话工作正常。我将'POST'请求的日志与我在使用Spring教程中提到的独立方法测试时获得的相同'POST'请求进行了比较。在请求部分没有找到任何差异。

'GET' call is working fine. I compared the the log of the 'POST' request with the same 'POST' request which I got while testing with standalone approach as mentioned in the spring tutorial. Didn't find any diff in the request part.

我知道在这篇文章中我很啰嗦。我想尽可能多地提供上下文信息。请帮助。

I know that I'm quite verbose in this post. I wanted to give as much context info as possible. Please help.

谢谢

推荐答案

您需要2件事要做的事情:

There are 2 things you need to do:

首先,将Apache Commons FileUpload库添加到类路径中。如果您使用maven,则可以获得此处的依赖关系。如果你不这样做,你仍然可以下载jar并手动添加它。

First, add the Apache Commons FileUpload library to your class path. If you use maven, you can get the dependency here. If you don't, you can still download the jar and add it manually.

其次,你必须声明一个 MultipartResolver bean在您的上下文中,名称为 multipartResolver 。使用Apache Commonds FileUpload,您可以使用 CommonsMultipartResolver 。例如,使用Java配置,那将是

Second, you have to declare a MultipartResolver bean in your context with name multipartResolver. With Apache Commonds FileUpload, you can use CommonsMultipartResolver. For example, with Java config, that would be

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); 
    // set any fields
    return commonsMultipartResolver; 
}

使用XML配置,

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- set any properties -->
</bean>

Spring官方文档

这篇关于SpringMVC-FileUpload - 客户端发送的请求在语法上是不正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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