我发现这个解决方案创建得非常好,即使使用 amazon ec2 也能 100% 工作,看看这个链接:
在 Android 上使用 POST 将文件上传到 HTTP 服务器(链接已删除)。
与之前的答案相比,此解决方案不需要httpmime
从 Apache导入庞大的库。
从原始文章复制文本:
本教程展示了一种使用 Android SDK 将数据(图像、MP3、文本文件等)上传到 HTTP/PHP 服务器的简单方法。
它包括在 Android 端进行上传工作所需的所有代码,以及用于处理文件上传和保存的 PHP 中的简单服务器端代码。此外,它还为您提供有关在上传文件时如何处理基本自动化的信息。
在模拟器上进行测试时,请记住通过 DDMS 或命令行将您的测试文件添加到 Android 的文件系统中。
我们要做的是设置请求的适当内容类型,并包含字节数组作为帖子的正文。字节数组将包含我们要发送到服务器的文件的内容。
您将在下面找到执行上传操作的有用代码片段。该代码还包括服务器响应处理。
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();
// Allow Inputs & Outputs.
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Set HTTP method to POST.
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];
// 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)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}
如果您需要在上传文件时使用用户名和密码对用户进行身份验证,下面的代码片段显示了如何添加它。您所要做的就是在创建连接时设置授权标头。
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 值。