Jersey File Download#

REST API to stream file with StreamingOutput#

public class DownloadService {
	public Response downloadJpgFile() {
		StreamingOutput fileStream = new StreamingOutput() {
			public void write( output)
					throws IOException, WebApplicationException {
				try {
					java.nio.file.Path path = Paths
							.get(Globals.UPLOAD_PATH + "avatar.jpg");
					byte[] data = Files.readAllBytes(path);
				} catch (Exception e) {
					throw new WebApplicationException("File Not Found !!");
		return Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
						"attachment; filename = myfile.jpg")

Jersey file download demo#

  • http://localhost:8080/rest/jersey/download/jpg

  • The filename with which JPG file will be saved, will be what you set in Response.header() method.