【发布时间】:2011-02-09 12:10:44
【问题描述】:
我正在尝试将视频从 Android 代码上传到我的 PHP Web 服务器代码,但无法成功。我指的是以下 link 来执行上传任务,但我在我的 Android 代码中收到以下响应,但在 PHP 服务器上找不到任何文件。
Android 响应
DEBUG/ServerCode(29484): 200
DEBUG/serverResponseMessage(29484): OK
我已经检查了很多事情,比如在php.ini 文件中设置值。
虽然我可以将图像从 Android 代码上传到服务器,因为我从 Android 发送了一个 64 位编码的ByteArray,并且 php 中有一个现成的函数可以从编码的ByteArrays 创建图像。
但是如果任何其他文件的类型不是图像,该代码也不起作用。
如果你们以前做过类似的事情,请指导我。
我正在使用的 PHP 代码:
<?php
$target_path = "./upload/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ".basename( $_FILES['uploadedfile']['name'])." has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
我正在使用的 Android 代码:
public void videoUpload()
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "/sdcard/video-2010-03-07-15-40-57.3gp";
String urlServer = "http://10.0.0.15/sampleWeb/handle_upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
URL url = new URL(urlServer);
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");
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile );
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
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)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d("ServerCode",""+serverResponseCode);
Log.d("serverResponseMessage",""+serverResponseMessage);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
【问题讨论】:
-
如果你保证你的 php.ini 没问题,那么你在代码中犯了一些错误。发布您的代码。您关注的博客看起来不错,但您可能在“关注”该博客文章时犯了一些错误。
-
嗨,Sarwar,我在这里更新了 Android 和 PHP 的代码。尝试了很多,但在这个小代码中找不到问题
-
Abhishek,你有没有在安卓服务器上上传视频的任何解决方案?我也在寻找相同的。如果你有任何想法,请与我分享。
-
是的,我已将其标记为下面的回答,它解决了目的,并且视频通过多部分表单上传得很好
标签: php android image-uploading