sftp工具类(支持ftp、sftp等文件操作)

package com.lianlian.cbfin.chnlgw.yintong.common.utils;

import com.lianlian.cbfin.chnlgw.yintong.api.common.enums.ResultCodeEnum;
import com.lianlian.cbfin.chnlgw.yintong.common.exception.BizException;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

import java.io.OutputStream;


@Slf4j
public class SFTPUtil {


    public static void fileUpload(String filePath, String fileName, String fileContent) {
        try {
            @Cleanup
            FileObject fo = VFS.getManager().resolveFile(filePath + fileName, createDefaultOptions());
            @Cleanup
            OutputStream os = fo.getContent().getOutputStream(4096);
            os.write(fileContent.getBytes());
            os.flush();
        } catch (Exception e) {
            log.error("文件上传异常", e);
            throw new BizException(ResultCodeEnum.UPLOAD_FILE_ERROR, e);
        }
    }

    public static void main(String[] args) throws Exception {
        String fileContent = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADYAAAAaCAYAAAD8K6+QAAAKWUlEQVRYhUWYy3IkyY1FDx4eEZnJqurqh6mtZWOajf7/s0YmtbW6qkhmhrsDmIVnSQsukkZnOuDAuReQH/7+P2UqqAsAmZARJIW6oGWc56SA1gxviokzZzGj2DbhaMZ1M3ZrvM3g/Ry4GirC+yMIhI6iKihF5cRVEISZRT+TAtyL69FQExLl7EXvAwAsaQ57M8wVN0FMURNcwICmcHHFN8NjBOWClazLFtRM3OHYNyjj1EQl2TcDlPMMhGRzw00pjHsvTgaj4BwJuhJhppgYoz8D8mJrUAEJpAggQBGlvD8Sb4JU0s8BCqghFIKSz78WV0CYPVAXDlMubrRdyAKfUQiAFGZCBZyzMBcyYYzAtbjsja0pjwEqYGZsTUGEcwRVSfPGmEUl0GDO4vU+yBQiEzNo7jRVRAFV6MKdhIIcRcRknAqSIEkzQ20lQJuCKVEr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DUbJ+SKA9MFqiie36l8H51Elo5+vjkXN64N/vrTCz/9cGHMTvNlsEsbr9URCbaWzNERcQrl7IMx55rHRBRVRTCygj6LEcKoos947kD06eMm5kJryz3MUYwZVC59MwNbD7mm3ZnMnoQKqsttAEglWwMo7n0iFC5wbMZtb3y8Ki9NUYVHHzQ3Xm43vDVez+D1nvz5LoxslCcjJ6/vb3z79m/uveOTpQtBMeZkjoX4HsXrPVZ5sRYzUUWiVOWajSaoO67GGP3Zd8uWKZAT5lyGVeW/CyOAY1O8rdFjxuSyKy9HW2sHLS5trSL++HZSr3d+/XwDYM7Jt7fJP78l//gyOUdhM/n6/sbr/RufD+G47nihBArFCmgkkdDH5OzJSFCptY+Yy30XEAJJ4raceM1VXjELt7VHmTPXBG4GtqwZgDmYy9N/FsdmfLwq10O4uPHXHz/z6cOVr693fv/6lYzBjx+FyKJq0XnO5O3tzpd78u395MMuCDsfdgdV/NvrJKN4ufoav6N4jMHIQlSgQ1Yy+io3dJlgEcVVkUhiJlms/nquGVYzrt1JrapcwzPQVGj63DjJ03Q7/Pb5hb/9+gsv151zTO6Pk8OFsxpf7snvXx7cLsX7WYzzQfQ7GtBIHrPxrzfj2J5W8PcvJ32sut/bMsMMmJHI84bBf50EBvJ9B1n11KVEEdIWP79rgTVDfRGSZxW6Cfrcp1QVUYmMRKr47cdP/O23n/jy+sYff35la8bt0ng9H/zf7++8vt/5/HFjJnx9ffDogzGgplAMksa2XzBRfHclquhz4C4c3rj7ZN4nmUuAFSU1QQqxtZ1UEmq9hsBy3yGrTLFlmExQV5BaNiuFtCXi4/vAKpBb0bSRMXl/v/N2vyNS/PzTjUT4xx9vzJl8/RZQneaGStBU+RoJNbnq5O+/fOB///KRMSf/D0HsbammkeiYAAAAAElFTkSuQmCC";

//            SFTPUtil.fileUpload("sftp://cbfadmin:[email protected]:22/mxsj/", "测试连接" + 5 + ".png", fileContent);


        FileObject fo = VFS.getManager().resolveFile("sftp://username:[email protected]:22/mxsj/测试连接.png", createDefaultOptions());
        fo.getName();
        fo.getChildren();
        fo.delete();
        System.err.println(fo);
    }

    private static FileSystemOptions createDefaultOptions() throws FileSystemException {
        FileSystemOptions opts = new FileSystemOptions();

        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setSessionTimeoutMillis(opts, 60 * 1000);

        SftpFileSystemConfigBuilder.getInstance().setConnectTimeoutMillis(opts, 5000);

        return opts;
    }
}

你可能感兴趣的:(工具代码片段,java)