From 91f58ac34a01db7bb4e30a57af4454e0c36fd1c9 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 14 六月 2020 13:06:21 +0800
Subject: [PATCH] 处理服务启动失败问题
---
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 98 insertions(+), 5 deletions(-)
diff --git a/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java b/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
index 1ced81b..9ffee77 100644
--- a/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
+++ b/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
@@ -2,6 +2,7 @@
import com.java110.utils.util.Base64Convert;
import com.java110.utils.util.DateUtil;
+import com.tencentcloudapi.tci.v20190318.models.FaceExpressionResult;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
@@ -45,7 +46,9 @@
try {
ftpClient = new FTPClient();
// request.setCharacterEncoding("utf-8");
- ftpClient.connect(server, port);
+ if(!ftpClient.isConnected()){
+ ftpClient.connect(server, port);
+ }
ftpClient.login(userName, userPassword);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
@@ -66,6 +69,9 @@
fileName += ".jpg";
} else if (imageBase64.contains("data:image/webp;base64,")) {
imageBase64 = imageBase64.replace("data:image/webp;base64,", "");
+ fileName += ".jpg";
+ } else if (imageBase64.contains("data:application/octet-stream;base64,")) {
+ imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", "");
fileName += ".jpg";
} else {
fileName += ".jpg";
@@ -111,7 +117,9 @@
try {
// request.setCharacterEncoding("utf-8");
ftpClient = new FTPClient();
- ftpClient.connect(server, port);
+ if(!ftpClient.isConnected()){
+ ftpClient.connect(server, port);
+ }
ftpClient.login(userName, userPassword);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
@@ -158,7 +166,9 @@
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
- ftpClient.connect(server, port);
+ if(!ftpClient.isConnected()){
+ ftpClient.connect(server, port);
+ }
ftpClient.login(userName, userPassword);
ftpClient.enterLocalPassiveMode();
if (ftpClient != null) {
@@ -167,16 +177,29 @@
FTP.DEFAULT_CONTROL_ENCODING);//闃叉涔辩爜
InputStream ins = ftpClient.retrieveFileStream(f);//闇�浣跨敤file.getName鑾峰�硷紝鑻ョ敤f浼氫贡鐮�
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
- byte[] buf = new byte[204800];
+ byte[] buf = new byte[2048];
int bufsize = 0;
while (ins != null && (bufsize = ins.read(buf, 0, buf.length)) != -1) {
byteOut.write(buf, 0, bufsize);
}
- return_arraybyte = byteOut.toByteArray();
+ //return_arraybyte = byteOut.toByteArray();
+ ByteArrayInputStream fis = new ByteArrayInputStream(byteOut.toByteArray());
+ byteOut.flush();
byteOut.close();
+ byte[] buffer = new byte[fis.available()];
+ int offset = 0;
+ int numRead = 0;
+ while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
+ offset += numRead;
+ }
+ if (offset != buffer.length) {
+ throw new IOException("Could not completely read file ");
+ }
+ fis.close();
if (ins != null) {
ins.close();
}
+ return_arraybyte = buffer;
}
} catch (Exception e) {
e.printStackTrace();
@@ -187,6 +210,76 @@
return return_arraybyte;
}
+ public static void main(String[] args) {
+ FtpUploadTemplate ftpUploadTemplate = new FtpUploadTemplate();
+ String img = ftpUploadTemplate.download("/hc/img/20200518/","ed05abae-2eca-40ff-81a8-b586ff2e6a36.jpg",
+ "118.89.243.11",617,"hcdemo","45j74jpWTf7bNhnC");
+
+ System.out.printf("img="+img);
+ }
+
+ public String download(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
+ InputStream is = null;
+ ByteArrayOutputStream bos = null;
+ FTPClient ftpClient = new FTPClient();
+ try {
+ if(!ftpClient.isConnected()){
+ ftpClient.connect(server, port);
+ }
+ ftpClient.login(userName, userPassword);
+ ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
+ ftpClient.enterLocalPassiveMode();
+ int reply = ftpClient.getReplyCode();
+ if (!FTPReply.isPositiveCompletion(reply)) {
+ ftpClient.disconnect();
+ }
+ String f = new String(
+ (remotePath + fileName).getBytes("GBK"),
+ FTP.DEFAULT_CONTROL_ENCODING);
+ is = ftpClient.retrieveFileStream(f);// 鑾峰彇杩滅▼ftp涓婃寚瀹氭枃浠剁殑InputStream
+ if (null == is) {
+ throw new FileNotFoundException(remotePath);
+ }
+ bos = new ByteArrayOutputStream();
+ int length;
+ byte[] buf = new byte[2048];
+ while (-1 != (length = is.read(buf, 0, buf.length))) {
+ bos.write(buf, 0, length);
+ }
+ ByteArrayInputStream fis = new ByteArrayInputStream(
+ bos.toByteArray());
+ bos.flush();
+ is.close();
+ bos.close();
+ byte[] buffer = new byte[fis.available()];
+ int offset = 0;
+ int numRead = 0;
+ while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
+ offset += numRead;
+ }
+ if (offset != buffer.length) {
+ throw new IOException("Could not completely read file ");
+ }
+ fis.close();
+ return Base64Convert.byteToBase64(buffer);
+ } catch (Exception e) {
+ logger.error("ftp閫氳繃鏂囦欢鍚嶇О鑾峰彇杩滅▼鏂囦欢娴�", e);
+ } finally {
+ try {
+ if(bos != null){
+ bos.close();
+ }
+ if(is !=null) {
+ is.close();
+ }
+ closeConnect(ftpClient);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+
public void closeConnect(FTPClient ftpClient) {
try {
ftpClient.disconnect();
--
Gitblit v1.8.0