将存放在数据库表中的文件流下载下来。
NoticeFile类的定义,主要关注其中的字符流定义格式为private byte[] fileData;
public String downloadFile() throws IOException {
long noticeId = Long.valueOf(request.getParameter("fileId"));
NoticeFile file = noticeFileService.selectByPrimaryKey(noticeId);//从库中查询到表记录数据,此时表中的字节流已经存放在fileData字节数组中
response.setContentType(file.getFileType() + ";charset=utf-8");
String fileName = new String((file.getFileName() + "."
+ file.getFileExt()).getBytes("GBK"),"ISO-8859-1");
response.setHeader("Content-Disposition","attachment;filename="+fileName);
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.getOutputStream().write(file.getFileData());
response.getOutputStream().flush();
response.getOutputStream().close();
return null;
}