1
在我的應用程序中,我試圖從內置的android圖庫上傳文件到php服務器。圖片上傳問題
我得到圖像的完整路徑例如/mnt/sdcard/image.jpg...當我點擊圖片上傳它不會顯示任何異常或錯誤或類似的東西....如果我打印服務器響應代碼是200和響應是OK ...但圖像不能在服務器上獲取。
請幫助!!!!
這裏是我的代碼
private void uploadImage(String imagePath2) {
String serverResponseMessage = null;
File file2=new File(imagePath2);
String file=file2.getName().replace("/","");
Log.i("file path",""+file.toString());
try
{
FileInputStream fileInputStream = new FileInputStream(new File(file));
/* FileInputStream fileInputStream=new FileInputStream(file);
bitmap=BitmapFactory.decodeStream(fileInputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();*/
URL url = new URL("http://ufindfish.b4live.com/uploadfile.php");
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);
//connection.addRequestProperty("skey",""+key);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + file +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
int maxBufferSize=1000;
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)
Log.e("debug", "File is written");
int serverResponseCode = connection.getResponseCode();
Log.i("Responce code",""+serverResponseCode);
serverResponseMessage = connection.getResponseMessage();
Log.i("Responce :",serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
if(serverResponseMessage.equalsIgnoreCase("Ok"))
{
Toast.makeText(UploadFromGalleryActivity.this,"Image Uploaded Sucessfully!!",Toast.LENGTH_LONG).show();
}
else if(serverResponseMessage.equalsIgnoreCase("error"))
{
Toast.makeText(UploadFromGalleryActivity.this,"Cannot upload.Please try again",Toast.LENGTH_LONG).show();
}
}
}
注:字符串路徑2我越來越路徑一樣,造成任何問題/mnt/sdcard/zoo.jpg...is?
我的服務器端的PHP文件是
<?php
$uploaddir = 'CatchOfTheDayImages/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "OK";
}
else {
echo "ERROR";
}
exit();
?>
嘗試檢查:is_uploaded_file函數(),並嘗試使用:copy()PHP函數而不是move_uploaded_file – metaforce 2011-05-27 11:42:17
@Lukas ...嗨...我很抱歉,但我不是很擅長php部分.. .php部分不是由我完成的......所以如果你能解釋一下它會有所幫助...... – android 2011-05-27 12:13:19