gpt4 book ai didi

java - 如何使用HttpClient将文件上传到Ubuntu的特定路径

转载 作者:行者123 更新时间:2023-12-02 00:31:57 24 4
gpt4 key购买 nike

我在将文件上传到我的 Ubuntu 服务器时收到 404 错误。

public class Test {

public static void main(String[] args) throws ClientProtocolException, IOException {
// TODO Auto-generated \ stub

CloseableHttpClient httpClient = HttpClients.createDefault();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "password");
System.out.println(creds.getUserPrincipal().getName());
System.out.println(creds.getPassword());
HttpPost uploadFile = new HttpPost("http://10.0.0.45/home");


MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);

// This attaches the file to the POST:
File f = new File("D:\\chinta\\abc.txt");
builder.addBinaryBody(
"file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName()
);

HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
System.out.println("=s========================"+response.getStatusLine());

HttpEntity responseEntity = response.getEntity();
}

}

我在response.getstatus 中收到 404 错误。

最佳答案

我尝试了你的代码,它工作正常。 404通常意味着服务器端不存在请求url。也许你应该检查一下。

关于java - 如何使用HttpClient将文件上传到Ubuntu的特定路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006929/

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