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.