JAVA工具类
目录:
- 根据图片URL下载图片,并转二进制数组
- 根据图片URL下载图片,并转base64编码
- 图片上传FTP服务器工具类
1、根据图片URL下载图片,并转二进制数组
/**
* 将url上图片转二进制数组
* @param url 图片地址
* @return Base64图片
* @throws Exception 异常
* @请求的方法为: byte[] imageData = urlImageToByteArray(new URL(图片URL));
*/
public static byte[] urlImageToByteArray(URL url) throws Exception {
//打开链接
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//超时响应时间为3秒
connection.setConnectTimeout(3000);
InputStream inStream = connection.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
byte[] picData = new byte[0];
picData = outStream.toByteArray();
if(picData.length == 0){
log.error("异常信息:下载的图片数据大小为0");
}
return picData;
}
2、根据图片URL下载图片,并转base64编码
/**
* 将url上图片转Base64
* @param url 图片地址
* @return Base64图片
* @throws Exception 异常
* @请求的方法为: byte[] imageData = urlImageToByteArray(new URL(图片URL));
*/
public static String urlImageToBase64(URL url) throws Exception {
//打开链接
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//超时响应时间为3秒
connection.setConnectTimeout(3000);
InputStream inStream = connection.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
//关闭输入流*
inStream.close();
byte[] picData = new byte[0];
picData = outStream.toByteArray();
if(picData.length == 0){
log.error("异常信息:下载的图片数据大小为0");
}
//进行base64位编码,编码后不带 data:image/jpeg;base64,
String base64Image = Base64.getEncoder().encodeToString(picData);
return base64Image;
}
3、图片上传FTP服务器工具类
3.1 ftpUtil.java类
import lombok.extern.slf4j.Slf4j;
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.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@Component
@Scope("prototype")
public class FtpUtil {
/**
* 默认用户名
*/
public static final String ANONYMOUS_LOGIN = "anonymous";
/**
* 日志对象
*/
// private static log log = LogManager.getlog(FtpUtil.class.getName());
/**
* FTP客户端对象
*/
private FTPClient ftp;
/**
* 是否已经连接
*/
private boolean is_connected;
private static Map<String, Boolean> pathMap = new ConcurrentHashMap<>(50);
public FtpUtil() {
ftp = new FTPClient();
is_connected = false;
ftp.setDefaultTimeout(90 * 1000);
ftp.setConnectTimeout(90 * 1000);
ftp.setDataTimeout(90 * 1000);
}
public FtpUtil(int defaultTimeoutSecond, int connectTimeoutSecond, int dataTimeoutSecond) {
ftp = new FTPClient();
is_connected = false;
ftp.setDefaultTimeout(defaultTimeoutSecond * 1000);
ftp.setConnectTimeout(connectTimeoutSecond * 1000);
ftp.setDataTimeout(dataTimeoutSecond * 1000);
}
/**
* Connects to FTP server.
*
* @param host FTP server address or name
* @param port FTP server port
* @param user user name
* @param password user password
* @param isTextMode text / binary mode switch
* @throws IOException on I/O errors
*/
public synchronized void connect(String host, int port, String user, String password, boolean isTextMode) throws IOException {
if (StringUtils.isBlank(host) || StringUtils.isBlank(user) || StringUtils.isBlank(password)) {
log.error("FtpUtil.connect():Host userName and password can not be empty.");
return;
}
// Connect to server.
try {
ftp.connect(host, port);
} catch (UnknownHostException ex) {
ex.printStackTrace();
throw new IOException("Failed to find the correct FTP server host, port, user, password, etc.");
}
// Check rsponse after connection attempt.
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnect();
throw new IOException("Failed to find the correct FTP server host, port, user, password, etc.");
}
if (StringUtils.isBlank(user)) {
user = ANONYMOUS_LOGIN;
}
// Login.
if (!ftp.login(user, password)) {
is_connected = false;
disconnect();
throw new IOException("Can't login to server host.");
} else {
is_connected = true;
}
// Set data transfer mode.
if (isTextMode) {
ftp.setFileType(FTP.ASCII_FILE_TYPE);
} else {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
log.debug("调试信息:Ftp连接成功。Ftp模式:" + FTP.BINARY_FILE_TYPE);
}
/**
* Uploads the file to the FTP server.
*
* @param ftpFileName server file name (with absolute path)
* @param localFile local file to upload
* @throws IOException on I/O errors
*/
public void upload(String ftpFileName, File localFile) throws IOException {
if (StringUtils.isBlank(ftpFileName) || null == localFile) {
log.error("FtpUtil.upload(String, File): arguments can not be null but " +
"ftpFileName is " + ftpFileName + ", localFile is " + localFile);
return;
}
// File check.
if (!localFile.exists()) {
throw new IOException("Can't upload '" + localFile.getAbsolutePath() + "'. This file doesn't exist.");
}
// Upload.
InputStream in = null;
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
in = new BufferedInputStream(new FileInputStream(localFile));
if (!ftp.storeFile(ftpFileName, in)) {
throw new IOException(
"Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
}
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
}
/**
* Uploads the file to the FTP server.
*
* @param ftpFileName server file name (with absolute path)
* @param strFile local String
* @throws IOException on I/O errors
*/
public void upload(String ftpFileName, String strFile) throws IOException {
if (StringUtils.isBlank(ftpFileName) || StringUtils.isBlank(strFile)) {
log.error("FtpUtil.upload(String, String): argument can not be null but " +
"ftpFileName is " + ftpFileName + ", strFile is " + strFile);
return;
}
// Upload.
InputStream in = null;
try {
ByteArrayInputStream is = new ByteArrayInputStream(strFile.getBytes("UTF-8"));
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
in = new BufferedInputStream(is);
if (!ftp.storeFile(ftpFileName, in)) {
throw new IOException(
"Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
}
}
/**
* 创建FTP目录
* @return
*/
private void createFtpPath(String strPath) throws IOException {
String[] strDirs=strPath.split("/");
String dir = "";
for (int i = 0; i < strDirs.length; i++) {
if (i > 0) {
dir += "/";
}
dir += strDirs[i];
if (null == pathMap.get(dir)) {
if (ftp.changeWorkingDirectory(strPath)) {
pathMap.put(dir, true);
log.debug("创建目录成功");
ftp.changeWorkingDirectory("/");
} else {
if(ftp.makeDirectory(dir)) {
pathMap.put(dir, true);
log.debug("创建目录成功");
} else {
}
}
}
}
}
/**
* Uploads the file to the FTP server.
*
* @param ftpFileName server file name (相对FTP的路径 + 文件名)
* @param bData local data
* @throws IOException on I/O errors
*/
public boolean upload(String filePath, String ftpFileName, byte[] bData) throws IOException {
if (StringUtils.isBlank(ftpFileName) || null == bData) {
log.error("FtpUtil.upload(String, byte[]) arguments can not be null but" +
" ftpFileName is " + ftpFileName + ", bData is " + bData);
return false;
}
// Upload.
InputStream in = null;
boolean success = false;
try {
if (null != pathMap && null == pathMap.get(filePath)) {
//先判断是否有该目录
createFtpPath(filePath);
}
ByteArrayInputStream is = new ByteArrayInputStream(bData);
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
in = new BufferedInputStream(is);
//ftp.changeWorkingDirectory("/");
if (!ftp.storeFile(ftpFileName, in)) {
log.error("ftp上传失败");
return false;
}
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
log.debug("ftp上传成功");
return true;
}
/**
* Downloads the file from the FTP server.
*
* @param ftpFileName server file name (with absolute path)
* @param localFile local file to download into
* @throws IOException on I/O errors
*/
public void download(String ftpFileName, File localFile) throws IOException {
if (StringUtils.isBlank(ftpFileName) || null == localFile) {
log.error("FtpUtil.download() arguments can not be null but " +
"ftpFileName is " + ftpFileName + ", localFile is " + localFile);
return;
}
// Download.
OutputStream out = null;
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
// Get file info.
FTPFile[] fileInfoArray = ftp.listFiles(ftpFileName);
if (fileInfoArray == null) {
throw new FileNotFoundException("File " + ftpFileName + " was not found on FTP server.");
}
// Check file size.
FTPFile fileInfo = fileInfoArray[0];
long size = fileInfo.getSize();
if (size > Integer.MAX_VALUE) {
throw new IOException("File " + ftpFileName + " is too large.");
}
// Download file.
out = new BufferedOutputStream(new FileOutputStream(localFile));
ftp.enterLocalPassiveMode();
if (!ftp.retrieveFile(ftpFileName, out)) {
throw new IOException(
"Error loading file " + ftpFileName + " from FTP server. Check FTP permissions and path.");
}
out.flush();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
}
}
/**
* Removes the file from the FTP server.
*
* @param ftpFileName server file name (with absolute path)
* @throws IOException on I/O errors
*/
public void remove(String ftpFileName) throws IOException {
if (StringUtils.isBlank(ftpFileName)) {
log.error("FtpUtil.remove(String): argument ftpFileName is " + ftpFileName);
return;
}
ftp.enterLocalPassiveMode();
if (!ftp.deleteFile(ftpFileName)) {
throw new IOException("Can't remove file '" + ftpFileName + "' from FTP server.");
}
}
/**
* Lists the files in the given FTP directory.
*
* @param filePath absolute path on the server
* @return files relative names list
* @throws IOException on I/O errors
*/
public List<String> list(String filePath) throws IOException {
if (StringUtils.isBlank(filePath)) {
log.error("FtpUtil.list(String) argument filePath can not be null but filePath is " + filePath);
return null;
}
List<String> fileList = new ArrayList<String>();
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftp.listFiles(filePath);
int size = (ftpFiles == null) ? 0 : ftpFiles.length;
for (int i = 0; i < size; i++) {
FTPFile ftpFile = ftpFiles[i];
if (ftpFile.isFile()) {
fileList.add(ftpFile.getName());
}
}
return fileList;
}
/**
* Sends an FTP Server site specific command
*
* @param args site command arguments
* @throws IOException on I/O errors
*/
public void sendSiteCommand(String args) throws IOException {
if (StringUtils.isBlank(args)) {
log.error("FtpUtil.sendSiteCommand(String): argument args is " + args);
return;
}
if (ftp.isConnected()) {
try {
ftp.sendSiteCommand(args);
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
}
/**
* Disconnects from the FTP server
*
* @throws IOException on I/O errors
*/
public void disconnect() {
try {
ftp.logout();
ftp.disconnect();
is_connected = false;
} catch (Exception ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
/**
* Makes the full name of the file on the FTP server by joining its path and
* the local file name.
*
* @param ftpPath file path on the server
* @param localFile local file
* @return full name of the file on the FTP server
*/
public String makeFTPFileName(String ftpPath, File localFile) {
if (StringUtils.isBlank(ftpPath)) {
return localFile.getName();
} else {
String path = ftpPath.trim();
if (path.charAt(path.length() - 1) != '/') {
path = path + "/";
}
return path + localFile.getName();
}
}
/**
* Test coonection to ftp server
*
* @return true, if connected
*/
public boolean isConnected() {
return is_connected;
}
/**
* Get current directory on ftp server
*
* @return current directory
*/
public synchronized String getWorkingDirectory() {
if (!is_connected) {
return "";
}
try {
return ftp.printWorkingDirectory();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
return "";
}
/**
* Set working directory on ftp server
*
* @param dir new working directory
* @return true, if working directory changed
*/
public synchronized boolean setWorkingDirectory(String dir) {
if (StringUtils.isBlank(dir)) {
log.error( "FtpUtil.setWorkingDirectory(String): argument dir is " + dir);
return false;
}
if (!is_connected) {
return false;
}
try {
return ftp.changeWorkingDirectory(dir);
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
return false;
}
/**
* Change working directory on ftp server to parent directory
*
* @return true, if working directory changed
*/
public boolean setParentDirectory() {
if (!is_connected) {
return false;
}
try {
return ftp.changeToParentDirectory();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
return false;
}
/**
* Get parent directory name on ftp server
*
* @return parent directory
*/
public String getParentDirectory() {
if (!is_connected) {
return "";
}
String w = getWorkingDirectory();
setParentDirectory();
String p = getWorkingDirectory();
setWorkingDirectory(w);
return p;
}
public void uploadBytes(String filePath, String oldFileName, byte[] data) throws IOException {
if (null != pathMap && null == pathMap.get(filePath)) {
//先判断是否有该目录
createFtpPath(filePath);
}
try(ByteArrayInputStream inputStream = new ByteArrayInputStream(data)) {
ftp.enterLocalPassiveMode();
String newFileName = oldFileName + ".json";
ftp.storeFile(oldFileName, inputStream);
ftp.rename(oldFileName, newFileName);
}
}
// /**
// * Get directory contents on ftp server
// *
// * @param filePath
// * directory
// * @return list of FTPFileInfo structures
// * @throws IOException
// */
// public List<FfpFileInfo> listFiles(String filePath) throws IOException {
// List<FfpFileInfo> fileList = new ArrayList<FfpFileInfo>();
//
// // Use passive mode to pass firewalls.
// ftp.enterLocalPassiveMode();
// FTPFile[] ftpFiles = ftp.listFiles(filePath);
// int size = (ftpFiles == null) ? 0 : ftpFiles.length;
// for (int i = 0; i < size; i++) {
// FTPFile ftpFile = ftpFiles[i];
// FfpFileInfo fi = new FfpFileInfo();
// fi.setName(ftpFile.getName());
// fi.setSize(ftpFile.getSize());
// fi.setTimestamp(ftpFile.getTimestamp());
// fi.setType(ftpFile.isDirectory());
// fileList.add(fi);
// }
//
// return fileList;
// }
/**
* Get file from ftp server into given output stream
*
* @param ftpFileName file name on ftp server
* @param out OutputStream
* @throws IOException
*/
public void getFile(String ftpFileName, OutputStream out) throws IOException {
if (StringUtils.isBlank(ftpFileName) || null == out) {
log.error("FtpUtil.getFile(): arguments can not be null but " +
"ftpFileName is " + ftpFileName + ", out is " + out);
return;
}
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
// Get file info.
FTPFile[] fileInfoArray = ftp.listFiles(ftpFileName);
if (fileInfoArray == null) {
throw new FileNotFoundException("File '" + ftpFileName + "' was not found on FTP server.");
}
// Check file size.
FTPFile fileInfo = fileInfoArray[0];
long size = fileInfo.getSize();
if (size > Integer.MAX_VALUE) {
throw new IOException("File '" + ftpFileName + "' is too large.");
}
// Download file.
if (!ftp.retrieveFile(ftpFileName, out)) {
throw new IOException(
"Error loading file '" + ftpFileName + "' from FTP server. Check FTP permissions and path.");
}
out.flush();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
}
}
/**
* Put file on ftp server from given input stream
*
* @param ftpFileName file name on ftp server
* @param in InputStream
* @throws IOException
*/
public void putFile(String ftpFileName, InputStream in) throws IOException {
if (StringUtils.isBlank(ftpFileName) || null == in) {
log.error("FtpUtil.putFile(String, InputStream): arguments can not be null but " +
"ftpFileName is " + ftpFileName + ", in is " + in);
return;
}
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
if (!ftp.storeFile(ftpFileName, in)) {
throw new IOException(
"Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
}
} finally {
try {
in.close();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
}
}
/**
* @param remoteDirectoryPath
* @author
*/
public void createDirectory(String remoteDirectoryPath) throws IOException {
log.debug("" + remoteDirectoryPath);
ftp.makeDirectory(remoteDirectoryPath);
}
/**
* Put file on ftp server from given input stream
*
* @param ftpFileName - file name on ftp server
* @param in-InputStream
* @return bIsRename-true上传成功;false-上传失败
* @throws IOException
*/
public synchronized boolean putFileV2(String ftpFileName, InputStream in) throws IOException {
if (StringUtils.isBlank(ftpFileName) || null == in) {
log.error("FtpUtil.putFileV2(String, InputStream): argument can not be null but" +
"ftpFileName is " + ftpFileName + ", in is " + in);
return false;
}
boolean bIsPutFileV2 = true;
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
if (!ftp.storeFile(ftpFileName, in)) {
bIsPutFileV2 = false;
throw new IOException(
"Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
}
} finally {
try {
in.close();
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
return bIsPutFileV2;
}
}
/**
* Put file on ftp server from given input stream
*
* @param ftpFileName - file name on ftp server
* @param in-InputStream
* @return bIsRename-true上传成功;false-上传失败
* @throws IOException
*/
public synchronized boolean putFileV3(String ftpFilePath, String ftpFileName, InputStream in) throws IOException {
log.debug("start sending");
if (StringUtils.isBlank(ftpFilePath) || StringUtils.isBlank(ftpFileName) || null == in) {
log.error("FtpUtil.putFileV3(String, String, InputStream): arguments can not be null but" +
"ftpFilePath is " + ftpFilePath + ", ftpFileName is " + ftpFileName + ", in is " + in);
return false;
}
boolean bIsPutFileV2 = true;
try {
// Use passive mode to pass firewalls.
ftp.enterLocalPassiveMode();
if (null != pathMap && null == pathMap.get(ftpFilePath)) {
//先判断是否有该目录
ftp.mkd(ftpFilePath);
}
if (!ftp.storeFile(ftpFilePath + File.separator + ftpFileName, in)) {
bIsPutFileV2 = false;
throw new IOException(
"Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
}
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("异常信息:" + ex.getMessage(), ex);
}
return bIsPutFileV2;
}
}
/**
* 重命名
*
* @param remoteOldPath
* @param remoteNewPath
* @return bIsRename-true更名成功;false-更名失败
* @throws IOException
*/
public synchronized boolean rename(String remoteOldPath, String remoteNewPath) throws IOException {
boolean bIsRename = false;
if (ftp == null) {
return bIsRename;
}
bIsRename = ftp.rename(remoteOldPath, remoteNewPath);
log.debug("上传重命名" + (bIsRename ? "成功" : "失败"));
return bIsRename;
}
public boolean removeDirectory(String path) throws IOException {
if (StringUtils.isBlank(path)) {
log.error("FtpUtil.removeDirectory(String): argument path is " + path);
return false;
}
return ftp.removeDirectory(path);
}
@Scheduled(cron = "0 0 0 1/2 * ?")
public void reCreateMap() {
pathMap = new ConcurrentHashMap<>(50);
}
}
3.2 ftpService.java 类
@Slf4j
@Service
public class FtpService {
@Autowired
private BaseConfig baseConfig;
/**
* @description: 上传图片到FTP服务器
*
* @param: fileName 文件名 (eg:test.jpg)
* @param: imageData 图片二进制数组
* @Exception
* @Return boolean
*/
public boolean uploadImage(String fileName, byte[] imageData){
FtpUtil ftpUtil = new FtpUtil();
boolean upload;
//登陆连接
try {
ftpUtil.connect(baseConfig.getFtpIp(), baseConfig.getFtpPort(), baseConfig.getUserName(), baseConfig.getPassword(), false);
if (ftpUtil.isConnected()) {
upload = ftpUtil.upload(baseConfig.getUploadPath(), fileName, imageData);
if(upload){
log.info("上传成功!imageName是:" + fileName);
ftpUtil.disconnect();
}else {
log.error("上传失败!imageName是:" + fileName);
Map<String, byte[]> map = new HashMap<>();
map.put(fileName, imageData);
//入队
BlockingQueueConfig.faceImageQueue.put(map);
return false;
}
}else {
ftpUtil.connect(baseConfig.getFtpIp(), baseConfig.getFtpPort(), baseConfig.getUserName(), baseConfig.getPassword(), false);
if(ftpUtil.isConnected()){
log.info("重新连接FTP服务器成功!");
}else {
log.error("重新连接FTP服务器失败!");
}
}
} catch (IOException e) {
log.error("连接FTP服务器失败:{}", e.getMessage());
} catch (InterruptedException e) {
log.error("上传失败的图片重新入队异常:{}", e.getMessage());
}
return true;
}
}