【发布时间】:2013-11-07 15:02:53
【问题描述】:
我正在使用“uploadAvatar”方法在我的 ubuntu 服务器上使用 android 4.1 和 php 使用 nginx 和 php-fpm 5.3 上传图像。照片是从相机中挑选或拍摄的,然后被裁剪,保存在文件夹中,然后上传到服务器。
我可以通过图库应用程序在手机中的正确路径上看到图像。我用图片的绝对路径调用方法,然后应用程序开始上传图片,服务器响应200,图片在服务器上但是坏了,我无法用浏览器读取图片。
public int uploadAvatar(String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
ProgressDialog_.dismiss();
Log.e("uploadFile", "Source File not exist :" + sourceFile.getAbsolutePath());
runOnUiThread(new Runnable() {
public void run() {
//messageText.setText("Source File not exist :"+uploadFilePath + "" + uploadFileName);
}
});
return 0;
}
else
{
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename=" + fileName + " + lineEnd");
dos.writeBytes(lineEnd);
// 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);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
/*
String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
+" http://www.androidexample.com/media/uploads/"
+uploadFileName;
//messageText.setText(msg);
*/
Toast.makeText(Register_avatar.this, "File Upload Complete.",
Toast.LENGTH_SHORT).show();
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ProgressDialog_.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
//messageText.setText("MalformedURLException Exception : check script url.");
Toast.makeText(Register_avatar.this, "MalformedURLException",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
ProgressDialog_.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
//messageText.setText("Got Exception : see logcat ");
Toast.makeText(Register_avatar.this, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}
ProgressDialog_.dismiss();
return serverResponseCode;
} // End else block
}
这是服务器端的 PHP 脚本:
图片已上传但损坏,在设备上正常... 我正在处理这个问题大约 2 天,有人可以帮助我吗?
你可以在这里看到损坏的图像:http://putp.about42.com/testimage.png
更新:
要构建这个方法,我遵循here 的教程:
我在该行中发现了一个错误:
dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename=""
+ fileName + """ + lineEnd);
我改变了这个:
dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename=" + fileName + " + lineEnd");
也许损坏的图像取决于此...对不起,但我不知道这行代码是做什么的...
谢谢
【问题讨论】:
-
@LatheesanKanes 谢谢,但这正是我用来构建我的方法的教程:( 出现错误 dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename= ""+ fileName + """ +lineEnd); 我解决了: dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename=" + fileName + " + lineEnd") 但也许这是错误...我不明白这行代码是什么意思:(一些帮助?谢谢