Created
January 25, 2016 09:42
-
-
Save aitoroses/4f7a2b197b732a6a691d to your computer and use it in GitHub Desktop.
Jersey Upload & Download Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodegeeks.enterprise.rest.jersey; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import com.sun.jersey.core.header.FormDataContentDisposition; | |
import com.sun.jersey.multipart.FormDataParam; | |
@Path("/files") | |
public class JerseyFileUpload { | |
private static final String SERVER_UPLOAD_LOCATION_FOLDER = "C://Users/nikos/Desktop/Upload_Files/"; | |
/** | |
* Upload a File | |
*/ | |
@POST | |
@Path("/upload") | |
@Consumes(MediaType.MULTIPART_FORM_DATA) | |
public Response uploadFile( | |
@FormDataParam("file") InputStream fileInputStream, | |
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) { | |
String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName(); | |
// save the file to the server | |
saveFile(fileInputStream, filePath); | |
String output = "File saved to server location : " + filePath; | |
return Response.status(200).entity(output).build(); | |
} | |
// save uploaded file to a defined location on the server | |
private void saveFile(InputStream uploadedInputStream, | |
String serverLocation) { | |
try { | |
OutputStream outpuStream = new FileOutputStream(new File(serverLocation)); | |
int read = 0; | |
byte[] bytes = new byte[1024]; | |
outpuStream = new FileOutputStream(new File(serverLocation)); | |
while ((read = uploadedInputStream.read(bytes)) != -1) { | |
outpuStream.write(bytes, 0, read); | |
} | |
outpuStream.flush(); | |
outpuStream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodegeeks.enterprise.rest.jersey; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import com.sun.jersey.core.header.ContentDisposition; | |
import com.sun.jersey.multipart.FormDataBodyPart; | |
import com.sun.jersey.multipart.FormDataMultiPart; | |
@Path("/files") | |
public class JerseyFileUpload { | |
private static final String SERVER_UPLOAD_LOCATION_FOLDER = "C://Users/nikos/Desktop/Upload_Files/"; | |
/** | |
* Upload a File | |
*/ | |
@POST | |
@Path("/upload") | |
@Consumes(MediaType.MULTIPART_FORM_DATA) | |
public Response uploadFile(FormDataMultiPart form) { | |
FormDataBodyPart filePart = form.getField("file"); | |
ContentDisposition headerOfFilePart = filePart.getContentDisposition(); | |
InputStream fileInputStream = filePart.getValueAs(InputStream.class); | |
String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName(); | |
// save the file to the server | |
saveFile(fileInputStream, filePath); | |
String output = "File saved to server location using FormDataMultiPart : " + filePath; | |
return Response.status(200).entity(output).build(); | |
} | |
// save uploaded file to a defined location on the server | |
private void saveFile(InputStream uploadedInputStream, String serverLocation) { | |
try { | |
OutputStream outpuStream = new FileOutputStream(new File( | |
serverLocation)); | |
int read = 0; | |
byte[] bytes = new byte[1024]; | |
outpuStream = new FileOutputStream(new File(serverLocation)); | |
while ((read = uploadedInputStream.read(bytes)) != -1) { | |
outpuStream.write(bytes, 0, read); | |
} | |
outpuStream.flush(); | |
outpuStream.close(); | |
uploadedInputStream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.avaldes; | |
import java.io.File; | |
import java.text.NumberFormat; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.QueryParam; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.core.Response.ResponseBuilder; | |
import org.apache.log4j.Logger; | |
@Path("/files") | |
public class RestFileDownloadExample { | |
static Logger logger = Logger.getLogger(RestFileDownloadExample.class); | |
private static final String api_version = "1.01A rev.1"; | |
private static final String FILE_FOLDER = "c://tmp/"; | |
@Path("/version") | |
@GET | |
@Produces(MediaType.TEXT_HTML) | |
public String returnVersion() { | |
return "<p>Version: " + api_version + "</p>"; | |
} | |
@GET | |
@Path("/download") | |
@Produces(MediaType.APPLICATION_OCTET_STREAM) | |
public Response downloadFilebyQuery(@QueryParam("filename") String fileName) { | |
return download(fileName); | |
} | |
@GET | |
@Path("/download/{filename}") | |
@Produces(MediaType.APPLICATION_OCTET_STREAM) | |
public Response downloadFilebyPath(@PathParam("filename") String fileName) { | |
return download(fileName); | |
} | |
private Response download(String fileName) { | |
String fileLocation = FILE_FOLDER + fileName; | |
Response response = null; | |
NumberFormat myFormat = NumberFormat.getInstance(); | |
myFormat.setGroupingUsed(true); | |
// Retrieve the file | |
File file = new File(FILE_FOLDER + fileName); | |
if (file.exists()) { | |
ResponseBuilder builder = Response.ok(file); | |
builder.header("Content-Disposition", "attachment; filename=" + file.getName()); | |
response = builder.build(); | |
long file_size = file.length(); | |
logger.info(String.format("Inside downloadFile==> fileName: %s, fileSize: %s bytes", | |
fileName, myFormat.format(file_size))); | |
} else { | |
logger.error(String.format("Inside downloadFile==> FILE NOT FOUND: fileName: %s", | |
fileName)); | |
response = Response.status(404). | |
entity("FILE NOT FOUND: " + fileLocation). | |
type("text/plain"). | |
build(); | |
} | |
return response; | |
} | |
} |
the sun/com.sun are the proposal for future java official implementation. they are not officially normalized. for jersey they are transfer in javax.ws.rs, then now they will be stable and official.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've seen numerous Jersey examples across the Internet showing the same usage involving com.sun.* packages. I am confused by the inclusion. My understanding has always been that sun./com.sun. are internal packages that are subject to removal, modification, etc. without notice, yet, I see this particular usage in many examples. Is there no way to implement this functionality using non-proprietary objects?
import **com.sun.**jersey.core.header.ContentDisposition;
import **com.sun.**jersey.multipart.FormDataBodyPart;
import **com.sun.**jersey.multipart.FormDataMultiPart;