gpt4 book ai didi

java - 使用HttpClient将数据从一台机器上传到另一台机器

转载 作者:行者123 更新时间:2023-11-28 23:41:34 26 4
gpt4 key购买 nike

我正在尝试使用 HttpClient 将文件从我的机器上传到另一台机器

这是我的代码:

package com.mxui;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class SampleFileUpload
{
private static String executeRequest(HttpRequestBase requestBase)
{
String responseString = "";
InputStream responseStream = null;
HttpClient client = new DefaultHttpClient();
try{
HttpResponse response = client.execute(requestBase);
if(response != null)
{
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null)
{
responseStream = responseEntity.getContent();
if (responseStream != null)
{
BufferedReader br = new BufferedReader (new InputStreamReader (responseStream));
String responseLine = br.readLine();
String tempResponseString = "";
while (responseLine != null)
{
tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator");
responseLine = br.readLine();
}
br.close();
if (tempResponseString.length()>0)
{
responseString = tempResponseString;
}
}
}
}
}catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}catch (ClientProtocolException e)
{
e.printStackTrace();
}catch (IllegalStateException e)
{
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}finally
{
if (responseStream != null)
{
try {
responseStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
client.getConnectionManager().shutdown();
return responseString;
}

public String executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription)
{
HttpPost postRequest = new HttpPost(urlString);
try{
MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : ""));
multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName()));
//FileBody fileBody = new FileBody(file, "application/octect-stream");
FileBody fileBody = new FileBody(file, "text/plain");
multiPartEntity.addPart("attachment", fileBody);
postRequest.setEntity(multiPartEntity);
}catch (UnsupportedEncodingException ex)
{
ex.printStackTrace();
}
return executeRequest(postRequest);
}

public static void main(String args[])
{
SampleFileUpload fileUpload = new SampleFileUpload();
File file = new File ("test.txt");
String response = fileUpload.executeMultiPartRequest("http://192.168.2.21:8080/home/user/Desktop/uploaded-data", file, file.getName(), "File Upload test Hydrangeas.jpg description");
System.out.println("Response : "+response);
}
}

但是它的执行和打印响应是空的,它也没有上传请任何人帮忙,我是否遗漏了什么谢谢。

最佳答案

您必须在另一台机器上也有相应的系统。一种接受 POST 请求并将数据存储在系统上。

Apaches Fileupload会在那里帮助你。

关于java - 使用HttpClient将数据从一台机器上传到另一台机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091603/

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