【问题标题】:RoboSpice File Upload with Spring on Android在 Android 上使用 Spring 上传 RoboSpice 文件
【发布时间】:2013-04-03 09:42:54
【问题描述】:

我一直在尝试了解如何使用带有 Spring 的 RoboSpice 将文件上传到我的服务器,但我找不到有效的示例。从我找到的一个例子中,我构建了:

   class UploadJsonRequest extends SpringAndroidSpiceRequest<APIResponseUpload> {

          public UploadJsonRequest() {
                super( APIResponseUpload.class );
            }

            @Override
            public APIResponseUpload loadDataFromNetwork() throws Exception {

                MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
                for(org.calflora.observer.model.Attachment a : o.attachments){

                    parts.add(a.name, new FileSystemResource(a.localPath));

                }
                //parts.add("record", base);
                return getRestTemplate().postForObject(URI, parts, APIResponseUpload.class);
            } 
     }
    return new UploadJsonRequest();

但是,这给了我错误:

Caused by: org.springframework.web.client.HttpClientErrorException: 404 err: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/json;charset=UTF-8

好的,所以这似乎表明我需要做一些额外的事情来表明数据应该使用 multipart/form-data 传输。如果这是正确的,这是如何完成的?如果这不正确,那么规范的方法是什么,因为这显然是一种常见的需求?

【问题讨论】:

  • 你看到这个代码了吗? stackoverflow.com/q/15769019/321354
  • 是的。您会注意到该代码与我的代码几乎相同。我尝试使用它,但它包含对“UploadRequestModel”的引用,该引用没有得到解释,而且我安装的任何 jar 中似乎都不存在 - 所以显然它不会构建。你能给我一些见解吗?

标签: android spring rest upload robospice


【解决方案1】:

就我而言,以下解决了这个问题

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(parts, requestHeaders);

RestTemplate restTemplate = getRestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

return restTemplate.postForObject(URI, requestEntity, APIResponseUpload.class);

但是,根据 Spring 的文档,这不应该手动设置消息转换器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-21
    • 2013-08-08
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多