【问题标题】:How to upload image on server? [duplicate]如何上传图片到服务器? [复制]
【发布时间】:2015-05-11 18:53:19
【问题描述】:

我已经在我的 IIS 上创建了文件夹,这是它的 url:

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

我正在尝试在我的 .NET 服务器上上传图片,但遗憾的是没有成功。我不知道出了什么问题,它说 File Uploaded 但是当我检查服务器时它显示 empty folder。这是我的代码

 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");
  }

 }

【问题讨论】:

标签: android server image-uploading image-upload


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多