2013-03-31 57 views
3

我正在做一个应用程序与春天休息模板,使Box.net休息电话。 但我面临着不通过Rest-template上传文件的问题。它发出一个错误为“错误请求400 Http状态码”。关于通过Java上传文件休息模板

这里是我的示例代码:

public static Object uploadfile(){ 

    String url="https://upload.box.com/api/2.0/files/content"; 
    String file_url="SOME URL or Local File System Path"; 

    File tmpFile = new File(file_url); 
    boxMap.add("filename",tmpFile); 
    boxMap.add("parent_id",747312798); 

    ResponseEntity<Object> response = restTemplate.exchange(url, 
    HttpMethod.POST, 
    new HttpEntity(boxMap,getHeaders()), 
    Object.class); 
    System.out.println("uploaded file info: "+response.getBody()); 
    return response.getBody(); 
} 

谁能告诉我的程序如何从Java休息模板上传文件。

回答

0

文件上传需要作为多部分POST执行。有关如何在Java中执行此操作的说明,请参阅:How can I make a multipart/form-data POST request using Java?

+0

嗨Seanrose,我通过你的链接过去了,我的文件,成功上传至Box.net。但是,我有一个使用文件Web URL(例如http://example.com/sample.txt)上传文件的场景。 是否Box支持这些类型的文件上传?或者它只支持本地文件。 – Rajkumar

+0

目前只有档案。 – seanrose

0

我已使用restTemplate解决了此问题。
请,看到一些代码示例:

public String uploadPhoto(File file, String token) throws ClientRequestException { 
    try { 
     MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>(); 
     UrlResource urlr = new UrlResource("file:" + file.getAbsolutePath()); 
     form.add("attachment", urlr); 
     WsUrl wsUrl = requestForObjectMultipart("/uploadProfilePhoto.json", form, WsUrl.class, token); 
     return wsUrl.getUrl(); 
    } catch (MalformedURLException e) { 
     throw new ClientRequestException("Something went wrong with file upload"); 
    } 
} 


protected <T extends ErrorAware> T requestForObjectMultipart(String methodUrl, Object r, Class<T> c, String token) throws ClientRequestException{ 
     HttpHeaders headers = new HttpHeaders(); 
     headers.add(SECURITY_TOKEN,token); 
     //Need to set content type here to avoid convertion with Jackson message converter 
     headers.add("Content-Type", "multipart/form-data"); 
     return requestForObjectWithHeaders(methodUrl, r, c, HttpMethod.POST, headers); 
    } 

protected <T extends ErrorAware> T requestForObjectWithHeaders(String methodUrl, Object r, Class<T> c, HttpMethod method, HttpHeaders headers) throws ClientRequestException{ 
     T result = restTemplate.exchange(getBaseUrl() + getApiUrlPref() + methodUrl, method, new HttpEntity<Object>(r,headers), c).getBody(); 
     if(result.hasError()) 
      throw new ClientRequestException(result.getError()); 
     return result; 
    } 

字符串标记 - 它只是安全令牌(提供ascustom HTTP标头)在我们休息的服务。它可以举例说明如何在请求中设置“自定义标题”。 注意:注意返回的数据(从上传文件后的web服务)被解析为JSON对象。 如果你不想要这个 - 你可以简单地忽略restTemplate.exchange()方法的结果。在Spring配置

我restTemplate初始化:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> 
     <property name="messageConverters"> 
      <list> 
       <ref bean="jsonConverter"/> 
       <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> 
      </list> 
     </property> 
     ... 
    </bean> 
<!-- To enable @RequestMapping process on type level and method level --> 
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <list> 
       <ref bean="jsonConverter"/> 
      </list> 

     </property> 
    </bean> 

    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
     <property name="supportedMediaTypes" value="application/json"/> 
    </bean>