Advertisement
psi_mmobile

Untitled

Sep 9th, 2020
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1.     // DOWNLOAD DOCUMENT SECTION BEGIN
  2.  
  3.     public void downloadFileListener(FacesContext facesContext, OutputStream outputStream) throws IOException {
  4.         if (null != (String)getSelectedRow(docTable).getAttribute("FilePath") &&
  5.             !((String)getSelectedRow(docTable).getAttribute("FilePath")).isEmpty()) {
  6.             File file = new File((String)getSelectedRow(docTable).getAttribute("FilePath"));
  7.             downloadFile(outputStream, file);
  8.         }
  9.     }
  10.  
  11.     public void downloadFileListenerFromIterator(FacesContext facesContext,
  12.                                                  OutputStream outputStream) throws IOException {
  13.         JUCtrlHierNodeBinding node = (JUCtrlHierNodeBinding)this.iteratorCurrentRow;
  14.  
  15.         Object filePath = node.getAttribute("FilePath");
  16.         if (null != filePath && !((String)node.getAttribute("FilePath")).isEmpty()) {
  17.             File file = new File((String)filePath);
  18.             log.debug(" DOWNLOAD FILE called second ");
  19.             downloadFile(outputStream, file);
  20.         }
  21.     }
  22.  
  23.     private void downloadFile(OutputStream outputStream, File file) throws java.io.IOException {
  24.         FileInputStream fis = null;
  25.         byte[] b;
  26.         try {
  27.             fis = new FileInputStream(file);
  28.  
  29.             int n;
  30.             while ((n = fis.available()) > 0) {
  31.                 b = new byte[n];
  32.                 int result = fis.read(b);
  33.                 outputStream.write(b, 0, b.length);
  34.                 if (result == -1)
  35.                     break;
  36.             }
  37.         } catch (IOException e) {
  38.             log.error(e.getMessage(), e);
  39.             e.printStackTrace();
  40.         } finally {
  41.             fis.close();
  42.             outputStream.flush();
  43.             outputStream.close(); //close added recently
  44.         }
  45.     }
  46.  
  47.     // DOWNLOAD DOCUMENT SECTION END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement