上传下载文件ftp(解决中文乱码问题)

1.jar包

<dependency>
	<groupId>com.jcraft</groupId>
	<artifactId>jsch</artifactId>
	<version>0.1.53</version>
</dependency>
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.5</version>
</dependency>


2.确定协议名称

ftp使用commons-net包

sftp使用jcraft包


3.连接工具

xftp

ssh


4.写ftp配置连接信息配置文件(ftp.properties)

host=主机名
port=端口号
username=用户
password=密码
dir=工作目录

5.编写接口

/**
	 * 连接服务器
	 */
	void getConnectService();

	/*
	 * 上传
	 */
	void fileUpLoad(String url) throws Exception;

	/**
	 * 下载
	 * 
	 * @param url 文件路径
	 */
	void fileDown(String url, String localpath) throws Exception;
	
	/**
	 * 获取文件列表
	 * @return
	 * @throws Exception
	 */
	List<String> getFileList() throws Exception;

	/**
	 * 释放资源
	 */
	void destroy();

6.连接服务器

public void getConnectService() {
		JSch jSch = new JSch();
		try {
			session = jSch.getSession(getConnParams("username"),
					getConnParams("host"),
					Integer.parseInt(getConnParams("port")));
			session.setPassword(getConnParams("password").getBytes());
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect(10000);
			serverDir = getConnParams("dir");
		} catch (JSchException e) {
			e.printStackTrace();
		}
	}

7.从连接中获取chanel通道,用户传输数据

channel channel = session.openChannel("sftp");

sftp协议需要将通道转换为ChannelSftp类型

sftp = (ChannelSftp) channel;


8.上传文件

public void fileUpLoad(String url) throws Exception {
		File file = new File(url);
		if (!file.exists()) {
			return;
		}	
		// 打开通道
		channel = session.openChannel("sftp");
		channel.connect(10000);
		sftp = (ChannelSftp) channel;
		sftp.setFilenameEncoding("UTF-8");
		sftp.cd(serverDir);
		// 打开输出流,上传文件,制定路径和文件名称
		InputStream in = new FileInputStream(file);
		
		sftp.put(in, file.getName());
		sftp.rename(file.getName(), rename());
		destroy();
	}

9.下载文件

public void fileDown(String url, String localpath) throws Exception {
		channel = session.openChannel("sftp");
		channel.connect(10000);
		ChannelSftp sftp = (ChannelSftp) channel;
		sftp.setFilenameEncoding("UTF-8");
		// 解决编码问题
		OutputStream writer = new FileOutputStream(new File(localpath));
		
		sftp.get(serverDir + url, writer);
		
		writer.flush();
		writer.close();
		destroy();
	}

10.获取文件列表

public List<String> getFileList() throws Exception {
		List<String> list = new ArrayList<String>();
		channel = session.openChannel("sftp");
		channel.connect(10000);
		sftp = (ChannelSftp) channel;
		
		Vector<ChannelSftp> vector =  sftp.ls(serverDir);
		
		for (Object obj : vector) {
			if(obj instanceof ChannelSftp.LsEntry){
				String fileName = ((LsEntry) obj).getFilename();
				list.add(fileName);
			}
		}
		return list;
	}

11.释放资源

public void destroy() {
		if (session != null) {
			session.disconnect();
		}
		if (channel != null) {
			channel.disconnect();
		}
	}


从这个内容上看,经验就是还要靠自己多研究,看源码有难度就看api,多尝试方法解决















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值