javaweb文件上传及下载

本文介绍了使用JFinal框架进行文件上传和下载的方法。详细展示了上传文件的控制器代码及FileService类实现,同时提供了前端HTML表单用于文件上传。此外,还给出了两种下载文件的方式,一种是通用的下载方法,另一种则是专门针对JFinal框架的简化方式。

这里的上传是针对jfinal的


代码如下:

public void uploadFile(){
	UploadFile uploadFile=this.getFile();
	String fileName=uploadFile.getOriginalFileName();
		
	File file=uploadFile.getFile(); 
	FileService fs=new FileService();    
        File t=new File("D:\\桌面\\"+UUID.randomUUID().toString());
   
        try {
            t.createNewFile();
            fs.fileChannelCopy(file, t);
            setAttr("tip",tip);
        } catch (IOException e) {
           e.printStackTrace();
        }
        file.delete();
        redirect("/jf/platform/projectdeclareperson");
	}
对应的Fileservice这个类与文件复制方法fileChannelCopy:
public class FileService {
	public void fileChannelCopy(File s, File t) {

        FileInputStream fi = null;

        FileOutputStream fo = null;

        FileChannel in = null;

        FileChannel out = null;
        try {

            fi = new FileInputStream(s);
            fo = new FileOutputStream(t);
            in = fi.getChannel();// 得到对应的文件通道
            out = fo.getChannel();// 得到对应的文件通道

            in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道

        } catch (IOException e) {
            e.printStackTrace();

        } finally {

            try {

                fi.close();
                in.close();
                fo.close();
                out.close();

            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
}


上传前台代码:
<form id="splitPage" class="form-horizontal" action="${cxt!}/jf/platform/projectdeclareperson/uploadFile"  enctype="multipart/form-data" method="post">
    <input type="file" name="file"/>
    <input type="submit"/>
</form>



这个是针对一般下载:

public void download1() throws Exception{
		String cxt =PathKit.getWebRootPath();
		
		ProjectDeclarePerson person = ProjectDeclarePerson.dao.findById(getPara());
		String realname =person.getStr("realname");
		File file =new File(cxt+"/WEB-INF/files/upload/"+realname);
		
		this.getResponse().setContentType("application/x-msdownload");
		this.getResponse().setHeader("Content-Disposition", "attachment;filename="+cxt+"/WEB-INF/files/upload/"+realname);
		InputStream inputStream = new FileInputStream(file);
		ServletOutputStream outputStream = this.getResponse().getOutputStream();
		
		byte b[]=new byte[1024];
		int n;
		while((n=inputStream.read(b))!=-1){
			outputStream.write(b,0,n);
		}
		outputStream.close();
		inputStream.close();
	}

针对jfinal的下载:

public void download() throws ServletException, IOException {
		
		String ids =getPara();
		String img =PathKit.getWebRootPath();
		File file =new File(img+"/index.txt");
		
		renderFile(file);
		
	}



下载前台代码:

<a href="/jf/platform/projectdeclareperson/download/${escapeXml(projectdeclareperson.ids!)}">下载哦</a>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值