Currently I am using org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput for file upload. I want to replace it with some generic Java EE design. Please suggest possible ways to upload file along with few other form data using Java EE 7 specs.
Asked
Active
Viewed 1,922 times
1
-
I tend to handle file uploads with a regular Servlet myself, still. The servlet 3.0 spec (since JEE6) has added multipart handling support out of the box. Is that within the scope of the question or must it be JAX-RS based? – Gimby Jul 14 '16 at 07:41
-
What we are trying to do is, we are trying to get the file information from the request without using any third party api's. Does you code/implementation do that? If yes, can you please share your code/idea ? – Purnendu Rath Jul 15 '16 at 08:42
-
I take that as a yes. – Gimby Jul 15 '16 at 08:47
-
Possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet) – Gimby Jul 15 '16 at 08:47
1 Answers
-1
The Java EE 7 has focused on RESTful webservices. So to do something corresponding the specs' approach, keep using JAX-RS 2.0 API:
Here you want to want to upload a file to your app => you want to POST a FILE to your JAX-RS Service. Your method can be like this:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
//Your local disk path where you want to store the file
String uploadedFileLocation = "D://uploadedFiles/" + fileDetail.getFileName();
System.out.println(uploadedFileLocation);
// save it
File objFile=new File(uploadedFileLocation);
if(objFile.exists())
{
objFile.delete();
}
try {
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
String output = "File uploaded via JAX-RS based RESTFul Webservice to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
This a Java EE 7 compliant Upload File JAX-RS Service.

Jason Bourne
- 756
- 1
- 14
- 34
-
@FormDataParam is available inside jersey-multipart-api and hence is a third party implementation, I want an implementation based on JAX-RS, using javax.ws api's. Is there any generic spec compliant to both resteasy and jersey? – Purnendu Rath Jun 28 '16 at 13:55
-
Jersey framework is the JAX-RS Reference Implementation. Resteasy is an other implementation of JAX-RS. Resteasy is not handling the MultiPartData differently : http://docs.jboss.org/resteasy/docs/2.3.4.Final/userguide/html/Multipart.html#MultipartFormData If you want to do "portable" code between Jersey & Resteasy, I think this is not possible actually. – Jason Bourne Jun 28 '16 at 14:20
-
@JsonBourne Thats's exactly what I want, I want a code independent of the implementer, code that should run with both RestEasy and Jersey. Portable code as you rightly pointed out. – Purnendu Rath Jun 29 '16 at 04:35