我发现这个解决方案非常好,即使在Amazon EC2上也可以100%工作,看看这个链接:
使用Android进行POST上传文件到HTTP服务器(链接已删除)
与以前的答案相比,这个解决方案不需要从Apache导入大型库httpmime
。
从原始文章中复制的文本:
本教程展示了使用Android SDK将数据(图像、MP3、文本文件等)上传到HTTP/PHP服务器的简单方法。
它包括所有在Android端使上传工作的所需代码,以及一个简单的PHP服务器端代码来处理文件上传和保存。此外,它还提供了有关如何在上传文件时处理基本授权的信息。
在模拟器上测试时,请记得通过DDMS或命令行将您的测试文件添加到Android的文件系统中。
我们要做的是设置请求的适当内容类型,并将字节数组包含为post请求的主体。字节数组将包含我们想要发送到服务器的文件的内容。
下面您将找到一个有用的代码片段,执行上传操作。该代码还包括服务器响应处理。
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "/data/file_to_send.mp3";
String urlServer = "http://192.168.1.1/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();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
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);
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
}
如果您需要在上传文件时使用用户名和密码对用户进行身份验证,下面的代码片段显示了如何添加它。您需要做的只是在创建连接时设置授权标头。
String usernamePassword = yourUsername + “:” + yourPassword
String encodedUsernamePassword = Base64.encodeToString(usernamePassword.getBytes(), Base64.DEFAULT)
connection.setRequestProperty (“Authorization”, “Basic ” + encodedUsernamePassword)
假设一个PHP脚本负责在服务器端接收数据。这样的PHP脚本示例可能如下所示:
<?php
$target_path = "./";
$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 2.1和4.3上测试。请记得在服务器端的脚本中添加权限,否则上传将无法工作。
chmod 777 uploadsfolder
uploadsfolder 是文件上传的文件夹。如果您计划上传大于默认2MB文件大小限制的文件,则必须修改 php.ini 文件中的 upload_max_filesize 值。