【问题标题】:API call missing header information for file transferAPI 调用缺少文件传输的标头信息
【发布时间】:2017-04-05 19:23:11
【问题描述】:

我正在编写一个 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");

【问题讨论】:

    标签: json java


    【解决方案1】:

    您确实应该发布服务器 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();
    

    【讨论】:

    • 我测试了您提供的代码,它运行没有错误,但我的显示名称仍然为空
    • 请发布服务器 API 规范
    • 你能给我提供服务器 API 规范的链接吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多