本文主要是介绍java 从ftp服务器获取图片并转化为base64,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.xxkj.mdoc.dingding.utils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;
import java.io.*;
/**
* FTP下载工具
* @author licong
* @date 2021/7/16
*/
@Component
public class FTPUtil {
/**
* 日志对象
**/
private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class);
/**
* 该目录不存在
*/
public static final String DIR_NOT_EXIST = "该目录不存在";
/**
* "该目录下没有文件
*/
public static final String DIR_CONTAINS_NO_FILE = "该目录下没有文件";
/**
* 本地字符编码
**/
private static String localCharset = "GBK";
/**
* FTP协议里面,规定文件名编码为iso-8859-1
**/
private static String serverCharset = "ISO-8859-1";
/**
* UTF-8字符编码
**/
private static final String CHARSET_UTF8 = "UTF-8";
/**
* OPTS UTF8字符串常量
**/
private static final String OPTS_UTF8 = "OPTS UTF8";
/**
* 设置缓冲区大小4M
**/
private static final int BUFFER_SIZE = 1024 * 1024 * 4;
/**
* FTPClient对象
**/
private static FTPClient ftpClient = null;
/**
* 连接FTP服务器
*
* @param address 地址,如:127.0.0.1
* @param port 端口,如:21
* @param username 用户名,如:root
* @param password 密码,如:root
*/
private static void login(String address, int port, String username, String password) {
ftpClient = new FTPClient();
try {
ftpClient.connect(address, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//限制缓冲区大小
ftpClient.setBufferSize(BUFFER_SIZE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeConnect();
logger.error("FTP服务器连接失败");
}
} catch (Exception e) {
logger.error("FTP登录失败", e);
}
}
/**
* FTP服务器路径编码转换
*
* @param ftpPath FTP服务器路径
* @return String
*/
private static String changeEncoding(String ftpPath) {
String directory = null;
try {
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) {
localCharset = CHARSET_UTF8;
}
directory = new String(ftpPath.getBytes(localCharset), serverCharset);
} catch (Exception e) {
logger.error("路径编码转换失败", e);
}
return directory;
}
/**
* 关闭FTP连接
*/
private static void closeConnect() {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
logger.error("关闭FTP连接失败", e);
}
}
}
/**
* 检查指定目录下是否含有指定文件
*
* @param ftpPath FTP服务器文件相对路径,例如:test/123
* @param fileName 要下载的文件名,例如:test.txt
* @return 成功返回true,否则返回false
*/
public static String checkFileInFtp(String address, int port, String username, String password,String ftpPath, String fileName) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
String re=null;
// 登录
login(address, port, username, password);
if (ftpClient != null) {
try {
String path = changeEncoding(ftpPath);
// 判断是否存在该目录
if (!ftpClient.changeWorkingDirectory(path)) {
logger.error(ftpPath + DIR_NOT_EXIST);
return re;
}
ftpClient.enterLocalPassiveMode(); // 设置被动模式,开通一个端口来传输数据
String[] fs = ftpClient.listNames();
// 判断该目录下是否有文件
if (fs == null || fs.length == 0) {
logger.error(ftpPath + DIR_CONTAINS_NO_FILE);
return re;
}
for (String ff : fs) {
String ftpName = new String(ff.getBytes(serverCharset), localCharset);
if (ftpName.equals(fileName)) {
ftpClient.retrieveFile(fileName,out);
break;
}
}
byte[] bytes = out.toByteArray();
BASE64Encoder encoder=new BASE64Encoder();
re=encoder.encode(bytes);
} catch (IOException e) {
logger.error("请求出错", e);
}finally {
closeConnect();
//关闭流
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return re;
}
public static void main(String[] args) throws IOException{
String a = checkFileInFtp("127.0.0.1",21,"anonymous","licong","/","qwer.pdf");
System.out.println(a );
}
}
这篇关于java 从ftp服务器获取图片并转化为base64的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!