使用多部分输入将图像从 SD 卡发送到服务器

发布于 2024-12-06 01:58:53 字数 131 浏览 3 评论 0原文

我正在制作一个Android应用程序,其中我通过多部分请求将图像从/mnt/sdcard/DCIM/Camera/IMG_20110922_124932.jpg发送到服务器到服务器..任何人都可以帮我...... 任何帮助将不胜感激.. 谢谢..

I am making an android app in which i am sending image to server from /mnt/sdcard/DCIM/Camera/IMG_20110922_124932.jpg to serverthrough multipart request .. Can anybody help me out....
Any help will be appreciated..
Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-12-13 01:58:53

这是我用来将图像发送到服务器的多部分帖子的简单示例:

public void MultipartPost(){
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(url);
//Set Credentials
    String auth = User + ":" + Pass;
    byte[] bytes = auth.getBytes();
    postRequest.setHeader("Authorization", "Basic " + new String(Base64.encodeBytes(bytes)));


    try {


        MultipartEntity mpC = new MultipartEntity();

        //Create stringbody for the filename
    StringBody sbPicID = new StringBody("123.jpg");

        //get a file reference from the image on the SD card
    File fle = new File("full path to the file");

        //create a filebody from the file
    FileBody fb = new FileBody(fle);

    //Add the file name and filebody to the Multipart Entitiy
    mpC.addPart("myImage", sbPicID);
    mpC.addPart("myImage", fb);

       //Set the entitiy of the post request to your Multipart
        postRequest.setEntity(mpC);

       HttpResponse res;

   //execute the post request
       Log.d(TAG,"Starting Send...");
   res = httpClient.execute(postRequest);
   Log.d(TAG, res.getStatusLine().getReasonPhrase());
   Log.d(TAG, res.getStatusLine().getStatusCode());
   res.getEntity().getContent().close();
   Log.d(TAG,"After Close");

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

    }

Here is a simple example of a multipart post that I use to send images to a server:

public void MultipartPost(){
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(url);
//Set Credentials
    String auth = User + ":" + Pass;
    byte[] bytes = auth.getBytes();
    postRequest.setHeader("Authorization", "Basic " + new String(Base64.encodeBytes(bytes)));


    try {


        MultipartEntity mpC = new MultipartEntity();

        //Create stringbody for the filename
    StringBody sbPicID = new StringBody("123.jpg");

        //get a file reference from the image on the SD card
    File fle = new File("full path to the file");

        //create a filebody from the file
    FileBody fb = new FileBody(fle);

    //Add the file name and filebody to the Multipart Entitiy
    mpC.addPart("myImage", sbPicID);
    mpC.addPart("myImage", fb);

       //Set the entitiy of the post request to your Multipart
        postRequest.setEntity(mpC);

       HttpResponse res;

   //execute the post request
       Log.d(TAG,"Starting Send...");
   res = httpClient.execute(postRequest);
   Log.d(TAG, res.getStatusLine().getReasonPhrase());
   Log.d(TAG, res.getStatusLine().getStatusCode());
   res.getEntity().getContent().close();
   Log.d(TAG,"After Close");

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

    }
甩你一脸翔 2024-12-13 01:58:53

请参阅我上周回答的[此问题][1] 已接受的答案。它与上面的答案非常相似,但还包含一些用于获取图像的示例 PHP 代码。

JSON 并将图像上传到服务器

See the accepted answer of [this question here][1] that I answered last week. It's very similar to the answer above but also includes some sample PHP code to get the image.

JSON and upload image to server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文