如何使用使用 PHP 的 Java HttpClient 库上传文件 [英] How to upload a file using Java HttpClient library working with PHP

查看:23
本文介绍了如何使用使用 PHP 的 Java HttpClient 库上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写 Java 应用程序,用 PHP 将文件上传到 Apache 服务器.Java 代码使用 Jakarta HttpClient 库版本 4.0 beta2:

import java.io.File;导入 org.apache.http.HttpEntity;导入 org.apache.http.HttpResponse;导入 org.apache.http.HttpVersion;导入 org.apache.http.client.HttpClient;导入 org.apache.http.client.methods.HttpPost;导入 org.apache.http.entity.FileEntity;导入 org.apache.http.impl.client.DefaultHttpClient;导入 org.apache.http.params.CoreProtocolPNames;导入 org.apache.http.util.EntityUtils;公共类 PostFile {public static void main(String[] args) 抛出异常 {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("二进制/八位字节流");System.out.println("执行请求" + httppost.getRequestLine());HttpResponse 响应 = httpclient.execute(httppost);HttpEntity resEntity = response.getEntity();System.out.println(response.getStatusLine());如果(resEntity != null){System.out.println(EntityUtils.toString(resEntity));}如果(resEntity != null){resEntity.consumeContent();}httpclient.getConnectionManager().shutdown();}}

PHP 文件 upload.php 非常简单:

阅读回复我得到以下结果:

执行请求 POST http://localhost:9002/upload.php HTTP/1.1

<前>HTTP/1.1 200 正常可能的文件上传攻击:文件名''.大批()

因此请求成功,我能够与服务器通信,但是 PHP 没有注意到该文件 - 方法 is_uploaded_file 返回了 false$_FILES 变量为空.我不知道为什么会发生这种情况.我已经跟踪了 HTTP 响应和请求,它们看起来不错:
请求是:

<前>POST/upload.php HTTP/1.1内容长度:13091内容类型:二进制/八位字节流主机:本地主机:9002连接:保持活动用户代理:Apache-HttpClient/4.0-beta2 (java 1.5)期望:100-继续˙Ř˙ŕ..... 二进制文件的其余部分...

和回复:

<前>HTTP/1.1 100 继续HTTP/1.1 200 正常日期: 2009 年 7 月 1 日,星期三 06:51:57 GMT服务器:Apache/2.2.8 (Win32) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g mod_autoindex_color PHP/5.2.5 mod_jk/1.2.26X-Powered-By:PHP/5.2.5内容长度:51保持活动:超时=5,最大值=100连接:保持活动内容类型:文本/html可能的文件上传攻击:filename ''.Array()

我在带有 xampp 的本地 windows xp 和远程 Linux 服务器上测试了这个.我也尝试使用以前版本的 HttpClient - 3.1 版 - 结果更不清楚,is_uploaded_file 返回 false,但是 $_FILES数组填充了正确的数据.

解决方案

好吧,我用的Java代码错了,正确的Java类来了:

import java.io.File;导入 org.apache.http.HttpEntity;导入 org.apache.http.HttpResponse;导入 org.apache.http.HttpVersion;导入 org.apache.http.client.HttpClient;导入 org.apache.http.client.methods.HttpPost;导入 org.apache.http.entity.mime.MultipartEntity;导入 org.apache.http.entity.mime.content.ContentBody;导入 org.apache.http.entity.mime.content.FileBody;导入 org.apache.http.impl.client.DefaultHttpClient;导入 org.apache.http.params.CoreProtocolPNames;导入 org.apache.http.util.EntityUtils;公共类 PostFile {public static void main(String[] args) 抛出异常 {HttpClient httpclient = new DefaultHttpClient();httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);HttpPost httppost = new HttpPost("http://localhost:9001/upload.php");File file = new File("c:/TRASH/zaba_1.jpg");MultipartEntity mpEntity = new MultipartEntity();ContentBody cbFile = new FileBody(file, "image/jpeg");mpEntity.addPart("userfile", cbFile);httppost.setEntity(mpEntity);System.out.println("执行请求" + httppost.getRequestLine());HttpResponse 响应 = httpclient.execute(httppost);HttpEntity resEntity = response.getEntity();System.out.println(response.getStatusLine());如果(resEntity != null){System.out.println(EntityUtils.toString(resEntity));}如果(resEntity != null){resEntity.consumeContent();}httpclient.getConnectionManager().shutdown();}}

注意使用 MultipartEntity.

I want to write Java application that will upload a file to the Apache server with PHP. The Java code uses Jakarta HttpClient library version 4.0 beta2:

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();
  }
}

The PHP file upload.php is very simple:

<?php
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.
";
  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);
}
?>

Reading the response I get the following result:

executing request POST http://localhost:9002/upload.php HTTP/1.1

HTTP/1.1 200 OK
Possible file upload attack: filename ''.
Array
(
)

So the request was successful, I was able to communicate with server, however PHP didn't notice the file - the method is_uploaded_file returned false and $_FILES variable is empty. I have no idea why this might happend. I have tracked HTTP response and request and they look ok:
request is:

POST /upload.php HTTP/1.1
Content-Length: 13091
Content-Type: binary/octet-stream
Host: localhost:9002
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.0-beta2 (java 1.5)
Expect: 100-Continue

˙Ř˙ŕ..... the rest of the binary file...

and response:

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Wed, 01 Jul 2009 06:51:57 GMT
Server: Apache/2.2.8 (Win32) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g mod_autoindex_color PHP/5.2.5 mod_jk/1.2.26
X-Powered-By: PHP/5.2.5
Content-Length: 51
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

Possible file upload attack: filename ''.Array
(
)

I was testing this both on the local windows xp with xampp and remote Linux server. I have also tried to use previous version of HttpClient - version 3.1 - and the result was even more unclear, is_uploaded_file returned false, however $_FILES array was filled with proper data.

解决方案

Ok, the Java code I used was wrong, here comes the right Java class:

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.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
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:9001/upload.php");
    File file = new File("c:/TRASH/zaba_1.jpg");

    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "image/jpeg");
    mpEntity.addPart("userfile", cbFile);


    httppost.setEntity(mpEntity);
    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();
  }
}

note using MultipartEntity.

这篇关于如何使用使用 PHP 的 Java HttpClient 库上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆