How to download a file using a Java REST service ?
Related Question:
How to zip a folder?
AngularJs / JavaScript to download a file using web service.
Solution:
@GET
@Path("/report/download/file/{filename : .+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFilebyPath(@PathParam("filename") String fileName) {
System.out.println("file location:--- //"+fileName);
return download("//"+fileName);
}
private Response download(String fileName) {
Response response = null;
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
// FileOutputStream fos = null;
// ZipOutputStream zos = null;
//String zipFile = folderPath + fileName + ".zip";
//fileListForFiltering = new ArrayList<String>();
/*String destFolderString = new File(filePath).getName();
String pattern = "_[0-9]+_[0-9]+";
Pattern p = Pattern.compile("_[0-9]+_[0-9]+");
//byte[] buffer = new byte[1024];
// Zip Utility
/* File[] files= file.listFiles();
for (File filename : files){
if(filename.toString().contains(businessDate)){
Matcher mm = p.matcher(filename.toString());
if (!mm.find()) {
this.fileListForFiltering.add(filename.getName());
}
}
}*/
// File zipfile= zipIt(zipFile, filePath, destFolderString);
// File zipfile = new File(zipFile) ;
File file = new File(fileName);
if (file.exists()) // if (zipfile.exists())
{
ResponseBuilder builder = Response.ok(file);
builder.header("Content-Disposition", "attachment; filename=" + file.getName());
response = builder.build();
long file_size = file.length();
LOG.info(String.format("Inside downloadFile==> fileName: %s, fileSize: %s bytes",
file, myFormat.format(file_size)));
} else {
LOG.error(String.format("Inside downloadFile==> FILE NOT FOUND: fileName: %s",
file));
response = Response.status(404).
entity("FILE NOT FOUND: " + file).
type("text/plain").
build();
}
return response;
}
AngularJs or javascript
How to zip a folder?
AngularJs / JavaScript to download a file using web service.
Solution:
@GET
@Path("/report/download/file/{filename : .+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFilebyPath(@PathParam("filename") String fileName) {
System.out.println("file location:--- //"+fileName);
return download("//"+fileName);
}
private Response download(String fileName) {
Response response = null;
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
// FileOutputStream fos = null;
// ZipOutputStream zos = null;
//String zipFile = folderPath + fileName + ".zip";
//fileListForFiltering = new ArrayList<String>();
/*String destFolderString = new File(filePath).getName();
String pattern = "_[0-9]+_[0-9]+";
Pattern p = Pattern.compile("_[0-9]+_[0-9]+");
//byte[] buffer = new byte[1024];
// Zip Utility
/* File[] files= file.listFiles();
for (File filename : files){
if(filename.toString().contains(businessDate)){
Matcher mm = p.matcher(filename.toString());
if (!mm.find()) {
this.fileListForFiltering.add(filename.getName());
}
}
}*/
// File zipfile= zipIt(zipFile, filePath, destFolderString);
// File zipfile = new File(zipFile) ;
File file = new File(fileName);
if (file.exists()) // if (zipfile.exists())
{
ResponseBuilder builder = Response.ok(file);
builder.header("Content-Disposition", "attachment; filename=" + file.getName());
response = builder.build();
long file_size = file.length();
LOG.info(String.format("Inside downloadFile==> fileName: %s, fileSize: %s bytes",
file, myFormat.format(file_size)));
} else {
LOG.error(String.format("Inside downloadFile==> FILE NOT FOUND: fileName: %s",
file));
response = Response.status(404).
entity("FILE NOT FOUND: " + file).
type("text/plain").
build();
}
return response;
}
AngularJs or javascript
Comments
Post a Comment