gpt4 book ai didi

json - API 调用缺少文件传输的 header 信息

转载 作者:行者123 更新时间:2023-12-01 09:13:45 30 4
gpt4 key购买 nike

我正在编写一个java代码,它将通过API调用将文件发送到某个URL,但是在URL的GET响应期间遗漏了一些信息,它看起来像我的文件信息缺少显示名称、文件类型。 display_name 将是我的文件的文件名

这是返回的 JSON 数据

{  
   "data_id":"55229f05ab534b08b369c324311e2c99",
   "file_info":{
      "display_name":"",
      "file_size":254,
      "file_type":"Not available",
      "file_type_description":"Not available",
      "md5":"8a0c92123d8ffefd95aa1d3dd239c3f7",
      "sha1":"1cfd579d81df680b64e2127296aac55566b95b59",
      "sha256":"a86758bed1a99e12d301fd8bc90749bef89685b9a9c93ad7fa6ee832cb6a7d4e",
      "upload_timestamp":"2016-11-22T05:12:42.374Z"
   },



这是我的示例java源

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.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class SentFile {

public static void main(String [] args) throws ClientProtocolException, IOException
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.0.25:8008/file");
// File file = new File("testScanFile.txt");
//FileInputStream fileInputStream = new FileInputStream(file);
FileBody bin = new FileBody(new File("testScanFile.txt"));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("bin", bin)
// .addPart("file",bin);
.build();
post.addHeader("content-type","application/json");
post.addHeader("Accept","application/json");
post.setEntity(reqEntity);
//InputStream is = new FileInputStream(file);
// post.setEntity(new FileEntity(file));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
// System.out.println(line);
PrintStream ps = new PrintStream(new FileOutputStream("data_id.txt"));
ps.print(line);
ps.close();
}
}

}



如果我尝试在 HttpEntity 类下添加 .addPart("file", bin) ,它会显示一些错误消息,this是我的 .addPart 引用链接,但是当我执行该程序时,我的编译器向我显示此错误

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Type mismatch: cannot convert from MultipartEntityBuilder to HttpEntity
Syntax error on token ".", delete this token
The method build() is undefined for the type SentFile




我也测试了这段代码,没有出现错误,但 display_name 仍然丢失

post.addHeader("content-type","application/json");
post.addHeader("Accept","application/json");

最佳答案

您确实应该发布服务器 API 定义,但是此示例显示了 Apache Fluent API 中的功能。 :

MultipartEntityBuilder.create()
.addBinaryBody("bin" // Important! Defined by the server
, new File("testScanFile.txt") // Not important, user defined
, ContentType.APPLICATION_JSON // Maybe ignored. Depends
, "testScanFile.txt" // User defined
).build();

关于json - API 调用缺少文件传输的 header 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40736428/

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