public class UploadFils {
public void toHostFils(){
Connection con = new Connection(ip);//建立链接
try {
con.connect();//链接主机
boolean isAuthed = con.authenticateWithPassword("用户名","密码");//查看是否链接成功
if(isAuthed){//如果链接成功则进行如下操作
SCPClient scpClient = con.createSCPClient();//创建相应的文件传送的相应链接
//FileInputStream fieloutput = null;
//File file = new File(filePath);
//fieloutput = new FileInputStream(targetFile);
try{
sess.execCommand(" mkdir "+"创建文件");//创建相应的文件夹
}catch(SFTPException e1){
e1.getMessage();
System.out.println("文件夹已存在");
}catch(Exception e){
e.printStackTrace();
}
/**
* 这里scpClient.put('','')第一个参数是本地的文件必须是带地址和文件名的第二个是上传的路径
* 如果不是默认的路径的话需要在创建一层目录才可以
*/
scpClient.put("文件的路径包含文件名", "上传的目录");
//scpClient.put(new byte[fieloutput.available()],targetFile.getName(), "上传的目录");
}else{
/**
* 这里是直接根据数据库里的地址创建
*/
scpClient.put("文件的路径包含文件名", "上传的目录");
//scpClient.put(new byte[fieloutput.available()],"文件名", "上传的目录");
}
//fieloutput.close();
con.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void toHostFils(){
Connection con = new Connection(ip);//建立链接
try {
con.connect();//链接主机
boolean isAuthed = con.authenticateWithPassword("用户名","密码");//查看是否链接成功
if(isAuthed){//如果链接成功则进行如下操作
SCPClient scpClient = con.createSCPClient();//创建相应的文件传送的相应链接
//FileInputStream fieloutput = null;
//File file = new File(filePath);
//fieloutput = new FileInputStream(targetFile);
if(StringUtils.isNotBlank("上传的路径")){//这里上传时需要创建件文件夹
//这里获取这session主要是问了创建文件夹的时候使用的
try{
sess.execCommand(" mkdir "+"创建文件");//创建相应的文件夹
}catch(SFTPException e1){
e1.getMessage();
System.out.println("文件夹已存在");
}catch(Exception e){
e.printStackTrace();
}
/**
* 这里scpClient.put('','')第一个参数是本地的文件必须是带地址和文件名的第二个是上传的路径
* 如果不是默认的路径的话需要在创建一层目录才可以
*/
scpClient.put("文件的路径包含文件名", "上传的目录");
//scpClient.put(new byte[fieloutput.available()],targetFile.getName(), "上传的目录");
}else{
/**
* 这里是直接根据数据库里的地址创建
*/
scpClient.put("文件的路径包含文件名", "上传的目录");
//scpClient.put(new byte[fieloutput.available()],"文件名", "上传的目录");
}
//fieloutput.close();
con.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}