以下是使用PHP上傳文件的片段。我是Android開發人員,我想上傳文件。這意味着我必須向包含此腳本的URL發送POST請求。POST請求在cURL中成功,但在Java中失敗
如果我想上傳文件,我應該使用什麼參數名稱?
PHP
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "https://some.site/upload/" . $_FILES["file"]["name"];
}
當我嘗試捲曲,該文件被上傳成功,但是當我通過Java做到這一點,它失敗。
捲曲
curl -F [email protected]_me_now_2.wav http://some.site/upload/
Java代碼的
File f = new File("call_me_now_11.wav");
HttpPost filePost = new HttpPost("https://some.site/upload/");
FileEntity fileEntity = new FileEntity(f, "audio/wav");
filePost.setEntity(fileEntity);
HttpClient client = new DefaultHttpClient();
client.execute(filePost, new BasicResponseHandler() {
@Override
public String handleResponse(HttpResponse response)
throws HttpResponseException, IOException {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
int c = 0;
StringBuilder builder = new StringBuilder();
while((c = content.read()) != -1) {
builder.append((char) c);
}
System.out.println(builder.toString());
return builder.toString();
}
});
錯誤/堆棧跟蹤? – 2012-04-19 09:33:20