本篇文章主要介绍了"通过sftp操作Linux服务器上的文件java",主要涉及到文件下载,Exception方面的内容,对于Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
下载sftp所需要用到的jar包:com.jcraft.jsch_0.1.31.jar所用到的文件操作工具类代码如下:import java.io.File;
...
下载sftp所需要用到的jar包:com.jcraft.jsch_0.1.31.jar
所用到的文件操作工具类代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class Util {
/**
* 链接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 sshSession = jsch.getSession(username, host, port);
System.out.println("Session创建成功");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session已连接");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("连接到主机" + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
return sftp;
}
/**
* 文件重命名
*
* @param directory 目录
* @param oldname 旧文件名
* @param newname 新文件名
* @param sftp
*/
public void renameFile(String directory, String oldname, String newname,
ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rename(oldname, newname);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*
* @param directory 目录
* @param uploadFile 要上传的文件名
* @param sftp
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件下载
*
* @param directory 目录
* @param downloadFile 要下载文件名
* @param saveFile 保持的文件名
* @param sftp
*/
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件删除
*
* @param directory 目录
* @param deleteFile 要删除的文件名
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
System.out.println("删除成功");
} catch (Exception e) {
System.out.println("删除失败");
e.printStackTrace();
}
}
/**
* 列出目录下的文件
*
* @param directory 目录
* @param sftp
* @return
* @throws SftpException
*/
public Vector listFiles(String directory, ChannelSftp sftp)
throws SftpException {
return sftp.ls(directory);
}
}
测试代码:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.junit.Test;
import com.chigo.utils.SFTPUtil;
import com.jcraft.jsch.ChannelSftp;
public class Test01 {
//建立链接
SFTPUtil sf = new SFTPUtil();
String host = "192.168.130.33";
int port = 22;
String username = "root";
String password = "abc345";
ChannelSftp sftp=sf.connect(host, port, username, password);
@Test
public void testList() throws Exception{
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/product/Home_appliances/";
Vector v = sf.listFiles(directory, sftp);
for(int i = 0;i < sf.listFiles(directory, sftp).size();i++){
System.out.println(v.get(i));
}
List list = new ArrayList();
for(int i = 2;i < sf.listFiles(directory, sftp).size();i++){
list.add(v.get(i).toString().substring(55, v.get(i).toString().length()));
System.out.println(v.get(i).toString().substring(55, v.get(i).toString().length()));
}
System.out.println(list.toString());
}
@Test
public void testUpload(){
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/homeAnimation/";
sf.upload(directory, "C:/product01.JPG", sftp);
}
@Test
public void testRenameFile(){
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/homeAnimation/";
String oldname = "baobiao1.png";
String newname = "a.png";
sf.renameFile(directory, oldname, newname, sftp);
}
@Test
public void testDelFile(){
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/homeAnimation/";
String deleteFile = "baobiao1.png";
sf.delete(directory, deleteFile, sftp);
}
}
以上就介绍了通过sftp操作Linux服务器上的文件java,包括了文件下载,Exception方面的内容,希望对Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_240116.html