Skip to content

Instantly share code, notes, and snippets.

@nate-getch
Forked from aitoroses/JerseyFileUpload.java
Created March 30, 2018 02:59
Show Gist options
  • Save nate-getch/347a30e65ae6812835d1c2406519ea67 to your computer and use it in GitHub Desktop.
Save nate-getch/347a30e65ae6812835d1c2406519ea67 to your computer and use it in GitHub Desktop.
Jersey Upload & Download Examples
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();
}
}
}
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();
}
}
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment