码迷,mamicode.com
首页 > 其他好文 > 详细

SSH 文件下载 代码跟注意点

时间:2018-03-17 12:22:35      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:pos   shm   att   ons   tab   getname   nes   ble   ppi   

该段代码实现了压缩文件,并下载到本地。注意事项:不能在ajax中弹出下载框,否则chrome可以下载。但是IE闪退!下载需要response的输出流写入操作

 

前台代码:// 创建文件夹
    function makeDirs(){
        if(ajList.length==0){
            alertMsg("请添加打印列表");
            return false;
        }
        window.open(‘${pageContext.request.contextPath}/business/dajg_dkb.do?makeDirByGdhsMore&ids=‘+ajList+"&fwlxid="+fwlxid[0]+"&tableName="+tableName);

    }

 

后台代码:

@RequestMapping(params = "makeDirByGdhsMore" )
    @ResponseBody
    public void makeDirByGdhsMore(String ids ,String fwlxid ,String  tableName,HttpServletResponse response){
        SuccessMsg successMsg = new SuccessMsg();
        try {
            successMsg =  dkbdaAjService.dirByGdhsMore(ids,fwlxid,tableName);
            Map <String,Object> map =new HashMap<String,Object>(2);
            if(successMsg.isSuccess()){
                String pathBase = ResourceUtil.getResource("dbconfig.properties").get("dirpath");
                pathBase = pathBase.replace("/","\\");
                HttpSession session = ContextHolderUtil.getRequest().getSession();
                SessionUser sessionUser=(SessionUser)session.getAttribute("login_session_user");
                String name = sessionUser.getUserName(),zipPath =pathBase+"-"+name+"-"+count+".zip";

                CompressUtil.zipNoCryption(pathBase,zipPath);
                count++;
                for(int i=0;i<3;i++){
                    CompressUtil.deleteAllDirs(new File(pathBase),pathBase);
                }
                zipPath = zipPath.replace("\\","/").split("/")[1];
                map.put("dirName",zipPath);
                successMsg.setDataMap(map);
                successMsg.setMsg(successMsg.getMsg()+" 压缩成功!");

                getFile(zipPath,response);

            }
        } catch (Exception e) {
            successMsg.setMsg(e.getMessage());
            successMsg.setSuccess(false);
            e.printStackTrace();
        }
    }

    public  void  getFile(String  fileName, HttpServletResponse response){

        InputStream in =null;
        File file= null;
        try {
            file=new File("D:\\"+fileName);
            if(file.exists()){
                System.out.println(file.getName());
            }
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            in=new FileInputStream(file);
            FileUtil.downLoadZipFile("D:\\"+fileName,fileName, response);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
                file.delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

 

 /**
     * 下载文件
     * @param filePath
     * @param fileName
     * @param response
     */
    public static HttpServletResponse downLoadZipFile(String filePath,String fileName,HttpServletResponse response){
        File file=new File(filePath);
        if(!file.exists())
            return null ;
       return  download(filePath,fileName,response);
    }
    public static HttpServletResponse download(String path, String fileName, HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            try{
                fileName = URLEncoder.encode(fileName, "UTF-8");
            }catch (Exception e){
                e.printStackTrace();
            }

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.setContentType("application/octet-stream; charset=utf-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

SSH 文件下载 代码跟注意点

标签:pos   shm   att   ons   tab   getname   nes   ble   ppi   

原文地址:https://www.cnblogs.com/a6948076/p/8587705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!