嗨,我想尝试将图片上传到服务器。但它在上传图像时给我IOException,这意味着异常部分总是运行。请帮我解决这个问题。
我使用以下代码:
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);
}
答案 0 :(得分:1)
/
之后的网址http:/webleads.usawebdept.com/IPhone/leadImage.php
中有http