[英]How to upload a file using httppost FileEntity working with PHP server
我在客戶端上的Java代碼是:
FileEntity entity = new FileEntity(zipfile, contentType);
post.addHeader(entity.getContentType());
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
我想在Apache服務器上使用PHP接收它。 我不知道如何在php函數move_uploaded_file($_FILES[" "]["tmp_name"],$upload_file)
中的$_FILES[" "]["tmp_name"]
中設置第一個值。 我見過其他人也問過同樣的問題。但是答案還不夠清楚。 我在等待您的答復,非常感謝!
我已經准備好進行自己的測試,並且已經通過下載apache httpclient在android上完成了,但這對您的情況來說應該不是問題。
我在“庫”文件夾中包含了httpclient.jar和httpmime.jar。
最重要的是在multipart-message的mime-part上設置part-name。 那是您的任務的第一部分。 php數組。
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url_string);
File file = new File(file_string);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
System.out
.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
System.out.println(statusLine);
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
return statusLine != null ? statusLine.getStatusCode() : 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.