Hello all
this post is about a very simple requirement -file handling (uploading and downloading various types of file) in ADF and it is needed very often to store file in absolute server path (actual path) and download from there
see step by step implementation -
Happy Learning :) Sample ADF Application
Next Post is this series - (Must Read)
Uploading mulitiple files to server path in Oracle ADF using af:inputFile
Uploading and downloading files from database (BLOB) in Oracle ADF
this post is about a very simple requirement -file handling (uploading and downloading various types of file) in ADF and it is needed very often to store file in absolute server path (actual path) and download from there
see step by step implementation -
- I have created a simple table in HR schema to store uploaded file name ,path and content type See sql script for this table-
- Then prepare model using this table and drop on page as af:table, and an af:inputFile to select and upload file is used in page
- Then create a ValueChangeListener on inputFile component to upload file to an actual path on server, and after upload a row is inserted in table to keep record of uploaded files
- Now run application and select files and see-
- See uploaded files in folder
- I have seen that developers often use servlet to download and open file in browser window using HTTP response , but no need to to do this as ADF provides built in component for this <af:fileDownloadActionListener> that automatically generate http response
- Now Upload part is complete , for download functionality added a link in table column and dropped an af:fileDownloadActionListener inside link and set properties for DownloadActionListener
- See the code in Download Listener
- Now run application and see how Upload and download works
CREATE TABLE "FILE_UPD_DWN" ( "FILE_NAME" VARCHAR2(50 BYTE), "PATH" VARCHAR2(100 BYTE), "CONTENT_TYPE" VARCHAR2(500 BYTE) )
Bean Method to Upload File-
/**Method to upload file to actual path on Server*/ private String uploadFile(UploadedFile file) { UploadedFile myfile = file; String path = null; if (myfile == null) { } else { // All uploaded files will be stored in below path path = "D://FileStore//" + myfile.getFilename(); InputStream inputStream = null; try { FileOutputStream out = new FileOutputStream(path); inputStream = myfile.getInputStream(); byte[] buffer = new byte[8192]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); out.close(); } catch (Exception ex) { // handle exception ex.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { } } } //Returns the path where file is stored return path; }
AMImpl method to insert record in table for Uploaded file
/**Method to set file path and name * @param name * @param path */ public void setFileData(String name, String path,String contTyp) { ViewObject fileVo = this.getFileUpdDwn1(); Row newRow = fileVo.createRow(); newRow.setAttribute("FileName", name); newRow.setAttribute("Path", path); newRow.setAttribute("ContentType", contTyp); fileVo.insertRow(newRow); this.getDBTransaction().commit(); fileVo.executeQuery(); }
ValueChangeListener to execute all methods -
/*****Generic Method to Get BindingContainer**/ public BindingContainer getBindingsCont() { return BindingContext.getCurrent().getCurrentBindingsEntry(); } /** * Generic Method to execute operation * */ public OperationBinding executeOperation(String operation) { OperationBinding createParam = getBindingsCont().getOperationBinding(operation); return createParam; } /**Method to Upload File ,called on ValueChangeEvent of inputFile * @param vce */ public void uploadFileVCE(ValueChangeEvent vce) { if (vce.getNewValue() != null) { //Get File Object from VC Event UploadedFile fileVal = (UploadedFile) vce.getNewValue(); //Upload File to path- Return actual server path String path = uploadFile(fileVal); System.out.println(fileVal.getContentType()); //Method to insert data in table to keep track of uploaded files OperationBinding ob = executeOperation("setFileData"); ob.getParamsMap().put("name", fileVal.getFilename()); ob.getParamsMap().put("path", path); ob.getParamsMap().put("contTyp", fileVal.getContentType()); ob.execute(); // Reset inputFile component after upload ResetUtils.reset(vce.getComponent()); } }
/**Method to download file from actual path * @param facesContext * @param outputStream */ public void downloadFileListener(FacesContext facesContext, OutputStream outputStream) throws IOException { //Read file from particular path, path bind is binding of table field that contains path File filed = new File(pathBind.getValue().toString()); FileInputStream fis; byte[] b; try { fis = new FileInputStream(filed); int n; while ((n = fis.available()) > 0) { b = new byte[n]; int result = fis.read(b); outputStream.write(b, 0, b.length); if (result == -1) break; } } catch (IOException e) { e.printStackTrace(); } outputStream.flush(); }
Happy Learning :) Sample ADF Application
Next Post is this series - (Must Read)
Uploading mulitiple files to server path in Oracle ADF using af:inputFile
Uploading and downloading files from database (BLOB) in Oracle ADF