如何使用PHP的JavaHttpClient库上载文件

如何使用PHP的JavaHttpClient库上载文件

我想编写Java应用程序,用PHP将文件上传到Apache服务器。Java代码使用JakartaHttpClient库版本4.0 Beta 2:

import java.io.File;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;public class PostFile {
  public static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
    File file = new File("c:/TRASH/zaba_1.jpg");

    FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");

    httppost.setEntity(reqEntity);
    reqEntity.setContentType("binary/octet-stream");
    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
      resEntity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();
  }}

PHP文件upload.php非常简单:

<?phpif (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
  move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $_FILES['userfile'] ['name']);} else {
  echo "Possible file upload attack: ";
  echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
  print_r($_FILES);}?>

以请求是成功的,我能够与服务器通信,但是PHP没有注意到文件-这个方法is_uploaded_file退回来false$_FILES变量是空的。我不知道为什么会发生这种事。我跟踪了HTTP响应和请求,它们看起来很好

小唯快跑啊
浏览 532回答 3
3回答

阿晨1998

好的,我使用的Java代码是错误的,下面是正确的Java类:import&nbsp;java.io.File;import&nbsp;org.apache.http.HttpEntity;import&nbsp;org.apache.http.HttpResponse;import&nbsp;org.apache.http.HttpVersion; import&nbsp;org.apache.http.client.HttpClient;import&nbsp;org.apache.http.client.methods.HttpPost;import&nbsp;org.apache.http.entity.mime.MultipartEntity; import&nbsp;org.apache.http.entity.mime.content.ContentBody;import&nbsp;org.apache.http.entity.mime.content.FileBody;import&nbsp;org.apache.http. impl.client.DefaultHttpClient;import&nbsp;org.apache.http.params.CoreProtocolPNames;import&nbsp;org.apache.http.util.EntityUtils; public&nbsp;class&nbsp;PostFile&nbsp;{ &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;HttpClient&nbsp;httpclient&nbsp;=&nbsp;new&nbsp;DefaultHttpClient(); &nbsp;&nbsp;&nbsp;&nbsp;httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,&nbsp;HttpVersion.HTTP_1_1); &nbsp;&nbsp;&nbsp;&nbsp;HttpPost&nbsp;httppost&nbsp;=&nbsp;new&nbsp;HttpPost("http://localhost:9001/upload.php"); &nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;file&nbsp;=&nbsp;new&nbsp;File("c:/TRASH/zaba_1.jpg"); &nbsp;&nbsp;&nbsp;&nbsp;MultipartEntity&nbsp;mpEntity&nbsp;=&nbsp;new&nbsp;MultipartEntity(); &nbsp;&nbsp;&nbsp;&nbsp;ContentBody&nbsp;cbFile&nbsp;=&nbsp;new&nbsp;FileBody(file,&nbsp;"image/jpeg"); &nbsp;&nbsp;&nbsp;&nbsp;mpEntity.addPart("userfile",&nbsp;cbFile); &nbsp;&nbsp;&nbsp;&nbsp;httppost.setEntity(mpEntity); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("executing&nbsp;request&nbsp;"&nbsp;+&nbsp;httppost.getRequestLine()); &nbsp;&nbsp;&nbsp;&nbsp;HttpResponse&nbsp;response&nbsp;=&nbsp;httpclient.execute(httppost); &nbsp;&nbsp;&nbsp;&nbsp;HttpEntity&nbsp;resEntity&nbsp;=&nbsp;response.getEntity(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println(response.getStatusLine()); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(resEntity&nbsp;!=&nbsp;null)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(EntityUtils.toString(resEntity)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(resEntity&nbsp;!=&nbsp;null)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resEntity.consumeContent(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;httpclient.getConnectionManager().shutdown(); &nbsp;&nbsp;}}注意使用MultipartEntity。
打开App,查看更多内容
随时随地看视频慕课网APP