---
title: "SSH框架对比Jfinal框架-文件上传下载"
author: "vicky_luo"
date: "2014-09-22T18:00:36Z"
tags: []
categories: ["企业架构"]
slug: "ssh-jfinal"
draft: false
lastmod: "2014-09-22T18:22:27Z"
id: 2119262
url: "https://vicky-luo.iteye.com/blog/2119262"
---

**SSH文件下载:**
       <span style="color:red"> File f = new File(filePath);</span>
        if (!f.exists()) {
            response.sendError(404, "File not found!");
            return;
        }
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(<span style="color:red">**f**</span>));
        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<UploadFile> files=null;
	String path="attachFile/";
	try {
		<span style="color:red">files=getFiles(path,1024*1024*10,"UTF-8");</span>
	} catch (Exception e) {
		renderText("总上传附件不能超过10M<a href='history.back();'>返回</a>");
		e.printStackTrace();
	}
        for(int i=0;i<files.size();i++){
	   <span style="color:red"> UploadFile uf=files.get(i);</span>
	    //拿到上传图片的后缀名称
	    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) {
			<span style="color:orange">//删除</span>
                        uf.getFile().delete();
		}
	}
	}
**Jfinal文件下载:**
        //getSession().getServletContext().getRealPath("/")+   flie.getStr("path_");
		    <span style="color:red">File f = new File(basePath);</span>
	    if (!f.exists()) {
                <span style="color:orange">//报错模板</span>
	        setAttr("title", "404");
		     setAttr("message", "File not found!");
	        render(consts.templateErr);
	    }else{
	        <span style="color:red">renderFile(f);</span>
	    }