java - 如何使用 httppost FileEntity 与 PHP 服务器一起上传文件

标签 java php

我在客户端的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/

相关文章:

php - 选择、检查日期和插入的 MySQL 存储过程

php - 如何通过删除浏览器的默认页眉和页脚在 Chrome 的 window.print() 中的每个页面上设置页码

php - MySQL JOIN 不工作

java - 如何查看windows进程参数

java - 使用 JAXB 编译多个包含相同元素的重复定义的 XSD

java - 为什么这段代码打印的是 20 20 而不是 20 10?

java - HttpSession,session.getAttribute(),问题

php - php 中是否有三元运算符的反向版本?

java - 如何从 JSF/Webflow 应用程序提供二进制内容?

php - Javascript 显示/隐藏的困难