我要求允许用户输入服务器路径和文件名并下载文件。
我使用下面的代码来达到目的:
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();
}
}
问题是,任何大于 48KB 的文件在下载时都会在其中附加一个额外的行。
开发者指南中提到的messageDownload项的使用方法不够清晰。它提到了一个VO。这个VO的查询应该是什么?我们如何在这个表中插入文件以在这个 VO 中使用?
请提出解决方案。