关于通过JavaREST模板上传文件
我正在用SpringREST模板做一个应用程序,用于进行Box.NETREST调用。 但我面临的问题是无法通过Rest模板上传文件。它给出的错误为“错误请求400 Http状态码” 以下是我的示例代码:关于通过JavaREST模板上传文件,java,box-api,resttemplate,Java,Box Api,Resttemplate,我正在用SpringREST模板做一个应用程序,用于进行Box.NETREST调用。 但我面临的问题是无法通过Rest模板上传文件。它给出的错误为“错误请求400 Http状态码” 以下是我的示例代码: public static Object uploadfile(){ String url="https://upload.box.com/api/2.0/files/content"; String file_url="SOME URL or Local File Sys
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();
}
publicstaticobjectuploadfile(){
字符串url=”https://upload.box.com/api/2.0/files/content";
String file_url=“某个url或本地文件系统路径”;
文件tmpFile=新文件(文件url);
添加(“文件名”,tmpFile);
添加(“父项id”,747312798);
ResponseEntity response=restemplate.exchange(url,
HttpMethod.POST,
新的HttpEntity(boxMap,getHeaders()),
对象(类);
System.out.println(“上传的文件信息:+response.getBody());
返回response.getBody();
}
谁能告诉我如何从Java Rest模板上载文件的过程。文件上载需要作为多部分帖子执行。关于如何在Java中实现这一点的说明可以在这里找到:我已经用restTemplate解决了这个问题。
请参见一些代码示例:
publicstringuploadphoto(文件、字符串标记)抛出ClientRequestException{
试一试{
MultiValueMap form=新链接的MultiValueMap();
URLROURCE urlr=新的URLROURCE(“文件:“+file.getAbsolutePath());
表格。添加(“附件”,urlr);
WsUrl WsUrl=requestForObjectMultipart(“/uploadProfilePhoto.json”,表单,WsUrl.class,令牌);
返回wsUrl.getUrl();
}捕获(格式错误){
抛出新的ClientRequestException(“文件上传出错”);
}
}
受保护的T requestForObjectMultipart(字符串方法URL、对象r、类c、字符串标记)引发ClientRequestException{
HttpHeaders=新的HttpHeaders();
添加(安全令牌、令牌);
//需要在此处设置内容类型,以避免使用Jackson消息转换器进行转换
添加(“内容类型”、“多部分/表单数据”);
返回requestForObjectWithHeaders(methodUrl、r、c、HttpMethod.POST、headers);
}
受保护的T requestForObjectWithHeaders(字符串方法URL、对象r、类c、HttpMethod方法、HttpHeaders标头)引发ClientRequestException{
T result=restTemplate.exchange(getBaseUrl()+getApiUrlPref()+methodUrl,方法,新的HttpEntity(r,headers),c).getBody();
if(result.hasError())
抛出新的ClientRequestException(result.getError());
返回结果;
}
字符串令牌-它是rest服务上唯一的安全令牌(由自定义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>
...
Hi Seanrose,我查看了您的链接,并成功地将文件上载到Box.net。但是,我有一个使用文件web URL(例如:)上传文件的场景。Box是否支持这些类型的文件上载?或者它只支持本地文件。
<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>