Jersey File download
With file Stream
@GET
@Path("/download")
public Response downloadBatchFile()
{
StreamingOutput fileStream = new StreamingOutput()
{
@Override
public void write(java.io.OutputStream output) throws IOException, WebApplicationException
{
try
{
java.nio.file.Path path = Paths.get("filepath"); //absolute path
byte[] data = Files.readAllBytes(path);
output.write(data);
output.flush();
}
catch (Exception e)
{
throw new WebApplicationException("File Not Found !!");
}
}
};
return Response
.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = filename")
.build();
With filepath
@GET
@Path("/download")
public Response downloadBatchFile()
{
File file = new File("filepath"); //absolute path
return Response
.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = filename") //filename with file format
.build();
}
@GET
@Path("/download")
public Response downloadBatchFile()
{
StreamingOutput fileStream = new StreamingOutput()
{
@Override
public void write(java.io.OutputStream output) throws IOException, WebApplicationException
{
try
{
java.nio.file.Path path = Paths.get("filepath"); //absolute path
byte[] data = Files.readAllBytes(path);
output.write(data);
output.flush();
}
catch (Exception e)
{
throw new WebApplicationException("File Not Found !!");
}
}
};
return Response
.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = filename")
.build();
With filepath
@GET
@Path("/download")
public Response downloadBatchFile()
{
File file = new File("filepath"); //absolute path
return Response
.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = filename") //filename with file format
.build();
}
댓글
댓글 쓰기