2014-02-05 128 views
0

我有这个客户端代码为multipartentitybuilder,但我无法找到处理它的服务器端的任何地方。MultiPartEntityBuilder服务器端 - java服务器端(apache tomcat)

public String multiPartExecute(String url, String keyOfString, String request, String keyForFile, File file) 
    { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(url); 
     MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); 
     multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     multipartEntity.addPart(keyForFile, new FileBody(new File("fileName"))); 
     multipartEntity.addTextBody(keyOfString, request); 
     post.setEntity(multipartEntity.build()); 
     HttpResponse response = null; 
     String line = "", output = ""; 

     try 
     { 
      response = client.execute(post); 
      BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); 

      while ((line = br.readLine()) != null) 
      { 
       output += line; 
      } 
     } 
     catch (ClientProtocolException e) 
     { 
      e.printStackTrace(); 
      return null; 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
      return null; 
     } 

     if (response.getStatusLine().getStatusCode() != 200) 
     { 
      response.getStatusLine().getStatusCode(); 
      return null; 
     } 

     HttpEntity entity = response.getEntity(); 
     try 
     { 
      entity.consumeContent(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     client.getConnectionManager().shutdown(); 

     return output; 
    } 

我在哪里可以找到信息,或可有人建议,如何编写服务器端的servlet来解析这个职位的要求。谢谢!

+0

看一看[ServletFileUpload](https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/util/http/fileupload/servlet /ServletFileUpload.html) – vzamanillo

回答

0

使用此代码从webservice获取文件。

$uploads_dir = '/uploads';foreach ($_FILES["photo"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
    $tmp_name = $_FILES["photo"]["tmp_name"][$key]; 
    $name = $_FILES["photo"]["name"][$key]; 
    move_uploaded_file($tmp_name, "$uploads_dir/$name"); 
} 

}

+0

我需要一个java服务器端(apache tomcat) – vlio20

+0

检查[this](http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/)教程,可能会帮助你。 – Cheerag

+0

我正在使用MultiPartEntityBuilder,所以这个链接不幸帮不了我,谢谢! – vlio20