以下为java处理前台的下载指定远程主机文件的请求代码

    /**
     * 下载远程主机文件
     * @param dataServerIp 服务器IP
     * @param dataServerUsername 服务器用户名
     * @param dataServerPassword 服务器登录密码
     * @param srcFile 要下载的文件路径
     * @param response response
     */
    public static ResultBO downLoadFile(String dataServerIp, String dataServerUsername, String dataServerPassword, String srcFile, HttpServletResponse response) {
        ResultBO resultBO = new ResultBO();
        Connection conn = null;
        try {
            conn = login(dataServerIp, dataServerUsername, dataServerPassword);
        } catch (Exception e) {
            String msg = String.format("login to remote pc fail,ip:%s, username:%s,password:%s, srcFile:%s", dataServerIp, dataServerUsername, dataServerPassword,srcFile);
            logger.error(msg, e);
            resultBO.setCode(ResultEnum.FAILURE.getValue());
            resultBO.setMessage(msg);
            return resultBO;
        }
        Session session = null;
        SCPClient client =null;
        try {
            session=conn.openSession();
            client=conn.createSCPClient();
            return download(srcFile, session, client, response);
        } catch (IOException e1) {
            String msg = String.format("Download file fail,ip:%s, username:%s,password:%s, srcFile:%s", dataServerIp, dataServerUsername, dataServerPassword,srcFile);
            logger.error(msg, e1);
            resultBO.setCode(ResultEnum.FAILURE.getValue());
            resultBO.setMessage(msg);
            return resultBO;
        }finally {
            if (session!=null){
                session.close();
            }
            if (conn!=null){
                conn.close();
            }
        }
    }


    /**
     * 下载文件
     * @param srcFile 文件路径
     * @param sessionSsh sessionSsh
     * @param client client
     * @param response response
     * @return 下载结果
     */
    private static ResultBO download(String srcFile, Session sessionSsh, SCPClient client, HttpServletResponse response){
        ResultBO resultBO = new ResultBO();
        InputStream stdout = null;
        InputStream stderr = null;
        BufferedReader errBr = null;
        SCPInputStream scpInputStream = null;
        OutputStream out = null;
        try {
            //获取文件名称
            String filename=srcFile.substring(srcFile.lastIndexOf("/")+1);
            //获取文件所在目录
            String src=srcFile.substring(0, srcFile.lastIndexOf("/"));
            String cmdGet = "tar -zcvf " + srcFile + ".tar.gz " + filename;
            // 执行压缩命令
            sessionSsh.execCommand("cd "+src+";"+cmdGet);
            // 等待执行结束,最多等待三秒
            sessionSsh.waitForCondition(ChannelCondition.EXIT_STATUS | ChannelCondition.EXIT_SIGNAL, defaultSessionOutTime);
            if (sessionSsh.getExitStatus() == null){
                //三秒没有执行完直接返回
                String msg = String.format("The command---%s-- took more than %s milliseconds to execute. Maybe the file you want to download is too large.", "cd "+src+";"+cmdGet, defaultSessionOutTime);
                resultBO.setCode(ResultEnum.FAILURE.getValue());
                resultBO.setMessage(msg);
                return resultBO;
            }
            if (!EXIT_STATUS_SUCCESS.equals(sessionSsh.getExitStatus())){
                //执行异常
                stderr = new StreamGobbler(sessionSsh.getStderr());
                errBr = new BufferedReader(new InputStreamReader(stderr));
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("exitStatus: " + sessionSsh.getExitStatus());
                while (true) {
                    String line = errBr.readLine();
                    if (line == null)
                        break;
                    stringBuffer.append("\r\n"+line);
                }
                resultBO.setCode(ResultEnum.FAILURE.getValue());
                resultBO.setMessage(stringBuffer.toString());
                return resultBO;
            }
            // 下载文件
            scpInputStream = client.get(src+"/"+filename+".tar.gz");
            response.setHeader("content-disposition", "attachment;filename="+filename+".tar.gz");
            out = response.getOutputStream();
            byte[] b = new byte[4096];
            int i;
            while ((i = scpInputStream.read(b)) != -1){
                out.write(b,0, i);
            }
            resultBO.setCode(ResultEnum.SUCCESS.getValue());
            return resultBO;
        } catch (Exception e) {
            String msg = String.format("Download file fail,srcFile:%s", srcFile);
            logger.error(msg, e);
            resultBO.setCode(ResultEnum.FAILURE.getValue());
            resultBO.setMessage(msg);
            return resultBO;
        } finally {
            if (stdout!=null){
                try {
                    stdout.close();
                } catch (IOException e) {
                    logger.error("Close stdout inputStream error.",e);
                }
            }

            if (errBr!=null){
                try {
                    errBr.close();
                } catch (IOException e) {
                    logger.error("Close error bufferReader error.",e);
                }
            }
            if (scpInputStream!=null){
                try {
                    scpInputStream.close();
                } catch (IOException e) {
                    logger.error("Close scpInputStream error.",e);
                }
            }
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    logger.error("Close outputStream error.", e);
                }
            }
        }
    }