Java Android-如何将视频/图像上传到PHP服务器

Java Android-如何将视频/图像上传到PHP服务器,java,php,android,web-services,Java,Php,Android,Web Services,我可以使用以下代码将字符串值发布到PHP服务器: public void callWebService(String strEmailList){ HttpResponse response = null; String responseBody=""; List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6); nameValuePairs.add(new

我可以使用以下代码将字符串值发布到PHP服务器:

 public void callWebService(String strEmailList){
    HttpResponse response = null;
    String responseBody="";
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
    nameValuePairs.add(new BasicNameValuePair("stringkey1",
            String_Value1));
    nameValuePairs.add(new BasicNameValuePair("stringkey2", String_Value2));
    nameValuePairs.add(new BasicNameValuePair("stringkey3", String_Value3));
    nameValuePairs.add(new BasicNameValuePair("stringkey4", String_Value4));
    nameValuePairs.add(new BasicNameValuePair("stringkey5", String_Value5));
    nameValuePairs.add(new BasicNameValuePair("stringkey6", Here i need to post Image));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://MY URL");

        if (nameValuePairs != null)
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response = httpclient.execute(httppost);
        responseBody = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    handleResponse(responseBody);
}
public void callWebService(字符串strEmailList){
HttpResponse响应=null;
字符串responseBody=“”;
List nameValuePairs=新的ArrayList(6);
添加(新的BasicNameValuePair(“stringkey1”),
字符串(u值1));
添加(新的BasicNameValuePair(“stringkey2”,String_Value2));
添加(新的BasicNameValuePair(“stringkey3”,String_Value3));
添加(新的BasicNameValuePair(“stringkey4”,String_Value4));
添加(新的BasicNameValuePair(“stringkey5”,String_Value5));
添加(新的BasicNameValuePair(“stringkey6”,这里我需要发布图片));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://MY 网址“);
if(nameValuePairs!=null)
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
responseBody=EntityUtils.toString(response.getEntity());
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
handleResponse(responseBody);
}

如果我只发布字符串值,我会得到
responseBody
。在
nameValuePair
中,我需要将图像发布到服务器。任何人都可以帮助我如何使用以下代码发布图像。

您可以将图像作为多部分实体发送到服务器

public void upload(String filepath) throws IOException
    {
     HttpClient httpclient = new DefaultHttpClient();
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     HttpPost httppost = new HttpPost("url");
     File file = new File(filepath);
     MultipartEntity mpEntity = new MultipartEntity();
     ContentBody cbFile = new FileBody(file, "image/jpeg");
     mpEntity.addPart("userfile", cbFile); 
     httppost.setEntity(mpEntity);
     System.out.println("executing request " + httppost.getRequestLine());
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity resEntity = response.getEntity();
             // check the response and do what is required
      }

您可以将映像作为多部分实体发送到服务器

public void upload(String filepath) throws IOException
    {
     HttpClient httpclient = new DefaultHttpClient();
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     HttpPost httppost = new HttpPost("url");
     File file = new File(filepath);
     MultipartEntity mpEntity = new MultipartEntity();
     ContentBody cbFile = new FileBody(file, "image/jpeg");
     mpEntity.addPart("userfile", cbFile); 
     httppost.setEntity(mpEntity);
     System.out.println("executing request " + httppost.getRequestLine());
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity resEntity = response.getEntity();
             // check the response and do what is required
      }

对于上载图像和视频,,,您需要使用多部分。首先,您需要将文件附加到文件体中,然后再附加到多部分中

public JSONObject file_upload1(String URL, String userid, String topic_id,
        String topicname, String filelist, String taglist,
        String textComment, String textLink) {
    JSONObject jObj = null;
    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);

        FileBody bin = null;
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

            File file = new File(filelist);

            try {
                bin = new FileBody(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
            reqEntity.addPart("post_data" + i, bin);

        reqEntity.addPart("tag", new StringBody("savetopicactivities"));
        reqEntity.addPart("user_id", new StringBody(userid));
        reqEntity.addPart("text", new StringBody(textComment));
        reqEntity.addPart("count",
                new StringBody(String.valueOf(taglist.size())));
        reqEntity.addPart("topic_id", new StringBody(topic_id));
        reqEntity.addPart("topic_name", new StringBody(topicname));
        reqEntity.addPart("link", new StringBody(textLink));

        httpPost.setEntity(reqEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        json = sb.toString();
        System.out.println("json   " + json);
        try {
            jObj = new JSONObject(json);
        } catch (Exception e) {

            e.printStackTrace();
        }

        is.close();

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // return JSON String
    return jObj;
}

对于上载图像和视频,,,您需要使用多部分。首先,您需要将文件附加到文件体中,然后再附加到多部分中

public JSONObject file_upload1(String URL, String userid, String topic_id,
        String topicname, String filelist, String taglist,
        String textComment, String textLink) {
    JSONObject jObj = null;
    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);

        FileBody bin = null;
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

            File file = new File(filelist);

            try {
                bin = new FileBody(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
            reqEntity.addPart("post_data" + i, bin);

        reqEntity.addPart("tag", new StringBody("savetopicactivities"));
        reqEntity.addPart("user_id", new StringBody(userid));
        reqEntity.addPart("text", new StringBody(textComment));
        reqEntity.addPart("count",
                new StringBody(String.valueOf(taglist.size())));
        reqEntity.addPart("topic_id", new StringBody(topic_id));
        reqEntity.addPart("topic_name", new StringBody(topicname));
        reqEntity.addPart("link", new StringBody(textLink));

        httpPost.setEntity(reqEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        json = sb.toString();
        System.out.println("json   " + json);
        try {
            jObj = new JSONObject(json);
        } catch (Exception e) {

            e.printStackTrace();
        }

        is.close();

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // return JSON String
    return jObj;
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
试一试{
MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
对于(int index=0;index
HttpClient-HttpClient=newdefaulthttpclient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
试一试{
MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
对于(int index=0;index
查看此信息,它可能会对您有所帮助。看看这个,它可能对你有帮助。将httpmime-4.2.1.jar添加到项目中将httpmime-4.2.1.jar添加到项目中