3

I am doing an Application with Spring Rest Template for making the Box.net Rest Calls. But I am facing the Problem of Not Uploading the Files through Rest-template. It is giving an Error as "Bad Request 400 Http Status Code".

Here is my sample Code:

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(); 
}

Can anyone tell me the procedure how to upload files from Java Rest Template.

4

2 回答 2

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;
    }

字符串令牌 - 它只是我们其余服务上的安全令牌(作为自定义 HTTP 标头提供)。它可以提供如何在请求中设置“自定义标头”的示例。注意:请注意返回的数据(上传文件后来自网络服务)被解析为 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>
于 2013-05-15T06:45:27.223 回答
0

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

于 2013-03-31T22:31:48.533 回答