Linux安装ftp、Java的FTP上传下载文件工具类

news/2024/7/8 1:52:02 标签: linux, java, centos

Linux安装ftp、Java的FTP上传下载文件工具类

    • 文章说明
    • Linux安装vsftpd
    • Java的工具类

文章说明

网上找到说linux安装ftp,采用vsftpd,在后续的配置中少了一些说明,给我折磨了许久,写下这篇文章来记录

Linux安装vsftpd

命令非常简单,而且不需要什么额外的配置

yum install vsftpd -y
service vsftpd start
service vsftpd status

vi /etc/vsftpd/vsftpd.conf # 添加下面三行

pasv_enable=YES			# 允许被动模式
pasv_min_port=5000     # 被动模式下服务器使用的最小端口
pasv_max_port=5000     # 被动模式下服务器使用的最大端口

上面的几个命令就是安装配置vsftpd的步骤,当然如果开启了防火墙则需要开启那个端口,然后如果是云服务器也需要在规则里面放开这个端口的通行

改为bash脚本如下

yum install vsftpd -y
echo -e "pasv_enable=YES\npasv_min_port=5000\npasv_max_port=5000" | sudo tee -a /etc/vsftpd/vsftpd.conf
service vsftpd start
service vsftpd status

在安装并开启好vsftpd服务后,还需要创建用户,如jack用户,采用如下命令

useradd jack
passwd jack

并且为它设置一下密码,然后就可以了,需要注意的是有一些用户默认是不可以采用ftp连接的,在 /etc/vsftpd/user_list 里面,采用如下命令可以查看到内容

nl /etc/vsftpd/user_list
1	# vsftpd userlist
2	# If userlist_deny=NO, only allow users in this file
3	# If userlist_deny=YES (default), never allow users in this file, and
4	# do not even prompt for a password.
5	# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
6	# for users that are denied.
7	root
8	bin
9	daemon
10	adm
11	lp
12	sync
13	shutdown
14	halt
15	mail
16	news
17	uucp
18	operator
19	games
20	nobody

Java的工具类

简单引入依赖

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

工具类代码

java">package com.boot.util;

import org.apache.commons.net.ftp.FTPClient;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FtpUtil {
    private final String host;
    private final Integer port;
    private final String username;
    private final String password;
    private final String path;

    public FtpUtil(String host, Integer port, String username, String password, String path) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.path = path;
    }

    private FTPClient getFtpClient() throws IOException {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(host, port);
        ftpClient.login(username, password);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(path);
        return ftpClient;
    }

    public void uploadFile(String fileName, InputStream inputStream) throws Exception {
        FTPClient ftpClient = getFtpClient();
        ftpClient.storeFile(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), inputStream);
        ftpClient.disconnect();
    }

    public void downloadFile(String fileName, OutputStream outputStream) throws Exception {
        FTPClient ftpClient = getFtpClient();
        try (InputStream inputStream = ftpClient.retrieveFileStream(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))) {
            byte[] buf = new byte[1024];
            int read;
            while ((read = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, read);
            }
            ftpClient.disconnect();
        }
    }

    public static void main(String[] args) throws Exception {
        FtpUtil ftpUtil = new FtpUtil("****", 21, "****", "****", "****");
        ftpUtil.uploadFile("测试文件.txt", Files.newInputStream(Paths.get("C:/file/测试文件.txt")));

        try (FileOutputStream outputStream = new FileOutputStream("C:/file/测试文件2.txt")) {
            ftpUtil.downloadFile("测试文件.txt", outputStream);
        }
    }

}

http://www.niftyadmin.cn/n/5536016.html

相关文章

数据同步软件有哪些

数据同步软件有哪些呢&#xff1f;随着企业规模的扩大&#xff0c;企业数据也积累得越来越多&#xff0c;万一发生宕机风险&#xff0c;那么这个损失将不可估量。所以为了容灾备用&#xff0c;我们往往需要将数据同步到另一台备胎服务器上&#xff0c;进行冗余。 那么需要同步的…

2024-07-04 base SAS programming学习笔记8(HTML)

当使用ODS来进行结果或数据集输出的时候&#xff0c;可以同时设置多个ODS 命令&#xff0c;同时输出到多个不同的文件。使用_ALL_ 表示关闭所有的ODS输出窗口&#xff0c;比如&#xff1a; ods html file(body)"html-file-pathname"; ods html file"pdf-file-pa…

CentOS 7配置阿里云镜像源及其加速

备份原yum源的配置&#xff1a;mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载Centos-7.repo文件curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 清除及生成缓存 # 清除yum缓存 yum clean …

vscode中的配置导致js文件被识别了react的文件,怎么修改配置

在vscode的settings中找到类似*.js": "javascriptreact"的配置&#xff0c;将其修改为 {"files.associations": {"*.js": "javascript"} }

Unity 之基于URP使用UniStorm Weather System天气系统

内容将会持续更新&#xff0c;有错误的地方欢迎指正&#xff0c;谢谢! Unity 之基于URP使用UniStorm Weather System天气系统 TechX 坚持将创新的科技带给世界&#xff01; 拥有更好的学习体验 —— 不断努力&#xff0c;不断进步&#xff0c;不断探索 TechX —— 心探索、…

android recyclerview 刷新 数据错乱的问题

前言 android recyclerview 错乱的问题 每次刷新item的背景框都发生偏移或者bug 解决方法 在添加数据之前先清空recyclerView的缓存 亲测有效&#xff01; binding.recycler.getRecycledViewPool().clear(); binding.recycler.setLayoutManager(new LinearLayoutManager(ge…

ReactNative如何实现沉浸式状态栏及渐变色Header【兼容Android和iOS】

沉浸式状态栏 需要用到react-native提供的StatusBar组件 import {StatusBar} from react-native;<StatusBar barStyle{dark-content} backgroundColor{transparent} translucent{true}></StatusBar>如果用到Navigation&#xff0c;需要设置如下属性 navigation.…

论文 | Measuring and Narrowing the Compositionality Gap in Language Models

存在的问题&#xff1a; 关于一个复杂的多跳问题&#xff0c;大语言模型可以正确回答基于该问题分解的多个子问题&#xff0c;但是对于这个问题的最终答案大语言模型可能最终还是给不出来正确的。该论文根据这个现象提出了一个术语&#xff0c;叫“组合性差距”。 解决的方法&…