图片上传问题

问题描述

| 在我的应用程序中,我试图将文件从内置的android画廊上传PHP服务器。 我正在获取完整的图像路径,例如/mnt/sdcard/image.jpg ...当我单击图像上载时,它不会显示任何异常或错误或类似的东西....如果我打印服务器的响应代码为200并且响应是可以的...但是图片未在服务器上上传。 请帮忙!!!! 这是我的代码
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,bufferSize);

        while (bytesRead > 0)
        {
        outputStream.write(buffer,bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable,maxBufferSize);
        bytesRead = fileInputStream.read(buffer,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();

       }
    }
}
注意:在字符串path2中,我正在获取/mnt/sdcard/zoo.jpg之类的路径...这是否引起任何问题? 我的服务器端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();
?>
    

解决方法

        本文可能有助于重构您的实现 http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/ 编码愉快!