1.ftp和ftps

ftp文件传输协议是应用层协议,用于传输文件信息,linux上需要安装vsftpd才能使用(明文传输)

ftps为FTP协议和数据通道添加了SSL功能,可以对传输信息加密

2.ftp传输文件

引入maven依赖

<!-- https://mvnrepository.com/artifact/commons-net/commons-net --><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.6</version></dependency>

代码,此处将传入的文件参数改为MultipartFile类型,方便前端传递文件数据,再转为File对象(建立临时文件,上传完成会删除),如果需要可以自己改为文件路径

package com.wyl.ppa.utils;import java.io.*;
import java.util.TimeZone;import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;public class Ftp {private FTPClient ftpClient;private String strIp;private int intPort;private String user;private String password;private static final Logger logger = LoggerFactory.getLogger(Ftp.class);/* ** Ftp构造函数*/public Ftp(String strIp, int intPort, String user, String Password) {this.strIp = strIp;this.intPort = intPort;this.user = user;this.password = Password;this.ftpClient = new FTPClient();}/*** 判断是否登入成功* */public void ftpLogin() {FTPClientConfig ftpClientConfig = new FTPClientConfig();ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());this.ftpClient.setControlEncoding("GBK");this.ftpClient.configure(ftpClientConfig);try {if (this.intPort > 0) {this.ftpClient.connect(this.strIp, this.intPort);} else {this.ftpClient.connect(this.strIp);}// FTP服务器连接回答int reply = this.ftpClient.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {this.ftpClient.disconnect();System.out.println();logger.error("登录FTP服务失败!");return;}this.ftpClient.login(this.user, this.password);// 设置传输协议this.ftpClient.enterLocalPassiveMode();this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);logger.info(this.user + "成功登陆FTP服务器");} catch (Exception e) {e.printStackTrace();logger.error(this.user + "登录FTP服务失败!" + e.getMessage());return;}this.ftpClient.setBufferSize(1024 * 2);this.ftpClient.setDataTimeout(30 * 1000);}/*** 退出关闭服务器链接* */public void ftpLogOut() {if (null != this.ftpClient && this.ftpClient.isConnected()) {try {boolean reuslt = this.ftpClient.logout();// 退出FTP服务器if (reuslt) {logger.info("成功退出服务器");}} catch (IOException e) {e.printStackTrace();logger.warn("退出FTP服务器异常!" + e.getMessage());} finally {try {this.ftpClient.disconnect();// 关闭FTP服务器的连接} catch (IOException e) {e.printStackTrace();logger.warn("关闭FTP服务器的连接异常!");}}}}/**** 上传multipartFile文件* @param multipartFile 上传的文件* @param romotUpLoadePath 上传服务器路径 - 应该以/结束* */public void uploadMultipartFile(MultipartFile multipartFile, String romotUpLoadePath) {File localFile = FileUtils.multipartFileToFile(multipartFile);BufferedInputStream inStream = null;boolean success;try {this.ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改变工作路径inStream = new BufferedInputStream(new FileInputStream(localFile));logger.info(localFile.getName() + "开始上传.....");success = this.ftpClient.storeFile(localFile.getName(), inStream);if (success) {logger.info(localFile.getName() + "上传成功");}FileUtils.deleteTempFile(localFile);} catch (FileNotFoundException e) {e.printStackTrace();logger.error(localFile + "未找到");} catch (IOException e) {e.printStackTrace();} finally {if (inStream != null) {try {inStream.close();} catch (IOException e) {e.printStackTrace();}}}}/**** 上传multipartFile文件* @param localFile 上传的文件* @param romotUpLoadePath 上传服务器路径 - 应该以/结束* */private void uploadFile(File localFile, String romotUpLoadePath) {BufferedInputStream inStream = null;boolean success;try {this.ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改变工作路径inStream = new BufferedInputStream(new FileInputStream(localFile));logger.info(localFile.getName() + "开始上传.....");success = this.ftpClient.storeFile(localFile.getName(), inStream);if (success) {logger.info(localFile.getName() + "上传成功");}} catch (FileNotFoundException e) {e.printStackTrace();logger.error(localFile + "未找到");} catch (IOException e) {e.printStackTrace();} finally {if (inStream != null) {try {inStream.close();} catch (IOException e) {e.printStackTrace();}}}}/**** 下载文件* @param remoteFileName   待下载文件名称* @param localDires 下载到当地那个路径下* @param remoteDownLoadPath remoteFileName所在的路径* */public void downloadFile(String remoteFileName, String localDires,String remoteDownLoadPath) {String strFilePath = localDires + remoteFileName;BufferedOutputStream outStream = null;boolean success = false;try {this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);outStream = new BufferedOutputStream(new FileOutputStream(strFilePath));logger.info(remoteFileName + "开始下载....");success = this.ftpClient.retrieveFile(remoteFileName, outStream);if (success) {logger.info(remoteFileName + "成功下载到" + strFilePath);return;}} catch (Exception e) {e.printStackTrace();logger.error(remoteFileName + "下载失败");} finally {if (null != outStream) {try {outStream.flush();outStream.close();} catch (IOException e) {e.printStackTrace();}}}if (!success) {logger.error(remoteFileName + "下载失败!!!");}}/**** 上传文件夹* @param localDirectory*            当地文件夹* @param remoteDirectoryPath*            Ftp 服务器路径 以目录"/"结束* */public void uploadDirectory(String localDirectory,String remoteDirectoryPath) {File src = new File(localDirectory);try {remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";this.ftpClient.makeDirectory(remoteDirectoryPath);// ftpClient.listDirectories();} catch (IOException e) {e.printStackTrace();logger.info(remoteDirectoryPath + "目录创建失败");}File[] allFile = src.listFiles();assert allFile != null;for (File file : allFile) {if (!file.isDirectory()) {String srcName = file.getPath();uploadFile(new File(srcName), remoteDirectoryPath);}}for (File file : allFile) {if (file.isDirectory()) {// 递归uploadDirectory(file.getPath(),remoteDirectoryPath);}}}/**** 下载文件夹* @param localDirectoryPath 本地地址* @param remoteDirectory 远程文件夹* */public void downLoadDirectory(String localDirectoryPath,String remoteDirectory) {try {String fileName = new File(remoteDirectory).getName();localDirectoryPath = localDirectoryPath + fileName + "//";boolean mkdirs = new File(localDirectoryPath).mkdirs();if(!mkdirs){logger.error("创建文件失败!");return;}FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory);for (FTPFile ftpFile : allFile) {if (!ftpFile.isDirectory()) {downloadFile(ftpFile.getName(), localDirectoryPath, remoteDirectory);}}for (FTPFile ftpFile : allFile) {if (ftpFile.isDirectory()) {String strremoteDirectoryPath = remoteDirectory + "/" + ftpFile.getName();downLoadDirectory(localDirectoryPath, strremoteDirectoryPath);}}} catch (IOException e) {e.printStackTrace();logger.info("下载文件夹失败");}}}

3.ftps

引入maven依赖<dependency>

<dependency>        <groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.54</version></dependency>

代码中设置了私钥,但是我上传没有用到加密功能,需要的话可以自己下载openssl生成一个私钥和公钥,把把公钥放在服务器上,私钥设置在代码里上传就可以了

package com.wyl.ppa.utils;import com.jcraft.jsch.*;import java.io.*;
import java.util.Properties;/*** Created by zhenghao on 2018/9/18.*/
public class SFtp {private ChannelSftp sftp;private Session session;/*** SFTP 登录用户名*/private String username;/*** SFTP 登录密码*/private String password;/*** 私钥*/private String privateKey;/*** SFTP 服务器地址IP地址*/private String host;/*** SFTP 端口*/private int port;/*** 构造基于密码认证的sftp对象*/public SFtp(String username, String password, String host, int port) {this.username = username;this.password = password;this.host = host;this.port = port;}/*** 构造基于秘钥认证的sftp对象*/public SFtp(String username, String host, int port, String privateKey) {this.username = username;this.host = host;this.port = port;this.privateKey = privateKey;}/*** 连接sftp服务器*/public void login() {try {JSch jsch = new JSch();if (privateKey != null) {jsch.addIdentity(privateKey);// 设置私钥}session = jsch.getSession(username, host, port);if (password != null) {session.setPassword(password);}Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);session.connect();Channel channel = session.openChannel("sftp");channel.connect();sftp = (ChannelSftp) channel;} catch (JSchException e) {e.printStackTrace();}}/*** 关闭连接 server*/public void logout() {if (sftp != null) {if (sftp.isConnected()) {sftp.disconnect();}}if (session != null) {if (session.isConnected()) {session.disconnect();}}}/*** 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory** @param directory    上传到该目录* @param sftpFileName sftp端文件名*/public boolean upload(String directory, String sftpFileName, InputStream input) throws SftpException {try {if (directory != null && !"".equals(directory)) {sftp.cd(directory);}sftp.put(input, sftpFileName);  //上传文件return true;} catch (SftpException e) {return false;}}//上传文件测试public static void main(String[] args) throws SftpException, IOException {}
}

Java使用FTP、FTPS上传文件相关推荐

  1. java获取ajax上传的文件,Java使用Ajax异步上传文件

    相关代码示例: html代码片段: 名称 class="layui-input"> 描述 文件 请选择配置文件 立即提交 重置 js代码片段: //上传配置文件 $(&quo ...

  2. http上传文件到ftp服务器,HTTP协议与FTP协议上传文件的优缺点

    1:用http协议上传更适合web编程的方便:传小于1M文件速度要比用ftp协议上传文件略快.安全性好:不像ftp那样:必须要启动一个ftp服务才行.2:用ftp协议上传文件大于1M的文件速度比htt ...

  3. HTTP协议与FTP协议上传文件的优缺点

    转自:http://blog.itpub.net/22434610/viewspace-615171/ 用http协议上传更适合web编程的方便:传小于1M文件速度要比用ftp协议上传文件略快.安全性 ...

  4. java在FTP服务器创建文件夹,java在ftp服务器上创建文件夹

    java在ftp服务器上创建文件夹 内容精选 换一换 启动目的端时失败,错误码:SMS.3103,提示迁移失败原因"对目的服务器重新建立引导失败".启动目的端后,系统会重新安装目的 ...

  5. ftp服务器上传文件权限设置,ftp服务器 上传文件权限设置

    ftp服务器 上传文件权限设置 内容精选 换一换 华为云对象存储服务帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用对 ...

  6. 如何设置ftp服务器上传文件夹至不同路径,设置ftp服务器上传文件夹

    设置ftp服务器上传文件夹 内容精选 换一换 上传单个或多个本地文件或文件夹至OBS指定路径.待上传的文件可以是任何类型:文本文件.图片.视频等等.上传本地文件或文件夹时,请确保本地文件或文件夹在上传 ...

  7. ftp linux 推送文件_Linux下SSH用FTP命令上传文件至另一个FTP空间

    如果没有ftp 提示: -bash: ftp: command not found 请先安装ftp应用程序: yum install ftp #ftp 127.0.0.1 21 输入远程空间的FTP ...

  8. php利用ftp上传视频,PHP用FTP类上传文件视频等的简单实现方法

    文件1:ftp.php<?php /** * 仿写CodeIgniter的FTP类 * FTP基本操作: * 1) 登陆; connect * 2) 当前目录文件列表; filelist * 3 ...

  9. 使用ftp服务上传文件时553报错的解决

    使用ftp服务上传文件时553报错的解决 在使用ftp上传本地文件时,会遇到553的报错,以下是我总结的解决方法 1.使用本地用户登陆时 在使用本地用户登陆ftp服务上传文件时,如果遇到553的报错, ...

  10. 使用ftp服务上传文件时553报错的解决(绝对有用)

    使用ftp服务上传文件时553报错的解决 在使用ftp上传本地文件时,会遇到553的报错,以下是我总结的解决方法 1.使用本地用户登陆时 在使用本地用户登陆ftp服务上传文件时,如果遇到553的报错, ...

最新文章

  1. spring中@value注解需要注意
  2. python 基础学习3-函数
  3. C++二维码相关库编译
  4. java用符号断开取前面_java-如何读取断开的符号链接指向的路径?
  5. 太爽了!宅男醒来后,发现自己变成了……
  6. 如何在 Kubernetes Pod 内进行网络抓包
  7. JDBC连接sql server数据库
  8. 暂无支持此机型的手机版本_miui+app官方版-miui+安卓版 _5577安卓网
  9. python基础 day1 var、 if、while、for、with as、open用法
  10. ArrayList详细
  11. 简易网站流量统计工具
  12. 腾讯云 obs 推流一直断开连接无法进行直播
  13. 查看 java opts,如何在命令提示符中检查JAVA_OPTS值?
  14. 动态壁纸html怎么设置方法,动态壁纸怎么设置
  15. linux vi面板如何复制一行
  16. Linux中RAID5搭建
  17. pytorch model.cuda()花费时间很长
  18. 关于内部文档的标准格式要求(图文)
  19. PMP项目经理常用项目管理工具分析
  20. QT 大作业实现对图片与视频的处理

热门文章

  1. 不用找了,学习BM算法,这篇就够了(思路+详注代码)
  2. element ui 排课_vue怎么去写一个学校排课系统
  3. linux修改文件创建的时间,Linux下修改文件创建修改时间
  4. Winform最基础的DBHelper类
  5. 基于ResNET50模型进行迁移学习构建中药饮片分类Web App
  6. imperva CDN的优势有哪些
  7. 套筒机械加工工艺规程制订(论文+DWG图纸+工序卡)
  8. 记账本记录日常收支 如何查看某个时间段的明细
  9. ToB 服务的交付能力优化
  10. socket服务器端和客户端通信——基础篇