SSH 文件下载 代码跟注意点

Posted 办公室的仙人掌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH 文件下载 代码跟注意点相关的知识,希望对你有一定的参考价值。

该段代码实现了压缩文件,并下载到本地。注意事项:不能在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 文件下载 代码跟注意点的主要内容,如果未能解决你的问题,请参考以下文章

ssh修改默认端口

python pxssh之实现批量下发ssh公钥文件

linux服务器修改ssh默认22端口方法

python练习-(秒转时分秒,时分秒转秒)-对比linux中文件的上次更改时间跟当前时间相差多久。

IDEA + SSH OA 第一天(项目收获:Hibernate XML)

在程序员行业,找师傅跟找对象差不多,这些点你得注意!