当前位置: 首页 > news >正文

衢州别墅设计工程师seo优化网络公司排名

衢州别墅设计工程师,seo优化网络公司排名,免费自助建站系统平台 贴吧,邯郸房产58同城首先要知道服务器的用户名和密码。 注意&#xff1a;一般情况&#xff0c;如果不是强制要求&#xff0c;尽量不要将文件上传到服务器 步骤&#xff1a; 1.导入依赖 <!--图片上传到服务器需要的依赖--> <dependency> <groupId>com.jcr…

首先要知道服务器的用户名和密码。

注意:一般情况,如果不是强制要求,尽量不要将文件上传到服务器

步骤:

1.导入依赖

<!--图片上传到服务器需要的依赖-->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>

2.编写配置文件application.yml

customize:
    remoteServer:
        sftp:
          SFTP_httpBaseUrl: /images/ # 访问附件的地址添加 一个映射 如  /images/ -》 /server-images/
          SFTP_httpPort: 80 # 公网访问的端口
          SFTP_directory: /server-images/ #主机保存附件目录
          SFTP_host: 192.168.1.10 #主机
          SFTP_port: 22 #端口号
          SFTP_username: root #用户名
          SFTP_password: 123456 #密码

 3.编写文件上传所需要的工具类

import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Properties;
import java.util.UUID;


/**
 * 类描述:
 * 上传文件到服务器的 工具类
 *
 * @ClassName SFTPUtil
 * @Author msi
 * @Date 2020/9/2 23:29
 * @Version 1.0
 */
@Component
public class SFTPUtil {

    /**
     * 返回公网访问的地址前缀
     */
    @Value("${customize.remoteServer.sftp.SFTP_httpBaseUrl}")
    protected String baseUrl;
    /**
     * 公网访问的端口
     */
    @Value("${customize.remoteServer.sftp.SFTP_httpPort}")
    protected int port;
    /**
     * 主机保存的目录
     */
    @Value("${customize.remoteServer.sftp.SFTP_directory}")
    protected String directory;
    /**
     * 主机的IP
     */
    @Value("${customize.remoteServer.sftp.SFTP_host}")
    protected String host;
    /**
     * ssh端口
     */
    @Value("${customize.remoteServer.sftp.SFTP_port}")
    protected int sshPort;
    /**
     * 用户名
     */
    @Value("${customize.remoteServer.sftp.SFTP_username}")
    protected String username;
    /**
     * 密码
     */
    @Value("${customize.remoteServer.sftp.SFTP_password}")
    protected String password;

    /**
     * 上传多文件到指定远程主机
     * @param files     文件数组
     * @return list 
     */
    public List<String> uploadMultipartFilesToServer(MultipartFile[] files) throws SftpException, JSchException, IOException {
        List<String> list = new ArrayList<>();
        ChannelSftp sftp = null;
        Session session = null;
        sftp = this.connect(this.host, this.sshPort, this.username, this.password);
        session = sftp.getSession();
        for (int i = 0; i < files.length; i++) {
            String originalFilename = files[i].getOriginalFilename();
            // 生成文件夹名 yyyy-mm
            String relativePath = new StringBuilder().append(LocalDate.now().getYear())
                    .append("-").append(LocalDate.now().getMonthValue()).toString();

            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();

            int lastIndex = originalFilename.lastIndexOf(".");
            String fileSuffix = originalFilename.substring(lastIndex);
            String filePrefix = originalFilename.substring(0, lastIndex);
            String fileName = new StringBuilder().append(filePrefix).append(uuid).append(fileSuffix).toString();

            // 文件上层目录
            String directory = this.directory + relativePath;
            // 创建文件夹
            this.createDir(directory, sftp);
            // 进入文件夹内
            sftp.cd(directory);
            // 创建文件
            sftp.put(files[0].getInputStream(), fileName);
            // 拼接返回格式
            String s = new StringBuilder("http://").append(this.host).append(":").append(this.port)
                    .append(this.baseUrl).append(relativePath).append("/").append(fileName).toString();

            list.add(s);
        }
        // 关掉连接
        sftp.disconnect();
        sftp.getSession().disconnect();

        return list;
    }

    /**
     * 建立连接
     * @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);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sftp;
    }

    /**
     * 创建目录
     *
     */
    public void createDir(String createpath, ChannelSftp sftp) {
        try {
            if (isDirExist(sftp, createpath)) {
                sftp.cd(createpath);
            }
            String pathArry[] = createpath.split("/");
            StringBuffer filePath = new StringBuffer("/");
            // 循环创建目录
            for (String path : pathArry) {
                if (path.equals("")) {
                    continue;
                }
                filePath.append(path + "/");
                if (isDirExist(sftp, filePath.toString())) {
                    sftp.cd(filePath.toString());
                } else {
                    // 建立目录
                    sftp.mkdir(filePath.toString());
                    // 进入并设置为当前目录
                    sftp.cd(filePath.toString());
                }
            }
            sftp.cd(createpath);
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }


    /**
     * 判断目录是否存在
     */
    public boolean isDirExist(ChannelSftp sftp, String directory) {
        boolean isDirExistFlag = false;
        try {
            SftpATTRS sftpATTRS = sftp.lstat(directory);
            isDirExistFlag = true;
            return sftpATTRS.isDir();
        } catch (Exception e) {
            if (e.getMessage().toLowerCase().equals("no such file")) {
                isDirExistFlag = false;
            }
        }
        return isDirExistFlag;
    }
}
 

4.编写对应controller进行调试

 @Autowired
    private UpdateFileUtil sftpUtil;
    /**
     * 上传文件到服务器
     *
     * @param files 图片
     * @return
     */
    @PostMapping("/file")
    public Result<List<String>> file(MultipartFile[] files) throws Exception {
        List<String> paths = sftpUtil.uploadMultipartFilesToServer(files);
        return Result.ofSuccess(paths);
    }

http://www.ritt.cn/news/15690.html

相关文章:

  • 教务系统网站开发方法营销案例100例小故事及感悟
  • 网站建设步骤及分工安卓优化大师官网下载
  • 杭州网站制作公司网盘搜索引擎
  • 杭州移动网站建设链接式友谊
  • 长沙建网站理互联网推广是什么意思
  • 成都手机wap网站制作青岛百度代理公司
  • 汕头百度网站建设分享几个x站好用的关键词
  • 东莞智通人才网招聘信息网天津网络优化推广公司
  • 上海自聊自做网站搜索网页
  • 在网站中设置网站地图好用的磁力搜索引擎
  • 网站的公告轮播效果怎么做怎么样引流加微信
  • 网站建设策划方案书专业seo网站优化推广排名教程
  • 服务外包平台搜索引擎优化的方法和技巧
  • 网站建设ui百度查询网
  • 做网站的一般多钱seo服务合同
  • 服务器ip做网站seo公司杭州
  • 网站建设赫伟创意星空科技1688官网入口
  • 做门户网站的思路一个网站可以优化多少关键词
  • 网站目录结构设计应注意的问题深圳网络营销平台
  • 手机上如何做mv视频网站淘宝网店代运营正规公司
  • 罗湖做网站公司体验营销策略有哪些
  • 天津建设协会网站百度今日小说排行榜
  • 网站开发项目的简介网上售卖平台有哪些
  • 服务器可以做几个网站软文网站平台
  • 南山网站建设公司市场营销的策划方案
  • 政府机关网站制作百度识图网站
  • 网站建设与管理学什么沈阳网站建设
  • 国外做网站公司能赚钱北京seo
  • 滁州商业网站建设网络营销成功案例介绍
  • 内部网站建设教程软文写作范例大全