i have this problem that i want to upload image on a server through multi-part form but its keep giving me invalid image when i try to upload it from android but work fine from web. this is html form which work fine.
http://letitripple.org/htmlForm.html
this is the request this html form create. (i get from chrome developer tool)
------WebKitFormBoundaryPug6xAUAlaUPbR86 Content-Disposition: form-data; name="wp-user-avatars"; filename="Desert.jpg" Content-Type: image/jpeg
------WebKitFormBoundaryPug6xAUAlaUPbR86 Content-Disposition: form-data; name="login_id"
51 ------WebKitFormBoundaryPug6xAUAlaUPbR86 Content-Disposition: form-data; name="cookie"
[email protected]|1473394633|vcN3CTbKi6pkAjPMKH1D9MHucPLKhw3wyeS7ViSTgGb|f5cbd913f6c3d6940066a660bac5908adb016682ce34f48e8200773eb4503c1e ------WebKitFormBoundaryPug6xAUAlaUPbR86 Content-Disposition: form-data; name="submit"
submit ------WebKitFormBoundaryPug6xAUAlaUPbR86--
This is my android code which is not working.
public AppResponse uploadImageFile(String csURL, String csFilePath)
{
AppResponse appResponse = null;
HttpURLConnection conn = null;
DataOutputStream dos = null;
ByteArrayOutputStream output = null;
InputStream inStream = null;
String Url = csURL;
String existingFileName = csFilePath;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "WebKitFormBoundaryleA9RIQsQBxW8Cgl";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
// String reponse_data = null;
int maxBufferSize = 1 * 1024 * 1024;
// String urlString = "YOUR PHP LINK FOR UPLOADING IMAGE";
try {
//------------------ CLIENT REQUEST
// FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
// open a URL connection to the Servlet
URL url = new URL(Url);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));//You can get an inputStream using any IO API
byte[] bytes;
byte[] buffers = new byte[605244];
int bytesReads;
output = new ByteArrayOutputStream();
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + twoHyphens + twoHyphens + boundary);
//conn.setRequestProperty("attach1", existingFileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + twoHyphens + twoHyphens + boundary + "\r\n");
dos.writeBytes("Content-Disposition: form-data; name=\"attach1\"; filename=\"profile.jpg\"\n Content-Type: image/jpg\r\n\r\n\r\n"); // uploaded_file_name is the Name of the File to be uploaded
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.e("Value", "Writing");
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + twoHyphens + twoHyphens + boundary + twoHyphens);
//String strInput = dos.toString();
int responseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
int nResponseCode = conn.getResponseCode();
inStream = new DataInputStream(conn.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
String line = "";
StringBuilder csResult = new StringBuilder("");
while ((line = in.readLine()) != null) {
Log.e("Debug", "Server Response " + csResult);
csResult.append(line);
}
appResponse = new AppResponse(nResponseCode, csResult.toString());
}
catch (IOException ioe) {
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
return appResponse;
}