我正在使用Apache CXF RESTful webservice上传文件,同时发送JSON数据,如下所示:
public class DownLoadImageAsyncTask extends AsyncTask{
@Override
protected void onPreExecute() {
progressDialog=new ProgressDialog(TwitterIntegration.this);
progressDialog.setCancelable(false);
progressDialog.setMessage(getString(R.string.please_wait));
progressDialog.setIndeterminate(true);
if(Validator.isNotNull(preferences.getImagePath())&& !preferences.getImagePath().isEmpty()){
preferences.getImagePath().clear();
}
filePath=preferences.getImagePath();
}
@Override
protected Object doInBackground(Object[] params) {
File file=new File(Environment.getExternalStorageDirectory(),"/HealthWel");
if(file.exists()==true){
file.delete();
}
file.mkdir();
for (int i=0;i<mURLs.size();i++){
File f=new File(file+"/"+i+".jpg");
if(f.exists()==true){
f.delete();
}
if(f.exists()==false){
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(mURLs.get(i));
try {
HttpResponse httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity httpEntity=httpResponse.getEntity();
InputStream is=httpEntity.getContent();
Boolean status=f.createNewFile();
FileOutputStream fileOutputStream=new FileOutputStream(f);
byte[]buffer=new byte[1024];
long total=0;
int count;
while ((count=is.read(buffer))!=-1){
total+=count;
fileOutputStream.write(buffer,0,count);
}
if(!downLoad) {
if (Validator.isNotNull(preferences.getImagePath()) && !preferences.getImagePath().isEmpty()) {
preferences.getImagePath().clear();
}
}
filePath.add(f.getPath());
fileOutputStream.close();
is.close();
runOnUiThread(new Runnable() {
public void run() {
// runs on UI thread
progressDialog.show();
}
});
}
else {
finish();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Object o) {
preferences.setImagePath(filePath);
dismissProgressDialog();
shareImage();
}
}
private void showProgressDialog() {
if(!isFinishing() && progressDialog==null) {
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.show();
}
}
/**
* dismiss Progress Dialog.
*/
private void dismissProgressDialog() {
if (!isFinishing() &&progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog=null;
}
}
我正在使用REST客户端发送JSON数据和文件。 当我发送请求时,我得到了foll错误:
@POST
@Path( "/upload/doc" )
@Consumes( { MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
@Produces( MediaType.APPLICATION_JSON )
public Response uploadRateSheet( @Multipart( value = "file" ) Attachment attachment, DocumentApiModel doc)
{
...
}
请帮忙......
谢谢。
答案 0 :(得分:2)
因此更新了多个部分的休息定义。
因此设置内容类型。
通过以上两项更改,您的REST方法声明将如下所示。
@POST
@Path( "/upload/doc" )
@Consumes( { MediaType.MULTIPART_FORM_DATA})
@Produces( MediaType.APPLICATION_JSON )
public Response uploadRateSheet(
@Multipart( value = "file",type="application/octet-stream") Attachment attachment, //Note set content type accordinly such that if any available providers for that content type is present it can resolve your attachment implicitly to avoid writing custom code.
@Multipart( value = "docModel",type="application/json") DocumentApiModel doc);
接下来请确保您已启用Jettission或Jackson等JSON提供程序
例如Jackson Json提供者
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"/>
</jaxrs:providers>
最后测试,请注意你必须使用文件设置这两个部分,当你选择String时,似乎rest客户端没有在Content-Disposition中设置内容类型。
或者,您可以使用CXF Webclient进行测试。
@Test
public void testMultipart() throws FileNotFoundException{
WebClient client = WebClient.create("<your service url>", Arrays.asList(new JacksonJaxbJsonProvider()));
client.type("multipart/form-data");
final Attachment attachment = new Attachment("file", new FileInputStream("<your file>"), new ContentDisposition(("attachment;filename=filename")));
final Attachment attachment1 = new Attachment("docModel","application/json",new DocumentApiModel());
client.post(new MultipartBody(Arrays.asList(attachment, attachment1)));
}