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. C++中void和void*指针的含义 (指针类型的含义)

    转载自:http://blog.csdn.net/lee_shuai 指针有两个属性:指向变量/对象的地址和长度,但是指针只存储地址,长度则取决于指针的类型:编译器根据指针的类型从指针指向的地址向后寻 ...

  2. TCP之服务与首部

    1. TCP 的服务 TCP 通过下列方式提供可靠性: 应用数据被分割成 TCP 认为最适合发送的数据块.与 UDP 不同,UDP 应用程序产生的数据报长度将保持不变.由 TCP 传递给 IP 的信息 ...

  3. redis的incr和incrby命令

    Redis Incr 命令将 key 中储存的数字值增一,如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. Redis Incrby 命令将 key 中储存的 ...

  4. python全栈开发第6天

    作业一:1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 3) 为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区 ...

  5. 代码实现程序启动后, 可以从键盘输入接收多个整数, 直到输入quit时结束输入. 把所有输入的整数倒序排列打印

    package com.loaderman.test; import java.util.Comparator; import java.util.Scanner; import java.util. ...

  6. The inherit, initial, and unset values

    The  inherit, initial, and unset keywords are special values you can give to any CSS property. Tests ...

  7. RxJava2实战---第六章 条件操作符和布尔操作符

    RxJava2实战---第六章 条件操作符和布尔操作符 RxJava的条件操作符主要包括以下几个: amb():给定多个Observable,只让第一个发射数据的Obsrvable发射全部数据. de ...

  8. java分布式事务及解决方案

    1.什么是分布式事务 分布式事务就是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.以上是百度百科的解释,简单的说,就是一次大的操作由不同的小操作组成 ...

  9. JavaScript高程第三版笔记(1-5章)

    第2章:在html中使用javascript ①script标签的defer属性 <script type="text/javascript" defer="def ...

  10. 从gopath到go mod的一次尝试

    windows下的尝试: gomod初尝试下载官方包1.11(及其以上版本将会自动支持gomod) 默认GO111MODULE=auto(auto是指如果在gopath下不启用mod)go mod h ...