文件以字节数组存在数据库中,下载的时候从服务器端下载到本地:
@RequestMapping("/xxx.action")
public ModelAndView doDownLoadAttachment(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mav = new ModelAndView("ajaxText");
String fileName = request.getParameter("fileName");
String riskId = request.getParameter("riskId");
String arkUser=request.getParameter("arkUser");
byte[] fileAttachment = riskRelationDao.getOne(riskId,
Constants4BizType.RELATION_FILE_ATTACHMENT_BUNINESS_TPYE,
fileName).getAttachment();
// String absFileName=System.getProperty("user.home")+"\\"+fileName;
if(fileAttachment==null){
mav.addObject("responseText","文件中含有特殊字符,获取文件失败!请删除后重新上传");
return mav;
}
boolean retBoolean = DownLoadUtil.downloadFile(fileAttachment,
response, fileName, arkUser);
if (!retBoolean) {
mav.addObject("responseText", "文件流读取异常,下载失败!");
}
return mav;
}
其中,ajaxText.vm 在web-inf下面的velocity或者views目录下面,如果文件目录嵌入更深,比如子在web-inf/velocity/item/ajaxText.vm 目录下面,则是:
ModelAndView mav = new ModelAndView("item/casedetails");
public class DownLoadUtil {
public static boolean downloadFile(byte[] fileAttachment,
HttpServletResponse response, String fileName, String arkUser) {
response.reset();
/*
* String fileType=fileName.substring(fileName.lastIndexOf("."));
* if((".pdf").equals(fileType)){
* response.setContentType("application/pdf;charset=GBK"); }else
* if((".xls").equals(fileType)||(".xlsx").equals(fileType)){
* response.setContentType("application/vnd.ms-excel;charset=GBK");
* }else if((".doc").equals(fileType)||("docx").equals(fileType)){
* response.setContentType("application/msword;charset=GBK"); }else
* if((".jpg").equals(fileType)||("jpeg").equals(fileType)){
* response.setContentType("image/jpeg"); }else
* if(".gif".equals(fileType)){ response.setContentType("image/gif");
* }else if(".png".equals(fileType)){ // image/png
* response.setContentType("application/x-png"); }else
* if(".bmp".equals(fileType)){
* response.setContentType("application/x-bmp"); }
*/
// "x-msdownload"不区分文件格式,统一下载为文件格式,具体的打开编辑器windows会根据文件后缀进行选择,charset='UTF-8'可选项
response.setContentType("application/x-msdownload;charset='UTF-8'");
// response.setCharacterEncoding("gbk");
try {
// html页面不能把utf解析出的"+"号进行转码,也就是不识别+号,所以要进行转码
response.setHeader(
"Content-Disposition",
"attachment;filename="
+ URLEncoder.encode(
("riskm_" + arkUser + "_" + fileName),
"UTF-8").replaceAll("\\+", "%20"));
} catch (Exception e) {
e.printStackTrace();
}
OutputStream out = null;
try {
out = response.getOutputStream();
out.write(fileAttachment);
out.flush();
response.flushBuffer();
} catch (IOException e) {
response.reset();
e.printStackTrace();
return false;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
下载页面嵌入一个下载的js组件Downloadr,
<td colspan="3" style="padding: 8px"> #foreach($fileName in $fileAttachmentsName) <span style="color: green;">$!{fileName} #if("$!{fileName}"!="") <a href="/xxx.action?fileName=$!{fileName}&riskId=$!{riskId}&arkUser=$!{arkUser}" rel="downloadr" title="Downloadr">下载</a> #end </span> <br><br> #end </td>
组件所在的文件夹放在工程的src/main/webapp/source/js 文件夹下,需要具体的组件和邮件给我哦~
<link href="../source/css/download/facebox.css" media="screen" rel="stylesheet" type="text/css"/> <script src="../source/js/download/facebox.js" type="text/javascript"></script> <link href="../source/css/download/downloadr.css" media="screen" rel="stylesheet" type="text/css"/> <script src="../source/js/download/jqbrowser.js" type="text/javascript"></script> <script src="../source/js/download/downloadr.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $('a[rel*=downloadr]').downloadr(); }); </script>