Hi Iam trying to upload an image to server. But it is giving me IOException while uploading the image, which means the exception part is always get running. Pls help me how to resolve this problem.
Iam using the below code:
try {
UpurlString = "http:/xxxxx/xxx/UpImage.php";
length = fileInputStream.available();
//connectHTTP(UpurlString);
url = new URL(UpurlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
Log.e("FilenameCheck",filename);
dos.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\""
+ filename + "" + lineEnd);
dos.writeBytes(lineEnd);
buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) > 0) {
dos.write(buffer, 0, bytesRead);
sentData += bytesRead;
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();
inStream = new DataInputStream(conn.getInputStream());
while ((strResponse = inStream.readLine()) != null) {
Log.e("Response", strResponse);
/*if (strResponse.equalsIgnoreCase("The file " + filename
+ " has been uploaded")) {
} else {
}*/
}
inStream.close();
} catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}