在我的应用程序中,我正在尝试将文件从内置的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, 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();
}
}
}
注意:在字符串path2中我得到的路径如/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 :(得分:0)