java ssh 下载excel_java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)...

packagecom.zskj.pd.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.nio.charset.Charset;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.zskj.pd.pojo.FtpEntity;public classftpUtil {private final static Logger LOG = LoggerFactory.getLogger(ftpUtil.class);public static voidmain(String args[]) {

FtpEntity m= new FtpEntity("100.80.10.38", 21, "test", "test");

FTPClient fTPClient=login(m);

uploadFile(fTPClient,"6666666666", "doc", "2.bat", "D:\\otherFiles");

}//验证登录

public staticFTPClient login(FtpEntity ftpEntity) {

FTPClient ftp= null;try{

ftp= newFTPClient();

ftp.connect(ftpEntity.getIp(), ftpEntity.getPort());

ftp.login(ftpEntity.getName(), ftpEntity.getPwd());

ftp.setCharset(Charset.forName("UTF-8"));

ftp.setControlEncoding("UTF-8");

LOG.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" 登录成功");

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnftp;

}//获取ftp某一文件(路径)下的文件名字,用于查看文件列表

public static voidgetFilesName(FTPClient fTPClient) {try{//获取ftp里面,“Windows”文件夹里面的文件名字,存入数组中

FTPFile[] files = fTPClient.listFiles("/Windows");//打印出ftp里面,“Windows”文件夹里面的文件名字

for (int i = 0; i < files.length; i++) {

System.out.println(files[i].getName());

}

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/***

*@paramtargetFileName

*@paramtargetPath

*@paramsourceFileName

*@paramsourcePath*/

public static voiduploadFile(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {try{

File sfp= new File(sourcePath+"\\"+sourceFileName);

fTPClient.storeFile(targetPath+"\\"+targetFileName, newFileInputStream(sfp));

LOG.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" 上传完成");

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/***

*@paramtargetFileName

*@paramtargetPath

*@paramsourceFileName

*@paramsourcePath*/

public static voiduploadFile2(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {

OutputStream os= null;

FileInputStream fis= null;try{

os= fTPClient.storeFileStream(targetPath+"\\"+targetFileName);

fis= new FileInputStream(new File(sourcePath+"\\"+sourceFileName));byte[] b = new byte[1024];int len = 0;while ((len = fis.read(b)) != -1) {

os.write(b,0, len);

}

os.flush();

LOG.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" 上传完成");

}catch(IOException e) {

e.printStackTrace();

}finally{try{

os.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}try{

fis.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}/***

*@paramtargetFileName

*@paramtargetPath

*@paramsourceFileName

*@paramsourcePath*/

public static voiddownloadFile(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {try{//将ftp根目录下的"jsoup-1.10.2.jar"文件下载到本地目录文件夹下面,并重命名为"1.jar"

fTPClient.retrieveFile(sourcePath+"\\"+sourceFileName, new FileOutputStream(new File(targetPath+"\\"+targetFileName)));

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/***

*@paramtargetFileName

*@paramtargetPath

*@paramsourceFileName

*@paramsourcePath*/

public static voiddownloadFile2(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {

InputStream is= null;

FileOutputStream fos= null;try{

is= fTPClient.retrieveFileStream(sourcePath+"\\"+sourceFileName);

fos= new FileOutputStream(new File(targetPath+"\\"+targetFileName));byte[] b = new byte[1024];int len = 0;while ((len = is.read(b)) != -1) {

fos.write(b,0, len);

}

fos.flush();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

fos.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}try{

is.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值