2017-02-16 4 views
0

Ich habe diesen Code, der eine JPEG-Datei auf den Server hochladen kann, aber die Datei wird nicht als JPEG erkannt. Ich denke, mein Problem besteht darin, die JPEG-Datei korrekt zu kodieren. Meine Lösung ist im Wesentlichen die gleiche wie this one. Ich habe andere Varianten versucht, die JPEG-Bytes unter Verwendung FileInputStream anzufügen und DataOutputStream anstelle von OutputStreamWriter usw. ohne Erfolg zu verwenden. Jeder Vorschlag geschätzt.Android HttpURLConnection Bild hochgeladen aber Datei nicht als JPEG erkannt

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

'Ich glaube, mein Problem ist es, die correctly.' JPEG-Datei zu codieren. Welche Art von Codierung? Und warum würden Sie es kodieren? Laden Sie die Datei lieber "so wie sie ist" hoch. – greenapps

+0

'OutputStreamWriter request = null;'. Warum rufen Sie einen Ausgabestream-Writer eine "Anfrage" an? Sie schreiben auf diese Weise einen ungelesenen Code. – greenapps

+0

'.append (" \ n \ n ");'. Du hast zu viele dieser leeren Zeilen. – greenapps

Antwort

0

Dank this post here, Lösung wird wie folgt dar:

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; 

Ich habe diese in den @Override protected String doInBackground(String... params) ein AsyncTask<String, Void, String>

Verwandte Themen