簡體   English   中英

Android:為什么我無法將圖像上傳到Web服務器?

[英]Android: Why am I unable to upload an image to a web server?

我正在使用HttpUrlConnection將圖像上傳到Web服務器。 當我運行應用程序並嘗試上傳圖像時,我得到一個Http響應200以及接收已上傳的假定圖像的文件名和imageid,但是當我檢查服務器時圖像沒有上傳。 文件名和id現在是列表的一部分,但是當我嘗試檢索圖像時,它返回null。

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();

    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("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary = " + boundary);
    conn.setRequestProperty("X-Forwarded-For", myIp);
    conn.setDoOutput(true);

    File file = new File(filePath);

    DataOutputStream ds = new DataOutputStream(conn.getOutputStream());
    ds.writeBytes(twoHyphens + boundary + LINE_FEED);
    ds.writeBytes("Content-Disposition: form-data; name=\"image\"; filename=\"" + file.getName() + "\"" + LINE_FEED);
    ds.writeBytes("ContentType: image/peg" + LINE_FEED);
    ds.writeBytes(twoHyphens + boundary + LINE_FEED);


    FileInputStream fStream = new FileInputStream(file);

    int bytesAvailable = fStream.available();

    int maxBufferSize = 1024;
    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bufferSize];

    int bytesRead = fStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
      ds.write(buffer, 0, bufferSize);
      bytesAvailable = fStream.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      bytesRead = fStream.read(buffer, 0, bufferSize);
    }

    ds.writeBytes(LINE_FEED);
    ds.writeBytes(twoHyphens + boundary + twoHyphens + LINE_FEED);

    fStream.close();
    ds.flush();
    ds.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;
}

我能夠通過使用PrintWriter和OutputStream而不是DataOutputStream來傳遞標題和圖像來解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM