java - Android:为什么我无法将图像上传到网络服务器?

标签 java android httpurlconnection

我正在使用 HttpUrlConnection 将图像上传到网络服务器。当我运行应用程序并尝试上传图像时,我收到 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 来传递标题和图像来解决该问题。

关于java - Android:为什么我无法将图像上传到网络服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38219793/

相关文章:

java - Zimbra:使用 HttpClient 将联系人下载为 CSV 文件

android - Environment.getExternalStorageState() 返回错误

android - 在 cordova android 的状态栏中显示多个推送通知

java - 什么时候用 Java 在 HttpURLConnection 中设置 cookie?

java - 在 swing 应用程序中使用多种语言

java - 将图片附加到使用 java 邮件 API 发送的电子邮件

java - Java 中不同枚举类联合的 switch 语句

android - 得到错误 : spawn EACCES while ionic build android in ubuntu 14. 04

java - android中的Http GET请求错误

java - Facebook Messenger 不发送消息