SSH框架对比Jfinal框架-文件上传下载

[b]SSH文件下载:[/b]
[color=red] File f = new File(filePath);[/color]
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream([color=red][b]f[/b][/color]));
byte[] buf = new byte[1024];
int len = 0;

response.reset(); [color=orange]// 非常重要[/color]
if (isOnLine) { [color=orange]// 在线打开方式[/color]
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
} else { [color=orange]// 纯下载方式[/color]
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();
[b]Jfinal文件上传:[/b]
List<UploadFile> files=null;
String path="attachFile/";
try {
[color=red]files=getFiles(path,1024*1024*10,"UTF-8");[/color]
} catch (Exception e) {
renderText("总上传附件不能超过10M<a href='history.back();'>返回</a>");
e.printStackTrace();
}
for(int i=0;i<files.size();i++){
[color=red] UploadFile uf=files.get(i);[/color]
//拿到上传图片的后缀名称
String suffix = uf.getFileName();
if(suffix!=null){
int travel_id=travel.getNumber("id").intValue();
String[] filenames = suffix.split("\\.");
String suffixName = filenames[filenames.length-1];
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) {
[color=orange]//删除[/color]
uf.getFile().delete();
}
}
}
[b]Jfinal文件下载:[/b]
//getSession().getServletContext().getRealPath("/")+ flie.getStr("path_");
[color=red]File f = new File(basePath);[/color]
if (!f.exists()) {
[color=orange]//报错模板[/color]
setAttr("title", "404");
setAttr("message", "File not found!");
render(consts.templateErr);
}else{
[color=red]renderFile(f);[/color]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值