Java 浏览器、客户端下载SFTP文件

浏览器下载SFTP

		String directory = "/opt/docs/content/certificate/AICH/";
		String fileName1 = "MASS_READ_TEMPLATE1.xlsx";		
	    String userName = "root";
		String password = "centos21";
		String host = "172.24.8.103";
		int port = 22;
/**
	 *从SFTP上下载文件的方法,请求浏览器
	 *
	 * @param context
	 *            FacesContext
	 * @param filePath
	 *           文件路径
	 * @param fileName
	 *            文件名
	 * @param userName
	 *           用户名
	 * @param password
	 *            密码
	 * @param host
	 *            ip地址
	 * @param port
	 *            端口号
	 */
	public static void downAndReadSftpFileToExplorer(FacesContext context, String filePath, String fileName,
		String userName, String password, String host, int port, String privateKey) throws Exception {
		ExternalContext externalContext = context.getExternalContext();
		byte[] buffer = downloadFileSftpToByte(userName, password, host, port, privateKey, filePath, fileName);
		externalContext.responseReset();
		externalContext.setResponseContentType("application/octet-stream");
		externalContext.setResponseContentLength(buffer.length);
		externalContext.setResponseHeader("Content-Disposition", "attachment;filename=" + new String(fileName));
		try (OutputStream toClient = new BufferedOutputStream(externalContext.getResponseOutputStream())) {
			toClient.write(buffer, 0, buffer.length);
			toClient.flush();
		} catch (IOException e) {
			LogLogger.error(e);
			throw e;
		}
	}

/**
	 * @Description: SFTP文件转Byte
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @param host
	 *            ip地址
	 * @param port
	 *            端口号
	 * @param privateKey
	 *           私钥
	 * @param directory
	 *            文件路径
	 * @param downloadFile
	 *            文件名
	 * @Return: byte[]
	 * @Throws: Exception
	 */
	public static byte[] downloadFileSftpToByte(String userName, String password, String host, int port,
			String privateKey, String directory, String downloadFile) throws Exception {
		Session session = null;
		ChannelSftp sftp = null;
		//网关服务器SFTP
		try {
			JSch jsch = new JSch();
			if (privateKey != null) {
				// 密钥设置
				jsch.addIdentity(privateKey);
			}
			session = jsch.getSession(userName, host, port);
			if (password != null) {
				session.setPassword(password);
			}
			Properties config = new Properties();
			config.put("StrictHostKeyChecking", "no");
			session.setConfig(config);
			session.connect();
			Channel channel = session.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
		} catch (JSchException e) {
			e.printStackTrace();
		}
		if (null!=directory&&!"".equals(directory)) {
			sftp.cd(directory);
		}
		InputStream is = sftp.get(downloadFile);
		byte[] fileData = IOUtils.toByteArray(is);
		return fileData;
	}

客户端下载SFTP

/**
	 * @Description: 客户端下载SFTP文件
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @param host
	 *            ip地址
	 * @param port
	 *            端口号
	 * @param privateKey
	 *            私钥
	 * @param directory
	 *            文件路径
	 * @param downloadFile
	 *            文件名
	 * @param saveFile
	 *            保存文件地址
	 * @Return: void
	 * @Throws: Exception
	 */
	public static void downloadFileToSftp(String userName, String password, String host, int port, String privateKey,
			String directory, String downloadFile, String saveFile) throws Exception {
		Session session = null;
		ChannelSftp sftp = null;
		// SFTP服务器
		try {
			JSch jsch = new JSch();
			if (null!=privateKey) {
				// 私钥
				jsch.addIdentity(privateKey);
			}
			session = jsch.getSession(userName, host, port);
			if (password != null) {
				session.setPassword(password);
			}
			Properties config = new Properties();
			config.put("StrictHostKeyChecking", "no");
			session.setConfig(config);
			session.connect();
			Channel channel = session.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
		} catch (JSchException e) {
			e.printStackTrace();
		}
		if (null!=directory && !"".equals(directory)) {
			sftp.cd(directory);
		}
		File file = new File(saveFile);
		sftp.get(downloadFile, new FileOutputStream(file));
	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值