2013-03-31 60 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>