从Android上传图像到PHP服务器的问题
问题描述:
嗨全部 我想上传图像文件从SD卡到Android的PHP服务器。我使用下面的代码上传,它不显示异常,但图像没有上传到服务器。我不知道是什么问题。请协助我。我附上我的代码和Logcat信息。 代码:从Android上传图像到PHP服务器的问题
public class UploadImage extends Activity {
InputStream is;
private int serverResponseCode;
private String serverResponseMessage;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "//sdcard//chsevtoneta.png";
String urlServer = "http://xxxxxxxxxxxxxxxxxxxxxxxx/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
Log.e("response",""+serverResponseCode);
Log.e("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}
}
}
和logcat的信息:
05-10 11:04:59.762: ERROR/response(2203): 200
05-10 11:04:59.762: ERROR/serverResponseMessage(2203): OK
答
看着你的源代码看起来你正在手工构建一个multipart/form上传:这很容易出错,在错误的地方添加换行符或额外的空格将很难通过但很容易混淆服务器(取决于所使用的HTTP解析器的严格性)。
我会推荐使用Android的内置HTTP组件来做到这一点:一个例子是在http://indiwiz.com/2009/02/11/multi-part-content-upload-in-apache-http-components-http-client/。
答
具有成功(200)消息的远程服务器的答案。我认为这个错误可能在服务器端,但是你不提供这部分的任何代码。
+0
谢谢,但服务器编码不适用于我。但对于其他服务器返回正确的响应[服务器返回一个网址]。任何获得回应的方法? – 2011-05-10 07:33:36
您的服务器正在返回成功消息:您的PHP代码是什么样的? – Femi 2011-05-10 06:56:21
谢谢,但服务器编码不适用于我。但对于其他服务器返回正确的响应[服务器返回一个网址]。任何获得回应的方法? – 2011-05-10 07:29:22
是的,那样很难调试:你有其他人的工作代码吗? – Femi 2011-05-10 07:30:22