【问题标题】:SpringMVC-FileUpload - The request sent by the client was syntactically incorrectSpringMVC-FileUpload - 客户端发送的请求语法错误
【发布时间】:2013-12-09 08:31:50
【问题描述】:

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

我正在开发 POC 并点击下面的链接。 http://spring.io/guides/gs/uploading-files/

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

控制器文件

@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)。

服务客户端

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 端点。每次,我都会遇到以下异常。

400 Bad Request - 客户端发送的请求语法错误

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

我知道我在这篇文章中很冗长。我想提供尽可能多的上下文信息。请帮忙。

谢谢

【问题讨论】:

  • 尝试从您的控制器中删除 @ResponseBody 并查看。
  • @aman 这不会影响请求进来。
  • @Aman - 感谢您的回复。但这没有帮助。刚才,我试过了
  • 请为org.springframework.web 包设置日志并检查输出。您使用的是 Spring 3.2+ 吗?
  • 不,我没有使用 3.2+ 版本。我正在使用 3.0.5 是的,我启用了日志,这有帮助。我担心我的代码有问题。谢谢

标签: java spring rest spring-mvc


【解决方案1】:

你需要做两件事:

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

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

@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 official documentation 中有进一步的记录。

【讨论】:

  • 有趣,MultipartConfigElement(在教程中创建为 bean 引用的类)应该做什么?看起来我需要一些茶点:-)
  • @Boris 请参阅我链接的文档的第 17.10.3 节。它用于配置在 Servlet 3.0 环境中如何解析多部分请求。我还没有使用它。在我的下一个应用中...
  • 非常感谢 Sotirios。我一无所知。现在一切正常。我刚刚在 multipartResolver 中添加了 。否则,图像文件不会被上传。我需要在这些方面花费大量时间。再次感谢。
  • @user19 不客气。 Spring 文档非常详细,如果您有时间阅读它。祝你好运!
猜你喜欢
  • 2017-01-22
  • 2012-06-18
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
  • 2015-08-10
  • 1970-01-01
相关资源
最近更新 更多