工作需要自己搭建了一台ftp服务器,用作测试。
以下为ftp工具类:
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
public class FtpUtil {
public static FTPClient ftpConnection(String url, String username, String password) throws IOException {
FTPClient ftpClient = new FTPClient();
String[] split = url.split(":");
if (split.length != 2){
System.out.println("--Connection error-->" + "ftp url is error");
}
String ip = split[0];
String port = split[1];
try {
ftpClient.connect(ip, Integer.parseInt(port));
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
ftpClient.login(username, password);
}
int replyCode = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)) {
String replyString = ftpClient.getReplyString();
System.out.println(replyString);
ftpClient.disconnect();
System.exit(1);
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory("/var/ftp/local");
} catch (IOException e) {
e.printStackTrace();
}
return ftpClient;
}
public static void close(FTPClient ftpClient) throws IOException{
if(ftpClient != null && ftpClient.isConnected()){
ftpClient.logout();
ftpClient.disconnect();
}
}
public static void upload(FTPClient ftpClient, File f) throws IOException {
if (f.isDirectory()){
ftpClient.makeDirectory(f.getName());
ftpClient.changeWorkingDirectory(f.getName());
String[] files = f.list();
for(String fstr : files){
File file1 = new File(f.getPath()+"/"+fstr);
if (file1.isDirectory()){
upload(ftpClient, file1);
ftpClient.changeToParentDirectory();
}else {
FileInputStream input = new FileInputStream(file1);
ftpClient.storeFile(file1.getName(), input);
input.close();
}
}
}else {
ftpClient.changeWorkingDirectory("/var/ftp/local");
FileInputStream input = new FileInputStream(f);
boolean b = ftpClient.storeFile(f.getName(), input);
input.close();
}
}
public static boolean deleteFile(FTPClient ftpClient, String workingDirectory, String fileName){
try {
if (StringUtils.isNotBlank(workingDirectory)){
ftpClient.changeWorkingDirectory(workingDirectory);
}
return ftpClient.deleteFile(fileName);
}catch (Exception e){
InetAddress remoteAddress = ftpClient.getRemoteAddress();
System.out.println(remoteAddress.getHostAddress());
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
try {
FTPClient ftpClient = ftpConnection("192.168.199.128:21", "ftpuser", "ftpuser");
downFile(ftpClient, "CH05_20220214115818.xml", "E:\\FTP-TEST");
} catch (IOException e) {
e.printStackTrace();
}
}
public static String downFile(FTPClient ftpClient, String fileAbsPath, String localUrl) throws IOException {
OutputStream os = null;
System.out.println(fileAbsPath.lastIndexOf("/"));
String fileName = fileAbsPath.substring(fileAbsPath.lastIndexOf("/")+1);
File localFile = new File(localUrl + "/" + fileName);
if (!localFile.getParentFile().exists()){
localFile.getParentFile().mkdirs();
localFile.createNewFile();
}
os = new FileOutputStream(localFile);
boolean isTrue = ftpClient.retrieveFile(new String(fileAbsPath.getBytes(), StandardCharsets.UTF_8), os);
if (!isTrue){
System.out.println("--error-->" + ftpClient.getReplyString());
}
os.close();
return localFile.getAbsolutePath();
}
}
使用该工具类上传的文件,再根据此工具类的下载方法下载是没问题的;
若将需要下载的文件直接使用服务器上传(rz)后,这时再使用该工具类下载会报错:
550 Filed to open file
之后排查原来是该文件的权限问题,使用该工具类上传时该文件的权限与通过服务器直接上传的权限不同,若通过服务器直接上传后更改该文件的权限:
[root@localhost local]# chmod 777 CH05_20220214115818.xml
之后再使用该工具类下载则没有问题,归根结底还是文件权限的问题, 特此记录。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)