如何在服务器上传图像?

时间:2015-03-10 10:59:16

标签: android server image-uploading image-upload

我在IIS上创建了文件夹,这是其中的网址:

http://xxx.xxx.xx.120/checkin/Documents/

我正在尝试在我的.NET服务器上传图像,但遗憾的是没有成功。我不知道哪里出错了,它说文件已上传,但当我在服务器上查看时,它会显示空文件夹。这是我的代码

 class ImageUploadTask extends AsyncTask<Void, Void, String> {
  private String webAddressToPost = "http://xxx.xxx.xx.120/checkin/Documents/";

  // private ProgressDialog dialog;
  private ProgressDialog dialog = new ProgressDialog(ImageGalleryDemoActivity.this);

  @Override
  protected void onPreExecute() {
   dialog.setMessage("Uploading...");
   dialog.show();
  }

  @Override
  protected String doInBackground(Void... params) {
   try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(webAddressToPost);

    MultipartEntity entity = new MultipartEntity(
      HttpMultipartMode.BROWSER_COMPATIBLE);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 100, bos);
    byte[] data = bos.toByteArray();
    String file = Base64.encodeBytes(data);
    entity.addPart("uploaded", new StringBody(file));

    entity.addPart("someOtherStringToSend", new StringBody("your string here"));

    httpPost.setEntity(entity);
    HttpResponse response = httpClient.execute(httpPost,
      localContext);
    BufferedReader reader = new BufferedReader(
      new InputStreamReader(
        response.getEntity().getContent(), "UTF-8"));

    String sResponse = reader.toString();
    System.out.println("PPPPPPPPPPPPP>>>>>>>>>> " + sResponse);
    return sResponse;
   } catch (Exception e) {
    // something went wrong. connection with the server error
       System.out.println(">>>?????? " + e.getMessage());
   }
   return null;
  }

  @Override
  protected void onPostExecute(String result) {
   dialog.dismiss();
   Toast.makeText(getApplicationContext(), "file uploaded",
     Toast.LENGTH_LONG).show();
   new DownloadImage().execute("http://xxx.xxx.xx.120/checkin/Documents/abc.jpg");
  }

 }

1 个答案:

答案 0 :(得分:0)

你可以使用Square的OkHttp。这是一个非常好的图书馆。

https://github.com/square/okhttp/wiki/Recipes