图像/视频/音频成功从Android上传到PHP,但可以';t打开它们-损坏/损坏
我在Android应用程序中发出此HTTP POST请求:图像/视频/音频成功从Android上传到PHP,但可以';t打开它们-损坏/损坏,php,android,Php,Android,我在Android应用程序中发出此HTTP POST请求: private final String delimiter = "--"; private final String boundary = "SwA" + Long.toString(System.currentTimeMillis()) + "SwA"; private final String charset = "UTF-8"; private final String l
private final String delimiter = "--";
private final String boundary = "SwA"
+ Long.toString(System.currentTimeMillis()) + "SwA";
private final String charset = "UTF-8";
private final String lineSpace = "\r\n";
private final String domain = (domain);
private HttpURLConnection configureConnectionForMultipart(String url)
throws MalformedURLException, IOException {
HttpURLConnection con = (HttpURLConnection) (new URL(url))
.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="
+ boundary);
return con;
}
private void addFormPart(String paramName, String value, DataOutputStream os)
throws IOException {
os.writeBytes(lineSpace + delimiter + boundary + lineSpace);
os.writeBytes("Content-Disposition: form-data; name=\"" + paramName
+ "\"" + lineSpace);
os.writeBytes("Content-Type: text/plain; charset=" + charset
+ lineSpace);
os.writeBytes(lineSpace + value + lineSpace);
os.flush();
}
private void addFilePart(String paramName, File data, DataOutputStream os)
throws IOException {
os.writeBytes(lineSpace + delimiter + boundary + lineSpace);
os.writeBytes("Content-Disposition: form-data; name=\"" + paramName
+ "\"; filename=\"" + data.getAbsolutePath() + "\"" + lineSpace);
os.writeBytes("Content-Type: application/octet \r\n");
os.writeBytes("Content-Transfer-Encoding: binary" + lineSpace);
// os.writeBytes(lineSpace);
os.flush();
FileInputStream fis = new FileInputStream(data);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.writeBytes(lineSpace);
os.flush();
fis.close();
}
private void finishMultipart(DataOutputStream os) throws IOException {
// os.writeBytes(lineSpace);
os.flush();
os.writeBytes(delimiter + boundary + delimiter + lineSpace);
os.close();
}
private class ObjectUploadRunnable implements Runnable {
private final String _filePath;
private final String _url = domain + "upload.php";
public ObjectUploadRunnable(String filePath) {
_filePath = filePath;
}
@Override
public void run() {
try {
HttpURLConnection con = configureConnectionForMultipart(_url);
con.connect();
DataOutputStream os = new DataOutputStream(
con.getOutputStream());
File data = new File(_filePath);
addFilePart("data", data, os);
finishMultipart(os);
String response = getResponse(con);
Log.i("BoxUpload", response);
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我通过脚本upload.php在服务器上捕获它:
...
$dir = "/uploads/";
$target_path = $dir.basename($_FILES['data']['name']);
if (move_uploaded_file($_FILES['data']['tmp_name'], $target_path)) {
echo "file uploaded";
} else {
echo print_r(error_get_last());
}
一切似乎都成功了,一个大小正确的文件被上传到我的服务器上,在所需的目录中。但是,当我尝试打开文件时,它似乎在某种程度上被损坏或损坏,因为它不会在我尝试的任何应用程序中打开。我上传的图像=jpeg,视频=mp4,音频=mp4。这些文件在上传之前都在客户端上工作。我在POST请求中是否缺少正确编码文件的内容?我以前从来没有上传过文件,所以我希望你能给我一些建议
编辑
如果这是相关的,我注意到我上传的文件增加了约100kb。可能是我的二进制数据中添加了一些东西,导致文件损坏?在这里解决了问题。我在addFilePart中注释掉的那一行实际上是必要的。我猜在请求的这一部分中,头信息和二进制数据之间需要有两行。为了清楚起见,它应该是这样的:
private void addFilePart(String paramName, File data, DataOutputStream os)
throws IOException {
os.writeBytes(lineSpace + delimiter + boundary + lineSpace);
os.writeBytes("Content-Disposition: form-data; name=\"" + paramName
+ "\"; filename=\"" + data.getAbsolutePath() + "\"" + lineSpace);
os.writeBytes("Content-Type: application/octet \r\n");
os.writeBytes("Content-Transfer-Encoding: binary" + lineSpace);
os.writeBytes(lineSpace);
os.flush();
FileInputStream fis = new FileInputStream(data);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.writeBytes(lineSpace);
os.flush();
fis.close();
}
现在一切都很好 你只是假设上传永远不会失败。坏主意。在执行任何其他操作之前,请始终检查$\u文件中的
['errors']
参数。好的,我执行了var\u转储[$\u文件),错误字段显示0个错误。这足够了吗?我认为实际上载中没有任何错误,因为一个大小正确的文件上载到了我想要的位置。我只是无法打开它。