1 windows下搭建ftp服务器

https://blog.csdn.net/qq_34610293/article/details/79210539

搭建好之后浏览器输入 ftp://ip就可以看见弹出账号密码的输入框。(win10家庭版本是没有在计算机管理中显示用户组的,具体添加方法见:https://www.kafan.cn/edu/68801291.html)

2 原生上传下载的demo

<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>

上传controller类

package com.test.domi.controller;

import com.test.domi.common.utils.FtpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.UUID; @RestController
@RequestMapping("/ftp")
public class FTPController { @PostMapping("/upload")
public Boolean pictureUpload(MultipartFile file) throws Exception {
String newName = UUID.randomUUID().toString();
String oldName = file.getOriginalFilename();
newName = newName + oldName.substring(oldName.lastIndexOf("."));
InputStream inputStream = file.getInputStream();
return FtpUtil.uploadFile("192.168.6.1",21,"testftp",
"testftp999","/home/testftp/images","/2015/01/02",newName,inputStream);
} @GetMapping("/download")
public void pictureDownload(HttpServletResponse response) throws Exception { FtpUtil.udownLoadFile("192.168.6.1",21,"testftp",
"testftp999","/home/testftp/images/2015/01/02","645c4e0a-4c23-4922-994e-84171a61a494.png",response);
} }

工具类

package com.test.domi.common.utils;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply; import javax.servlet.http.HttpServletResponse;
import java.io.*; public class FtpUtil { public static boolean uploadFile(String host, int port, String username, String password,
String basePath, String filePath, String filename, InputStream input) {
FTPClient ftp = new FTPClient();
Boolean result = false;
try {
// 连接FTP服务器
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//切换到上传目录
String path = basePath + filePath;
if (!ftp.changeWorkingDirectory(path)) {
//如果目录不存在则创建目录
String[] dirs = path.split("/");
StringBuffer tempPath = new StringBuffer();
for (String dir : dirs) {
if (null == dir || "".equals(dir)) {
continue;
}
tempPath.append("/").append(dir);
if (!ftp.changeWorkingDirectory(tempPath.toString())) {
if (!ftp.makeDirectory(tempPath.toString())) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath.toString());
}
}
}
}
//设置上传文件为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件默认是10M大小
//客户端编码等基本设置
//FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);设置unix下下载大小的限制
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
} public static void udownLoadFile(String host, int port, String username, String password,
String remotePath,String fileName, HttpServletResponse response) { FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
String name = ff.getName();
if (name.equals(fileName)) {
response.setContentType("application/form-data");
String realFileName = "我的重命名" + name.substring(name.lastIndexOf("."));
//"inline; filename=\""
//+ URLEncoder.encode(title, "UTF-8")
//+ ".txt\"");
response.setHeader("Content-disposition",
"attachment; filename=" + new String(realFileName.getBytes("utf-8"), "ISO8859-1"));
response.setCharacterEncoding("utf-8");
ftp.retrieveFile(ff.getName(), response.getOutputStream());
}
}
ftp.logout(); } catch (Exception e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) { }
} } }
}

3 springboot整合spring Camel 读取ftp文件

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bindy</artifactId>
<version>2.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp-starter</artifactId>
<version>2.19.1</version>
</dependency>

配置文件

待办:

附件FTP服务器,下载的附件不显示设置的名字和后缀,只能重命名后缀才能打开,

对ftp客户端的工具类封装,建立表结构关系,维护fileId与原文件名,存放位置之间的关系,

上传失败时候,在catch中删除文件

ftp服务器上传下载共享文件的更多相关文章

  1. Spring学习---Spring中利用组件实现从FTP服务器上传/下载文件

    FtpUtil.java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...

  2. 使用批处理文件在FTP服务器 上传下载文件

    1.从ftp服务器根目录文件夹下的文件到指定的文件夹下 格式:ftp -s:[配置文件] [ftp地址] 如:ftp -s:c:\vc\ftpconfig.txt   192.168.1.1 建立一个 ...

  3. FTP服务器上传,下载文件

    public class FtpUtil { /** * * @param host FTP服务器地址 * @param port FTP服务器端口 * @param username FTP登录账号 ...

  4. 使用ftp软件上传下载php文件时换行丢失bug

    正 文:   在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了, ...

  5. Linux 终端访问 FTP 及 上传下载 文件

    今天同事问我一个问题,在Linux 下访问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上.google 一下. 方 ...

  6. Linux 终端访问 FTP 及 上传下载 文件[转]

    1.      Linux 终端连接FTP [oracle@Dave ~]$ ftp 10.85.7.97 Connected to 10.85.7.97. 220 Serv-U FTP Server ...

  7. 使用ftp软件上传下载php文件时换行丢失bug(全部变为一行)

    文章来源:http://www.piaoyi.org/computer/ftp-php-r-n-bug.html 正 文: 在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下 ...

  8. Python 基于Python实现Ftp文件上传,下载

    基于Python实现Ftp文件上传,下载   by:授客 QQ:1033553122 测试环境: Ftp客户端:Windows平台 Ftp服务器:Linux平台 Python版本:Python 2.7 ...

  9. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

随机推荐

  1. okHttp3 源码分析

    一, 前言 在上一篇博客OkHttp3 使用详解里,我们已经介绍了 OkHttp 发送同步请求和异步请求的基本使用方法. OkHttp 提交网络请求需要经过这样四个步骤: 初始化 OkHttpClie ...

  2. 代码优化-多态代替IF条件判断

    场景描述 在开发的场景中,常常会遇到打折的业务需求,每个用户对应的等级,他们的打折情况也是不一样的.例如普通会员打9折,青铜会员打8.5折,黄金会员打8折等等.在一般开发中最简单的就是判断用户的等级, ...

  3. Mysql 修改密码和设置远程连接

    [参考文章]:mysql修改root密码和设置权限 1. 修改密码 1.1 set password 登录mysql set password for 用户名@localhost = password ...

  4. TCP层close系统调用的实现分析

    在调用close系统调用关闭套接字时,如果套接字引用计数已经归零,则需继续向上层调用其close实现,tcp为tcp_close:本文仅介绍tcp部分,前置部分请参考本博关于close系统调用的文章: ...

  5. GPS定位RTK解决方案

    GPS差分: 实时差分定位是指在测量点上实时得到高精度的定位结果.这种模式的具体方法是:在一个已知测站上架设GPS基准站接收机和数据电台,连续跟踪所有可见卫星,并通过数据电台向移动站发送差分改正数据. ...

  6. XPATH了解

    特殊标签 找SVG这种特殊标签可以使用[name()='svg'],如//[name()='svg']/[name()='line'][2] 文本 找标签内的文本时可以使用: //*[text()=' ...

  7. 虚拟化技术原理(CPU、内存、IO)

    本文来自:http://www.ywnds.com/?p=5856 虚拟化 云计算现在已经非常成熟了,而虚拟化是构建云计算基础架构不可或缺的关键技术之一. 云计算的云端系统, 其实质上就是一个大型的分 ...

  8. python jieba分词小说与词频统计

    1.知识点 """ 1)cut() a) codecs.open() 解决编码问题 b) f.readline() 读取一行,也可以使用f.readlines()读取多行 ...

  9. brew update 很慢

    brew使用国内镜像源 这里用中科大的,另外还有清华的可用 # 步骤一 cd $(brew --repo) git remote set-url origin https://mirrors.tuna ...

  10. Sql UpdateOrInsert

    SqlServer(先更新,受影响条数为0,则Insert,通过事务): begin tran update table set column=columnvalue where wherestr b ...