Android HttpURLConnection图片上传但文件无法识别为JPEG

时间:2017-02-16 16:37:47

标签: android image jpeg httpurlconnection multipart

我有这个代码能够将JPEG文件上传到服务器,但文件不能识别为JPEG。我认为我的问题是正确编码JPEG文件。我的解决方案与this one基本相同。我尝试使用FileInputStream附加JPEG字节并使用DataOutputStream代替OutputStreamWriter等其他变体无效。任何建议都表示赞赏。

final String boundary = "==================";
final String mimeType = "image/jpeg";
final int IMAGE_QUALITY = 100;

URL url = null;
HttpURLConnection urlConnection = null;
OutputStreamWriter request = null;
String response = null;

try {
    url = new URL(params[0]);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);     ///
    urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    urlConnection.setRequestMethod("POST");

    OutputStream outputStream= urlConnection.getOutputStream();

    request = new OutputStreamWriter(outputStream);
    request.append("--" + boundary).append("\n");
    request.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + imageFileName + "\"").append("\n\n");
    request.append("Content-Type: " + mimeType).append("\n\n");
    request.append("Content-Encoding: base64").append("\n\n");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    imageThumbnail.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, stream);
    byte[] byteArray = stream.toByteArray();
    //request.append(new String(byteArray)).append("\n");
    String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
    request.append(encodedImage);

    request.append("--" + boundary + "--");
    request.flush();
    request.close();

    String line = null;
    InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
    BufferedReader reader = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    response = sb.toString();   // = "Success"

    isr.close();
    reader.close();

} catch (MalformedURLException e) {
    e.printStackTrace();
    response = "Malformed URL";

} catch (IOException e) {
    e.printStackTrace();
    response = "IO Exception";
}

return response;  

1 个答案:

答案 0 :(得分:0)

感谢this post here,解决方案如下:

final String boundary = "==================";
final String twoHyphens = "--";
final String crlf = "\r\n";
final String mimeType = "image/jpeg";
final int IMAGE_QUALITY = 100;

URL url = null;
HttpURLConnection urlConnection = null;
DataOutputStream dos;
String response = null;

try {
    url = new URL(params[0]);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);     ///
    urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    //urlConnection.setRequestProperty("Content-Type", "image/jpeg");
    urlConnection.setRequestMethod("POST");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    imageThumbnail.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, stream);
    byte[] byteArray = stream.toByteArray();

    dos = new DataOutputStream(urlConnection.getOutputStream());
    dos.writeBytes(twoHyphens + boundary + crlf);
    dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + imageFileName + "\"" + crlf);
    dos.writeBytes("Content-Type: " + mimeType + crlf);
    dos.writeBytes(crlf);
    dos.write(byteArray);
    dos.writeBytes(crlf);
    dos.writeBytes(twoHyphens + boundary + twoHyphens);
    dos.flush();
    dos.close();

    String line = null;
    InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
    BufferedReader reader = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    response = sb.toString();

    isr.close();
    reader.close();

} catch (MalformedURLException e) {
    e.printStackTrace();
    response = "Malformed URL";

} catch (IOException e) {
    e.printStackTrace();
    response = "IO Exception";
}

return response;

我在@Override protected String doInBackground(String... params)

AsyncTask<String, Void, String>内有这些内容