2011-05-27 101 views
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(); 
?> 
+0

尝试检查:is_uploaded_file函数(),并尝试使用:copy()PHP函数而不是move_uploaded_file – metaforce 2011-05-27 11:42:17

+0

@Lukas ...嗨...我很抱歉,但我不是很擅长php部分.. .php部分不是由我完成的......所以如果你能解释一下它会有所帮助...... – android 2011-05-27 12:13:19

回答