曾经使用过的文件下载方法

三种方法,都已测试通过。环境:weblogic 10

 

 

        SysLog sysLog = new SysLog();
        sysLog.setPath("d:/osclog/F1_logo.jpg");
        sysLog.setName("F1_logo.jpg");

 

 

/*
        // 下载本地文件
        fileName = sysLog.getName(); // 文件名
        try {
        // 读到流中
        InputStream inStream = new FileInputStream(sysLog.getPath());// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=/"" + fileName + "/"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        */




        /*
        File file = new File(sysLog.getPath());
        try {
            InputStream fis = new BufferedInputStream(new FileInputStream(sysLog.getPath()));
            byte[] buffer = new byte[fis.available()];//判断流的长度
            fis.read(buffer);
            fis.close();
           
            response.reset();
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(sysLog.getName().getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("application/octet-stream");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
           
        } catch (IOException e1) {
            System.out.println("error");
            e1.printStackTrace();
        }
        */
       
       
        /*
        File file = new File("d://osclog//wwsdk.log");
        FileInputStream fs = null;
        fileName = "wwsdk.log";
        try {
            fs = new FileInputStream(new File("d:/osclog/wwsdk.log"));
        }catch(Exception e) {
            e.printStackTrace();
        }
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=/"" + fileName + "/"");
        int b = 0;
        try {
            PrintWriter out = response.getWriter();
            while((b=fs.read())!=-1) {
                out.write(b);
            }
            fs.close();
            out.close();
            System.out.println("文件下载完毕.");
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("下载文件失败!");
        }
        */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值