Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用apache';HTTP客户端库不工作_Java_Php - Fatal编程技术网

Java 使用apache';HTTP客户端库不工作

Java 使用apache';HTTP客户端库不工作,java,php,Java,Php,此问题与非常类似,但即使是MultipartEntity也无法正确上载文件。以下是客户端的MWE: import java.io.*; import java.util.*; import org.apache.http.entity.mime.*; import org.apache.http.client.*; import org.apache.http.message.*; import org.apache.http.*; import org.apache.http.client.

此问题与非常类似,但即使是
MultipartEntity
也无法正确上载文件。以下是客户端的MWE:

import java.io.*;
import java.util.*;

import org.apache.http.entity.mime.*;
import org.apache.http.client.*;
import org.apache.http.message.*;
import org.apache.http.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.*;
import org.apache.http.impl.client.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;



// Emulate the post behavior of curl in java except post a string.
// https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily

public class Foo{

    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
    // TODO: Fix and test this method.
    private static void PostData() throws Exception {
        String url = "http://localhost/index.php";
        DefaultHttpClient httpclient = new DefaultHttpClient();

        // create the post request.
        HttpPost httppost = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity();

        ContentBody body = new FileBody(new File("/tmp/HelloWorld"),
                org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM);
        entity.addPart("file", body);
        httppost.setEntity(entity);

        // execute request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        InputStream stream = resEntity.getContent();

        System.out.println(response.getStatusLine());
        System.out.println(convertStreamToString(stream));

    }
    public static void main(String[] args) throws Exception {
        PostData();
    }
}
/tmp/HelloWorld'的内容是
HelloWorld`

下面是
index.php
的外观:

<?php
echo empty($_FILES); 
print_r($_REQUEST); 
print_r($_POST); 
print_r($_GET); 
print_r($_FILES); 
>?

我的猜测是我在客户端代码中做了一些愚蠢的事情,但我不确定它是什么。

我深入研究了PHP源代码,显然,
内容配置
行上需要一个
文件名
。因此,在客户机中添加以下代码from可以解决问题

    FormBodyPart customBodyPart = new FormBodyPart("file", body) {
            @Override
            protected void generateContentDisp(final ContentBody body) {
                StringBuilder buffer = new StringBuilder();
                buffer.append("form-data; name=\"");
                buffer.append(getName());
                buffer.append("\"");
                buffer.append("; filename=\"-\"");
                addField(MIME.CONTENT_DISPOSITION, buffer.toString());
            }
    };
    entity.addPart(customBodyPart);
    FormBodyPart customBodyPart = new FormBodyPart("file", body) {
            @Override
            protected void generateContentDisp(final ContentBody body) {
                StringBuilder buffer = new StringBuilder();
                buffer.append("form-data; name=\"");
                buffer.append(getName());
                buffer.append("\"");
                buffer.append("; filename=\"-\"");
                addField(MIME.CONTENT_DISPOSITION, buffer.toString());
            }
    };
    entity.addPart(customBodyPart);