I have a requirement to allow user to enter server path and file name and download file.
I used the below code to serve the purpose:
HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse();
File fileToDownload = null;
try
{
fileToDownload = new File(filePath);
}
catch (Exception e)
{
throw new OAException("Invalid File Path or file does not exist.");
}
response.setContentType(fileType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentLength((int)fileToDownload.length());
InputStream in = null;
ServletOutputStream outs = null;
try
{
outs = response.getOutputStream();
in = new BufferedInputStream(new FileInputStream(fileToDownload));
int ch;
while ((ch = in.read()) != -1)
{
outs.write(ch);
}
}catch (IOException e)
{
// TODO
e.printStackTrace();
}finally
{
try
{
outs.flush();
outs.close();
if (in != null)
{
in.close();
}
}catch (Exception e)
{
e.printStackTrace();
}
}
The issue with this is, that any file greater than 48KB, when downloaded appends an extra line in it.
The method for using messageDownload item mentioned in Developer's guide is not clear enough. It mentions a VO. What should be the query of this VO? How do we insert file in this table to be used in this VO?
Please suggest a solution.
I finally resolved my query.
I have used messageDownload Bean to serve the purpose.
View instance: VO with query for LOB table
View attribute: VO Attribute having file name
MIME type : Either hard-code it to 'text/html' or make it dynamic as in DevGuide
File View Attr: VO Attribute having file data
Data type: CLOB
I have added an input field for file path and file name. Then, on click on GO button, the step 2 Procedure is called and the messageDownload item VO is executed accordingly.
Please let me know, in case of any queries.