SSH框架对比Jfinal框架-文件上传下载
SSH文件下载: File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); byte buf = new byte; int len = 0;
response.reset(); <span style="color:orange">// 非常重要</span>
if (isOnLine) { <span style="color:orange">// 在线打开方式</span>
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
} else { <span style="color:orange">// 纯下载方式</span>
response.setContentType("application/x-msdownload;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName(),"iso-8859-1");
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
Jfinal文件上传: List files=null; String path="attachFile/"; try { files=getFiles(path,1024102410,"UTF-8"); } catch (Exception e) { renderText("总上传附件不能超过10M返回"); e.printStackTrace(); } for(int i=0;i<files.size();i++){ UploadFile uf=files.get(i); //拿到上传图片的后缀名称 String suffix = uf.getFileName(); if(suffix!=null){ int travel_id=travel.getNumber("id").intValue(); String filenames = suffix.split("\."); String suffixName = filenames; String fileName=System.currentTimeMillis()+""+month+"."+suffixName; uf.getFile().renameTo(new File(uf.getSaveDirectory()+fileName)); try { AttachFile attachFile=new AttachFile(); attachFile.set("id", "T_ATTACHFILE_ID.nextval") attachFile.save(); } catch (Exception e) { //删除 uf.getFile().delete(); } } } Jfinal文件下载: //getSession().getServletContext().getRealPath("/")+ flie.getStr("path"); File f = new File(basePath); if (!f.exists()) { //报错模板 setAttr("title", "404"); setAttr("message", "File not found!"); render(consts.templateErr); }else{ renderFile(f); }