我在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 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,videos = mp4,audio = mp4。这些文件在上传之前都在客户端上运行。我错过了在POST请求中正确编码文件的东西吗?我之前从未做过文件上传,所以我很感激一些建议...
修改
如果这是相关的,我已经注意到我上传的文件增长了大约100kb。也许某些东西会被添加到我的二进制数据中,这会破坏文件?
答案 0 :(得分:0)
在这里找出问题所在。我在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();
}
现在一切都很好!