MultiPartEntityBuilder服务器端 - java服务器端(apache tomcat)

时间:2014-02-05 08:42:37

标签: java android apache post httprequest

我有multipartentitybuilder的客户端代码,但我无法找到服务器端的任何地方进行处理。

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来解析此post请求。谢谢!

1 个答案:

答案 0 :(得分: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");
}

}