Php 图像上传问题
在我的应用程序中,我试图将文件从内置的android gallery上传到php服务器 我正在获取图像的完整路径,例如/mnt/sdcard/image.jpg…当我单击图像上载时,它不会显示任何异常或错误或诸如此类的内容…如果我打印的服务器响应代码为200,响应正常…但图像不会上载到服务器上 请帮忙 这是我的密码Php 图像上传问题,php,android,Php,Android,在我的应用程序中,我试图将文件从内置的android gallery上传到php服务器 我正在获取图像的完整路径,例如/mnt/sdcard/image.jpg…当我单击图像上载时,它不会显示任何异常或错误或诸如此类的内容…如果我打印的服务器响应代码为200,响应正常…但图像不会上载到服务器上 请帮忙 这是我的密码 private void uploadImage(String imagePath2) { String serverResponseMessage = null;
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……这会导致任何问题吗
我的服务器端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@Lukas…嗨…对不起,我不太擅长php部分…php部分不是我做的…所以如果你能解释一下,那会很有帮助。。。