曾經使用過的文件下載方法

三種方法,都已測試通過。環境: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("下載文件失敗!");
        }
        */

發佈了51 篇原創文章 · 獲贊 5 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章