我正在创建一个应用程序,我需要在邮件中附加文件,然后将该完整文件作为另一个名为' asana'的门户API的输入发送。我不想在本地保存/下载文件。
我引用了这个site来通过邮件获取附件。 但是,我需要将文件发送到' asana' API没有下载它。 以下是通过API发送文件的asana API代码。
String url = "https://app.asana.com/api/1.0/tasks/"+asanaActivitiesDto.getTaskId()+"/attachments";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("Authorization", "Bearer " + asanaActivitiesDto.getBearerToken());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("task", jsonInput.get("taskId").toString(), ContentType.TEXT_PLAIN);
// now here I actually want file from attachment of the mail
// currently taking file from local for testing
File f = new File(asanaActivitiesDto.getFile());
builder.addBinaryBody(
"file",
//Here, as a second input, I need to set file from the
//attachment of the mail
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName()
);
HttpEntity multipart = builder.build();
post.setEntity(multipart);
HttpResponse response = client.execute(post);
HttpEntity responseEntity = response.getEntity();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
JSONParser parser = new JSONParser();
jsonOutput = (JSONObject) parser.parse(result.toString());
在 new FileInputStream(f)的地方,我需要发送附加到该邮件的文件。有什么方法可以做同样的事情?
答案 0 :(得分:0)
您只需将电子邮件附件中的数据作为InputStream读取即可。尝试类似:
mbp = // the MimeBodyPart containing the attachment
builder.addBinaryBody(
"file",
mbp.getInputStream(),
ContentType.APPLICATION_OCTET_STREAM,
mbp.getFileName()
);