Create Directory Path under the Properties File:
Under this file path keep some pdf or tif documents for reading and downloading.
Application.properties:
NEW_FILE_PATH = c:/Files
Add below code in your java file:
String filesPath = resourceBundle.getString("NEW_FILE_PATH");
StringBuilder stringBuilder = new StringBuilder();
File dir = new File(filesPath);
String[] filenames = null;
if (dir != null && dir.listFiles()!= null) {
filenames= dir.list();
for(int i=0;i<filenames.length;i++){
fileList.add(filenames[i]);
}
}
Once Get the filenames from Directory We need to show the files in the frontEnd, So:
Add the Div to StringBuilder and construct the file to showing on UI.
if(filenames != null && filenames.length > 0)
{
stringBuilder.append("<div class=\"file_display\">")
.append("<div class=\"files\">");
for(int i=0;i<fileList.size();i++) {
String fileName = "";
fileName = fileList.get(i);
System.out.println("fileName::"+fileName);
checkPDFFile(fileName,stringBuilder,auditLoanCaseID);
}
stringBuilder.append("</div></div>");
}
return stringBuilder.toString();
private void checkPDFFile(String fName, StringBuilder sb,String auditLoanCaseID)
{
if(fName.endsWith(".PDF") || fName.endsWith(".pdf")){
log.info("Checking the File extension ::" + fName);
sb.append(HREF);
sb.append("#");
sb.append("\" ");
sb.append(" onClick=\"javascript:downloadLoanCaseDoc('"+fName+"','"+auditLoanCaseID+"' )\">");
sb.append("<img src=\"images/PDF_doc.png\"><span>");
sb.append(fName);
sb.append(SPAN);
log.info("final fName value :::"+fName);
log.info("final fName value :::"+sb);
}
else{
log.info("Checking the File extension in Other Files::" + fName);
sb.append(HREF);
sb.append("#");
sb.append("\" ");
sb.append(" onClick=\"javascript:downloadLoanCaseDoc('"+fName+"','"+auditLoanCaseID+"')\">");
sb.append("<img src=\"images/images.png\"><span>");
sb.append(fName);
sb.append(SPAN);
}
}
Now Write Script method and call the action class for downloading the file.
function downloadLoanCaseDoc(docName, loanCaseId) {
var url = "downloadLoancaseIdDocuments.action?docName="+docName;
window.open(url,'edit','scrollbars=yes,width=415,height=365');
}
Action class :
public void downloadLoancaseIdDocuments(){
HttpServletResponse httpRes = ServletActionContext.getResponse();
ResourceBundle resourceBundle = ResourceBundle.getBundle("resource.application");
response.setContentType("application/octet-stream");
response.setHeader("Cache-control","public");
response.setHeader("Content-Disposition","inline; filename="+docName);
ServletOutputStream serveltOutputStream = null;
File file = null;
FileInputStream fis = null;
int length = 0;
try {
serveltOutputStream = httpRes.getOutputStream();
String filesPath = resourceBundle.getString("NEW_FILE_PATH");
file = new File(filesPath);
fis = new FileInputStream(file);
log.info("fis.available()" + fis.available());
byte[] buff = new byte[Constants.BYTES];
int read = 0;
//read file contents
while((read = fis.read(buff))!= -1){
serveltOutputStream.write(buff,0, read);
}
serveltOutputStream.flush();
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
httpRes.flushBuffer();
serveltOutputStream.close();
log.info("downloadLoanCaseIdDocuments Ends....");
} catch (Exception e2) {
// TODO: handle exception
}
}
}