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

Hi,

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 java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.sun.jersey.multipart.FormDataParam;

@Path("/upload")
public class RESTService {

public RESTService() {}

@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.MULTIPART_FORM_DATA)
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 = input.read(buffer))) {
wr.write(buffer, 0, n);
count += n;
}
wr.close();


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:

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

[there is the file content]

------------------------------437c56a39444--

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:

 <dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
<scope>provided</scope>
</dependency>

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(InjectableValuesProvider.java:94)
...
Caused by: java.lang.NullPointerException
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:86)
... 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

0 vote

Hi Aleksander - Welcome to the community.

Since your question is development related, you can also try asking this over at: https://community.developer.atlassian.com (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: https://community.atlassian.com/t5/Feedback-Forum-articles/The-Atlassian-Developer-Community/ba-p/459061

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

Sam Hall Community Champion Apr 10, 2017

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.

Sam Hall Community Champion Apr 10, 2017

Here's a link to the question asked over on the Dev Community, in case people want to follow the trail: https://community.developer.atlassian.com/t/how-to-write-a-rest-plugin-which-handles-a-file-upload/2296

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,316 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot