Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Attach a file to jira from Java API using a stream

Gil Brown
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 4, 2017

I'm trying to add attachment from input stream or byte array .

 

 

I have the following code:

 

Blob blob = rset.getBlob(12);

int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
blob.free();
String folder ="/home/atlassian/application-data/SBMAttachments/";
File tempFile = new File(folder+filePrefix + fileName);

FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(blobAsBytes);
addAttachmentToJira(tempFile,issue, fileName,user);
public static boolean addAttachmentToJira(File file,Issue issue,String filename,ApplicationUser user) throws Exception{
MimeManager mm = new MimeManager(new FileInputStream(file));

AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager();
CreateAttachmentParamsBean.Builder builder = new CreateAttachmentParamsBean.Builder();
builder.file(file);
builder.filename(file.getName());
builder.contentType(mm.getSuggestedMimeType(filename));
builder.author(user);
builder.issue(issue);
builder.copySourceFile(true);
CreateAttachmentParamsBean bean = builder.build();
com.atlassian.fugue.Either<AttachmentError,ChangeItemBean> result = attachmentManager.tryCreateAttachment(bean);
if (result.isLeft()) {
AttachmentError attachmentError = (AttachmentError) result.left().get();
//LOG.error("AttachmentError '" + attachmentError.getLogMessage());
} else {
ChangeItemBean changeItemBean = (ChangeItemBean) result.right().get();
//attachmentManager.createAttachment(bean);
//LOG.error("created!");
}
return true;
}


 

My idea is to add the attachment without saving the file (to save time).

Is there a way to add the attachment from input stream ?

 

1 answer

1 vote
Gonchik Tsymzhitov
Community Champion
July 22, 2018
Shye Wilkanski
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 22, 2018

Thanks, I already resolved it with the file system .

Gonchik Tsymzhitov
Community Champion
July 23, 2018

May I know the full use case? 

 

Looks like you have developed a custom attachment uploader. 

Is that correct?

Shye Wilkanski
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 23, 2018

Yes, We had old system (SBM) and we needed to migrate the issues from there to our JIRA.

SBM saved the attachments data as BLOB's in the DB and I wanted to take the stream of BLOB and save it directly to the issue.

 

eventually I saved the BLOB on the JIRA machine as Files and then attaching this file to the issue.

Gonchik Tsymzhitov
Community Champion
July 23, 2018

Thank you! 

Suggest an answer

Log in or Sign up to answer