java将文件上传到云服务器

添加依赖jar包

<!--sftp文件上传-->
<dependency>
   <groupId>com.jcraft</groupId>
   <artifactId>jsch</artifactId>
   <version>0.1.54</version>
</dependency>

编写ftp.properties配置文件

ip = 服务器ip
user = 登录用户名
password = 登录密码
port = 22(默认22端口)
filePath = 文件存放的linux路径

编写FtpUtils工具类

package com.hc.travelerothermodeprovider.commons;


import com.jcraft.jsch.Channel;
import com.jcraft.jsch.*;
import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.*;
import java.util.Properties;
import java.util.Vector;

public class FtpUtils {

    /**
     * 利用JSch包实现SFTP上传文件
     * @param bytes  文件字节流
     * @param fileName  文件名
     * @throws Exception
     */
    public static void sshSftp(byte[] bytes,String fileName) throws Exception{


        Properties properties = new Properties();

        InputStream in = FtpUtils.class.getResourceAsStream("properties文件路径");
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        properties.load(bf);


        String ip = properties.getProperty("ip");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        int port= Integer.parseInt(properties.getProperty("port"));

        Session session = null;
        Channel channel = null;


        JSch jsch = new JSch();


        if(port <=0){
            //连接服务器,采用默认端口
            session = jsch.getSession(user, ip);
        }else{
            //采用指定的端口连接服务器
            session = jsch.getSession(user, ip ,port);
        }

        //如果服务器连接不上,则抛出异常
        if (session == null) {
            throw new Exception("session is null");
        }

        //设置登陆主机的密码
        session.setPassword(password);//设置密码
        //设置第一次登陆的时候提示,可选值:(ask | yes | no)
        session.setConfig("StrictHostKeyChecking", "no");
        //设置登陆超时时间
        session.connect(30000);


        OutputStream outstream = null;
        try {
            //创建sftp通信通道
            channel = (Channel) session.openChannel("sftp");
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;


            //进入服务器指定的文件夹
            sftp.cd(properties.getProperty("filePath"));

            //列出服务器指定的文件列表
//            Vector v = sftp.ls("*");
//            for(int i=0;i<v.size();i++){
//                System.out.println(v.get(i));
//            }

            //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
            outstream = sftp.put(fileName);
            outstream.write(bytes);
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关流操作
            if(outstream != null){
                outstream.flush();       
                outstream.close();
            }
            if(session != null){
                session.disconnect();
            }
            if(channel != null){
                channel.disconnect();
            }
        }
    }

}

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值