sftp连接--文件下载

来源:互联网 发布:linux yum安装ant 编辑:程序博客网 时间:2024/05/18 00:34

public class TftpUtil {
private Log log = LogFactory.getLog(this.getClass());
private ChannelSftp sftp;
private Channel channel;
private Session session;

/** * @param args * @throws InterruptedException * @throws ParseException * @throws IOException */public TftpUtil() {

// sftp = connect(host, port , username, password);
// sftp.disconnect();
}

/** * 连接sftp服务器 *  * @param host *            主机 * @param port *            端口 * @param username *            用户名 * @param password *            密码 * @return */public ChannelSftp connect(String host, int port, String username,        String password) {    ChannelSftp sftp = null;    try {        JSch jsch = new JSch();        jsch.getSession(username, host, port);        session = jsch.getSession(username, host, port);        log.info("Session created.");        session.setPassword(password);        Properties sshConfig = new Properties();        sshConfig.put("StrictHostKeyChecking", "no");        session.setConfig(sshConfig);        session.connect();        log.info("Session connected.");        log.info("Opening Channel.");        channel = session.openChannel("sftp");        channel.connect();        sftp = (ChannelSftp) channel;        log.info("Connected to " + host + ".");    } catch (Exception e) {        e.printStackTrace();    }    return sftp;}public void uploadFile(String ftpPath, String fileName, String localPath) {    try {        sftp.get(ftpPath + fileName, fileName);        sftp.getInputStream();    } catch (Exception e) {        e.printStackTrace();        log.info("download error.");    } finally {        sftp.quit();        channel.disconnect();        session.disconnect();        sftp.disconnect();    }}/** * download 从ftp下载文件到本地 *  * @throws java.lang.Exception * @return * @param newfilename *            本地生成的文件名 * @param filename *            服务器上的文件路径+文件名 */public long ftpdownload(String filename, HttpServletResponse response)        throws Exception {    long result = 0;    InputStream is = null;    OutputStream out = null;    try {        is=sftp.get(filename);        out = response.getOutputStream();        byte[] bytes = new byte[1024];        int c;        while ((c = is.read(bytes)) != -1) {            out.write(bytes, 0, c);            result = result + c;        }        out.flush();    } catch (IOException e) {        e.printStackTrace();    } finally {        if (is != null) {            is.close();        }        if (out != null) {            out.close();        }    }    return result;}public ChannelSftp getSftp() {    return sftp;}public void setSftp(ChannelSftp sftp) {    this.sftp = sftp;}

// 触发servlet
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
String id= req.getParameter(“id”);
if(StringUtils.isBlank(id)){
throw new Exception(“唯一标识不能为空”);
}

        // 文件路径        String fileName = "文件所在路径-全路径地址";        String downFilename = fileName.substring(fileName.lastIndexOf("/")+1);        resp.setContentType("application/octet-stream");        String urlFileName = URLEncoder.encode(downFilename, "UTF-8");        resp.setHeader("Content-Disposition", "attachment;filename=\""                + urlFileName);        TftpUtil tftp = new TftpUtil();        // host port username password配置文件        ChannelSftp sftp = tftp.connect(TFTP_HOST,TFTP_PORT, TFTP_USERNAME,TFTP_PASSWORD);        tftp.setSftp(sftp);        tftp.ftpdownload(fileName, resp);    }catch(CommonException e){        log.error(e);        if(e.getErrCode().equals(RcmsErrorCode.ERROR_CODE_TLRNO_SESSION_INVALID)){            resp.sendRedirect(req.getContextPath() + "/common/expired.jsp");        }else{            resp.sendRedirect(req.getContextPath() + "/common/error.jsp");        }        return;    }catch(FileNotFoundException e){        log.error(e);        resp.sendRedirect(req.getContextPath() + "/common/error.jsp");        return;    }catch (Exception e) {        log.error(e);        resp.sendRedirect(req.getContextPath() + "/common/error.jsp");        return;    }}
0 0
原创粉丝点击