我在客户端的java代码是:
FileEntity entity = new FileEntity(zipfile, contentType);
post.addHeader(entity.getContentType());
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
我想在 apache 服务器上使用 PHP 接收它。
我不知道如何设置 $_FILES[" "]["tmp_name"]
中的第一个值在php函数中move_uploaded_file($_FILES[" "]["tmp_name"],$upload_file)
。
我看到其他人问过同样的问题,但答案不够明确。我正在等待您的答复,非常感谢!
最佳答案
我已经准备好自己的测试了,并且已经通过下载 apache httpclient 在 Android 上完成了测试,但这对于您的情况来说应该不是问题。
我将 httpclient.jar 和 httpmime.jar 包含在“libs”文件夹中。
最重要的是在多部分消息的 mime 部分上设置部分名称。这是你的助理的第一部分。 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;
关于java - 如何使用 httppost FileEntity 与 PHP 服务器一起上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291588/