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

Aleksander Kocieniewski April 9, 2017

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.

 

2 answers

0 votes
Hua Soon SIM _Akeles_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 23, 2020
0 votes
Sam Hall
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 10, 2017

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

Aleksander Kocieniewski April 10, 2017

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

Sam Hall
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 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

Like # people like this

Suggest an answer

Log in or Sign up to answer