使用Httpurlconnection上传加速图像

时间:2016-07-28 10:57:51

标签: java android httpurlconnection

有没有办法加快将图片上传到网络服务器的过程。我正在开发的应用程序上传图片需要很长时间。我的代码有效,我知道我能够成功将图像上传到服务器。 我的代码是基于我在这里找到的教程。

  public String uploadFile(String apiPath, String filePath, String type)
  {
    String path = "";
    String result = "";

    switch (type)
    {
      case "M":
        path = "Merchant/" + apiPath;
        break;

      case "C":
        path = "Customer/" + apiPath;
        break;
    }

    Log.i(ApiSecurityManager.class.getSimpleName(), m_token);

    String href = "http://tysomapi.fr3dom.net/" + path + "?token=" + m_token;
    Log.i(ApiSecurityManager.class.getSimpleName(), href);
    try
    {
      String myIp = getIp();
      String charset = "UTF-8";
      File file = new File(filePath);

      PrintWriter writer;
      OutputStream outputStream;

      URL url = new URL(href);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setRequestProperty("User-Agent", "java");


      conn.setDoInput(true);
      conn.setUseCaches(false);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("image", file.getName());
      conn.setRequestProperty("Content-Type", "multipart/form-data; boundary = " + boundary);
      conn.setRequestProperty("X-Forwarded-For", myIp);
      conn.setDoOutput(true);
      outputStream = conn.getOutputStream();
      writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);

      writer.append(twoHyphens + boundary + LINE_FEED);
      writer.append("Content-Disposition: form-data; name=\"image\"; filename=\"" + file.getName() + "\"" + LINE_FEED);
      writer.append("ContentType: image/peg" + LINE_FEED);
      writer.append(twoHyphens + boundary + LINE_FEED);
      writer.flush();

      writer.append(twoHyphens + boundary + LINE_FEED);
      writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
      writer.append(LINE_FEED);
      writer.flush();

      FileInputStream inputStream = new FileInputStream(file);
      byte[] buffer = new byte[4096];
      int bytesRead = -1;
      while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
      }
      outputStream.flush();
      inputStream.close();

      writer.append(LINE_FEED);
      writer.flush();

      writer.append(LINE_FEED);
      writer.append(twoHyphens + boundary + twoHyphens + LINE_FEED);
      writer.close();

      Log.i(getClass().getSimpleName(), "Response Code: " + conn.getResponseCode());
      if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
      {
        throw new RuntimeException("Failed : HTTP error code : "
            + conn.getResponseCode());
      }

      BufferedReader br = new BufferedReader(new InputStreamReader(
          (conn.getInputStream())));

      String output;

      while ((output = br.readLine()) != null)
      {
        result = result + output;
      }

      conn.disconnect();
    }

    catch (
        MalformedURLException e
        )

    {
      e.printStackTrace();
    }

    catch (
        IOException e
        )

    {
      e.printStackTrace();
    }

    return result;
  }

1 个答案:

答案 0 :(得分:0)

使用此库:

https://github.com/gotev/android-upload-service/wiki

它会自动处理URL连接,故障和重试。