ユーザーがサーバー パスとファイル名を入力し、ファイルをダウンロードできるようにする必要があります。
以下のコードを使用して目的を果たしました。
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 で使用するファイルをこのテーブルに挿入するにはどうすればよいですか?
解決策を提案してください。