How to download a file from the server using javaEE
Tech-Today

How to download a file from the server using javaEE


The following snippet will accept a filename in the server's directory, set it as the content of the FacesContext for the user to download.

public String downloadXMLInvoice(String fileName) {
File file = new File(getXmlInvoiceDir().getAbsolutePath() + File.separator + fileName);

try {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getResponse();
res.setContentType("application/force-download");
res.setContentLength((int) file.length());
res.addHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");

OutputStream out = res.getOutputStream();
InputStream fin = new FileInputStream(file);

byte[] buf = new byte[1024];
int sig = 0;
while ((sig = fin.read(buf, 0, 1024)) != -1) {
out.write(buf, 0, sig);
}

fin.close();
out.flush();
out.close();

context.responseComplete();
} catch (Exception e) {
log.error(Epic failed :-) ", e.getMessage(), file.getAbsolutePath());
}

return null;
}




- How To Download A File In Angular2
There are 2 ways I use to download a file with Angular2 or greater. For this example, we will use a Java REST service. The first approach would be taking advantage of the HTTP download, where the Angular side will just call a URL that will initiate the...

- How To Download A File Using Rest Api
Normally I create an interface when defining a REST api, this is useful when testing using Arquillian. To define a download file api we must define the method below in the interface. @GET @Path("/downloadFile") ActionStatus downloadFile(@QueryParam("file")...

- How To Upload A File In Rest Api
This is how I define the method: @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) ActionStatus uploadFile(@MultipartForm FileUploadForm form); And then the implementation: public ActionStatus uploadFile(FileUploadForm form) { File file...

- How To Generate A Jasper Report In Java
This tutorial will guide you in generating a jasper report in java. You need: 1.) Eclipse 2.) iReport - http://community.jaspersoft.com/project/ireport-designer Steps: 1.) First we need to create a model for our report. For example we have a Student...

- Download Image Given A Url In C# Using The System.net.httpwebrequest Class
Saves image url object into the local machine /// /// Saves the list of url object into the output directory /// /// list of url path /// root directory that will be used for saving internal static void PhotoDownloader(ArrayList aList, string outputDir)...



Tech-Today








.