文件上传下载

文件上传下载
FtpClient插件有可能无法连接服务器,使用jsch-0.1.24.jar插件解决
以下是相关工具类

package com.run.ayena.intersearch.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 SftpTools {

    /** 
     * 链接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); 
            System.out.println("密码输入成功");
            Properties sshConfig = new Properties();  
            sshConfig.put("StrictHostKeyChecking", "no");
            System.out.println("链接参数设置成功");
            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();  
        }  
    }  


    public void uploada(String directory,String sftpFileName,InputStream input, ChannelSftp sftp) throws SftpException{
        try {
            sftp.cd(directory);

        } catch (Exception e) {
            // TODO: handle exception
        }
        sftp.put(input, sftpFileName);
    }

    /** 
     * 文件下载 
     *  
     * @param directory 目录 
     * @param downloadFile 要下载文件名 
     * @param saveFile 保持的文件名 
     * @param sftp 
     */  
    public void download(String directory, String downloadFile,  
            String saveFile, ChannelSftp sftp) {  
        FileOutputStream fos=null;
        try {  
            sftp.cd(directory);  
            File file = new File(saveFile);  
            fos=new FileOutputStream(file);
            sftp.get(downloadFile, fos);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{ 
            if(null!=fos){
                try {
                    fos.close();
                } catch (IOException 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);  
    }  


    //批量删除文件
    public void delete(String directory, String[] fileNames, ChannelSftp aa) {
        for (String fileName : fileNames) {
            this.delete(directory, fileName, aa);
        }
    }

    /**
     * 创建目录文件夹
     * @param directory  要创建文件夹的位置路径
     * @param fileName  要创建文件夹的名称
     * @param sftp   sftp连接
     */
    //创建目录文件
    public void mkdir(String directory,String fileName,ChannelSftp sftp){
        try {  
            sftp.cd(directory);  
            sftp.mkdir(fileName);  
            System.out.println("文件夹创建成功");  
        } catch (Exception e) {  
            System.out.println("文件夹创建失败");  
            e.printStackTrace();  
        }  
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值