安装 vsftpd
yum install -y vsftpd
cd /etc/vsftpd
systemctl enable vsftpd.service
systemctl start vsftpd.service
systemctl stop firewalld
通过windows下的资源管理器访问
- 如果安装成功,并不需要任何账号密码,就能直接读取。默认在服务器上的文件夹是 /var/ftp,一般只有一个默认文件夹 pub。
- 这里的资源管理器只拥有读的权限,增删改都不具备。
ftp://119.24.12.211/
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7c9fbc8a1fd7f9ba2dd8669778bb1d55.png)
使用java连接ftp服务器上传文件(注意主动模式和被动模式)
- java连接必须创建用户密码连接
- 创建的用户ljj默认存储的根文件夹路径为 /home/ljj
- 创建ftp用户ljj
useradd ljj
- 给用户ljj赋予密码,输入完后连续输入两次您需要的密码(它可能会提示你过于简单,不用管它)
passwd ljj
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6d171153641b242c6a962bf380dc9b1e.png)
其他
- vsftpd 安装后默只支持匿名 FTP 登录,用户如果试图使用 Linux 操作系统中的账号登录服务器,将会被 vsftpd 拒绝,但可以在 vsftpd 里配置用户账号和密码登录。
- java程序ftp连接参考工具类
package com.yj.oa.common.utils.ftp;
import com.yj.oa.common.utils.DateUtils;
import com.yj.oa.common.utils.HttpHeaderUtil;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
public class FtpUtil{
private static Logger log = LoggerFactory.getLogger(FtpUtil.class);
private static final String host = "171.24.165.240";
private static final int port = 21;
private static final String username = "ljj";
private static final String password = "1";
private static final String basePath = "/home/ljj/images";
public static final String filePath = "http://171.24.165.240:7777";
public static boolean uploadFile(Map<String, InputStream> FileMap) throws IOException
{
boolean result = false;
FTPClient ftp = new FTPClient();
InputStream input = null;
List<String> uploadS = new ArrayList<>();
if (getConnect(ftp))
{
int i = 0;
String fileName = "";
for (Map.Entry<String, InputStream> entry : FileMap.entrySet())
{
fileName = entry.getKey();
input = entry.getValue();
try
{
result = ftp.storeFile(fileName, input);
if (result)
{
i++;
uploadS.add(fileName);
}
}
catch (IOException e)
{
ftp.disconnect();
ftp.logout();
input.close();
return false;
}
finally
{
log.info("已成功上传" + (i) + " 个");
}
}
}
closeConnect(ftp);
input.close();
return result;
}
public static ResponseEntity<byte[]> fileDown(String fileId, String fileName) throws IOException
{
FTPClient ftp = new FTPClient();
boolean b = false;
if (getConnect(ftp))
{
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs)
{
if (ff.getName().equals(fileId))
{
log.info("$$$$$ 开始下载", DateUtils.DateToSTr(new Date()));
InputStream in = null;
ByteArrayOutputStream outputStream = null;
try
{
in = ftp.retrieveFileStream(ff.getName());
int len = 0;
byte[] buffer = new byte[1024];
outputStream = new ByteArrayOutputStream();
while ((len = in.read(buffer)) > 0)
{
outputStream.write(buffer, 0, len);
}
byte[] bytes = outputStream.toByteArray();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentDispositionFormData("attachment", getFileName(fileName));
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
log.info("$$$$$ 下载完成", DateUtils.DateToSTr(new Date()));
return new ResponseEntity<byte[]>(bytes, httpHeaders, HttpStatus.OK);
}
catch (IOException e)
{
e.printStackTrace();
}finally
{
closeConnect(ftp);
in.close();
outputStream.close();
}
}
}
}
return new ResponseEntity<byte[]>(null, new HttpHeaders(), HttpStatus.BAD_REQUEST);
}
public static boolean isHashFile(String filename) throws IOException
{
FTPClient ftp = new FTPClient();
boolean b = false;
if (getConnect(ftp))
{
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs)
{
if (ff.getName().equals(filename))
{
return true;
}
}
}
closeConnect(ftp);
return false;
}
public static boolean deleteFile(String[] FileName) throws IOException
{
FTPClient ftp = new FTPClient();
boolean bol = false;
if (getConnect(ftp))
{
int count = 0;
for (String fileName : FileName)
{
try
{
bol = ftp.deleteFile(fileName);
if (bol)
{
count++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
System.out.println("删除了:" + count + "个");
}
}
closeConnect(ftp);
}
return bol;
}
public static void closeConnect(FTPClient ftp) throws IOException
{
ftp.logout();
if (ftp.isConnected())
{
ftp.disconnect();
}
}
public static boolean getConnect(FTPClient ftp) throws IOException
{
ftp.setControlEncoding("UTF-8");
try
{
int reply;
ftp.connect(host, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
log.error("连接失败");
return false;
}
}
catch (IOException e)
{
e.printStackTrace();
}
ftp.changeWorkingDirectory(basePath);
ftp.enterLocalActiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
return true;
}
public static void mkdirs(String localPath)
{
java.io.File file2 = new java.io.File(localPath);
if (!file2.exists() && !file2.isDirectory())
{
file2.mkdirs();
}
}
public static String getFileName(String fileName) throws UnsupportedEncodingException
{
String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};
String userAgent = HttpHeaderUtil.getUserAgent();
for (String keyWord : IEBrowserKeyWords)
{
if (userAgent.contains(keyWord))
{
return URLEncoder.encode(fileName, "UTF-8");
}
}
return new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
}
参考文档