c 调用java 上传文件_如何使用Java HttpClient库和PHP一起上传文件

我想编写Java应用程序,它将使用PHP将文件上传到Apache服务器。Java代码使用Jakarta HttpClient库版本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();

}

}

PHP文件upload.php非常简单:

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

}

?>

阅读响应,我得到以下结果:

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

HTTP/1.1 200 OK

Possible file upload attack: filename ''.

Array

(

)

这样请求就成功了,我可以与服务器通信了,但是PHP没有注意到文件- is_uploaded_file返回的方法false和$_FILES变量为空。我不知道为什么会这样。我已经跟踪了HTTP响应和请求,它们看起来还不错:

请求是:

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

(

)

我正在使用xampp的本地Windows XP和远程Linux服务器上对此进行测试。我还尝试过使用HttpClient的早期版本-版本3.1- is_uploaded_file返回的结果更加不清楚false,但是$_FILES数组中填充了正确的数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值