繁体   English   中英

API调用缺少用于文件传输的头信息

[英]API call missing header information for file transfer

我正在编写一个Java代码,它将通过API调用将文件发送到某些URL,但是在URL的GET响应过程中会丢失一些信息,就像我的文件信息丢失一样,它们是display_name,file_type。 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),它会向我显示一些错误消息, 是我对.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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM