i am writing a java code which will send a file to certain URL through a API call, but there is some information miss out during the GET response from the URL, it's seen like my file information missing which are display_name, file_type. The display_name will be the file name of my file
Here are the return JSON data
{
"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"
},
here is my sample java source
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();
}
}
}
if i try to add in .addPart("file", bin) under the HttpEntity class, it's show me some error message, this is my reference link for the .addPart but when i executed the program, my compiler show me this error
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
i also tested this code and no error show up but the display_name still missing
post.addHeader("content-type","application/json");
post.addHeader("Accept","application/json");