gpt4 book ai didi

java - 使用 apache 的 httpclient 库上传文件不起作用

转载 作者:行者123 更新时间:2023-11-30 04:06:12 26 4
gpt4 key购买 nike

这个问题与 How to upload a file using Java HttpClient library working with 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.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' 的内容为HELLO WORLD`。

这是 index.php 的样子:

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

输出如下所示,这似乎暗示文件内容被发送到 $_REQUEST$_POST,而不是 $_FILES >

1
Array
(
[file] => HELLO WORLD

)
Array
(
[file] => HELLO WORLD

)
Array
(
)
Array
(
)

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

最佳答案

我深入研究了 PHP 源代码,显然 Content-Disposition 行需要一个 filename。因此,添加以下代码,来自this answer ,在客户端解决了问题。

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

关于java - 使用 apache 的 httpclient 库上传文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com