How to write a REST plugin which handles a file upload? Edited


I am trying to write a plugin with REST which uploads a file. All I want now is just to receive a file from client and save it on disk. My code is for now:



import com.sun.jersey.multipart.FormDataParam;

public class RESTService {

public RESTService() {}

@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getMessage(@FormDataParam("file") InputStream uploadedInputStream) throws IOException {

String uploadedFileLocation = "/destination/file.xls";

OutputStreamWriter wr = new FileWriter(new File(uploadedFileLocation));
InputStreamReader input = new InputStreamReader(uploadedInputStream);
char[] buffer = new char[1024 * 4];
long count = 0;
int n = 0;
while (-1 != (n = {
wr.write(buffer, 0, n);
count += n;

String output = "File uploaded to : " + uploadedFileLocation;

return Response.status(200).entity(output).build();

Client sends the file by curl execution:

  curl -D- -u user:pswd -X POST -H "X-Atlassian-Token: no-check" -F "file=@file.xls" http://jira:8080/rest/uploadRestPlugin/1.0/upload

The problem is that, the file saved on disk has got additional headers and a footer:

Content-Disposition: form-data; name="file"; filename="file.xls"
Content-Type: text/plain

[there is the file content]


How could I retreive a file from InputStream without additional info? In text files it isn't a big problem, but other files cannot be edited (like .xls) by cutting them in a simple way.

I use this library in pom:


When I add a parameter: @FormDataParam("file") FormDataContentDisposition formDataContentDisposition I got the error:

[c.a.p.r.c.error.jersey.ThrowableExceptionMapper] Uncaught exception thrown by REST service: Exception obtaining parameters
com.sun.jersey.api.container.ContainerException: Exception obtaining parameters
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(
Caused by: java.lang.NullPointerException
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(
... 208 more

Trying to use (adding dependency in pom) other plugins for REST technologies like:
- com.sun.jersey.multipart.MultiPart 
- org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput

give always stacktrace:

com.atlassian.util.concurrent.LazyReference$InitializationException: com.sun.jersey.spi.inject.Errors$ErrorMessagesException

Please help me.


1 answer

Hi Aleksander - Welcome to the community.

Since your question is development related, you can also try asking this over at: (if you haven't already).

There are a lot of helpful and experienced people over there, so you might get a quicker response.

You can read more about the Developer Community in this article:

Thank you for your reply. I didn't know about forum dedicated for developers. I created a thread there as you suggested.

No problem. Atlassian decided to split, but sadly it's not obvious where people should go to post questions of this nature.

I'd post an answer here as well if I can help, but couldn't in this case.

Someone might reply here too.

Here's a link to the question asked over on the Dev Community, in case people want to follow the trail:

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,124 views 4 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you