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

时间:2016-11-22 07:51:21

标签: java json java-api

我正在编写一个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),它会显示一些错误消息,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");

1 个答案:

答案 0 :(得分: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();