2017-02-16 8 views
0

이 코드는 JPEG 파일을 서버에 업로드 할 수 있지만 파일이 JPEG로 인식되지 않습니다. 제 문제는 JPEG 파일을 올바르게 인코딩하는 것입니다. 내 솔루션은 기본적으로 this one과 같습니다. FileInputStream을 사용하고 OutputStreamWriter 대신 DataOutputStream을 사용하여 JPEG 바이트를 추가 할 때 다른 변형을 시도했지만 아무 소용이 없습니다. 어떤 제안도 감사합니다.Android HttpURLConnection 이미지가 업로드되었지만 파일이 JPEG로 인식되지 않습니다.

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; 
+0

'내 문제는 JPEG 파일 correctly.'을 인코딩에 대해 생각합니다. 어떤 종류의 인코딩? 그리고 왜 그것을 인코딩하겠습니까? 파일을 '있는 그대로'업로드하는 것이 더 좋습니다. – greenapps

+0

'OutputStreamWriter request = null;'. 왜 출력 스트림 작성자를 '요청'이라고 부릅니까? 이런 식으로 읽지 않는 코드를 작성합니다. – greenapps

+0

'.append ("\ n \ n");'. 빈 줄이 너무 많습니다. – greenapps

답변

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>