Java实现FTP上传文件
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class FtpUtil {
public static FTPClient connection(String hostname,int port,String username,String password){
FTPClient ftp = new FTPClient();
try {
//连接FTP服务器
ftp.connect(hostname, port);
//下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
//登录ftp
ftp.login(username, password);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
}
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return ftp;
}
public static void uploadFile(FTPClient ftpClient, String path, String directory, String fileName, InputStream inputStream){
try {
//让客户端告诉服务端开通一个端口用来数据传输(必须要 不然会一直卡死)
ftpClient.enterLocalPassiveMode();
//循环生成目录并进入
stringPath(ftpClient,path,directory);
//如果缺省该句 传输txt正常 但图片和其他格式的文件传输出现乱码
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//开始上传
ftpClient.storeFile(new String(fileName.getBytes("GBK"), StandardCharsets.ISO_8859_1), inputStream);
//关闭输入流
inputStream.close();
} catch (Exception e) {
log.error(e.getMessage(),e);
}
}
public static void stringPath(FTPClient ftpClient, String path,String directory) throws IOException {
List<String> string= Lists.newArrayList();
Arrays.stream((path+directory).split("/")).filter(StringUtils::isNotBlank).forEach(string::add);
StringBuilder paths = new StringBuilder();
for (String s : string) {
paths.append("/").append(new String(s.getBytes("GBK"), StandardCharsets.ISO_8859_1));
if(!ftpClient.changeWorkingDirectory(paths.toString())){
ftpClient.makeDirectory(paths.toString());
}
}
ftpClient.changeWorkingDirectory(paths.toString());
}
public static void ftpLogout(FTPClient ftpClient){
try {
ftpClient.logout();
} catch (IOException e) {
log.error(e.getMessage(),e);
}
};
public static void main(String[] args) {
String hostname="127.0.0.1";
int port =9100;
String username="admin";
String password="12345678";
String path="/usr/local/file/";
String directory="wenzhang";
String fileName="myFile.xlsx";
//这里写文件的输入流
InputStream inputStream = null;
//连接ftp服务
FTPClient ftpClient = FtpUtil.connection(hostname, port, username,password);
//上传ftp文件
FtpUtil.uploadFile(ftpClient, path, directory, fileName, inputStream);
//退出ftp
FtpUtil.ftpLogout(ftpClient);
}
}
依赖
<!--FTP传输所需协议-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
文章来源地址https://www.toymoban.com/news/detail-510615.html
文章来源:https://www.toymoban.com/news/detail-510615.html
到了这里,关于Java实现FTP上传文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!