|
@@ -2,7 +2,6 @@ package com.java110.core.client;
|
|
|
|
|
|
|
|
import com.java110.utils.util.Base64Convert;
|
|
import com.java110.utils.util.Base64Convert;
|
|
|
import com.java110.utils.util.DateUtil;
|
|
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.FTP;
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
@@ -43,10 +42,11 @@ public class FtpUploadTemplate {
|
|
|
String userName, String userPassword, String ftpPath) {
|
|
String userName, String userPassword, String ftpPath) {
|
|
|
String fileName = "";
|
|
String fileName = "";
|
|
|
FTPClient ftpClient = null;
|
|
FTPClient ftpClient = null;
|
|
|
|
|
+ ByteArrayInputStream is = null;
|
|
|
try {
|
|
try {
|
|
|
ftpClient = new FTPClient();
|
|
ftpClient = new FTPClient();
|
|
|
// request.setCharacterEncoding("utf-8");
|
|
// request.setCharacterEncoding("utf-8");
|
|
|
- if(!ftpClient.isConnected()){
|
|
|
|
|
|
|
+ if (!ftpClient.isConnected()) {
|
|
|
ftpClient.connect(server, port);
|
|
ftpClient.connect(server, port);
|
|
|
}
|
|
}
|
|
|
ftpClient.login(userName, userPassword);
|
|
ftpClient.login(userName, userPassword);
|
|
@@ -86,9 +86,9 @@ public class FtpUploadTemplate {
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] context = Base64Convert.base64ToByte(imageBase64);
|
|
byte[] context = Base64Convert.base64ToByte(imageBase64);
|
|
|
- ByteArrayInputStream is = new ByteArrayInputStream(context);
|
|
|
|
|
|
|
+ is = new ByteArrayInputStream(context);
|
|
|
boolean saveFlag = ftpClient.storeFile(fileName, is);
|
|
boolean saveFlag = ftpClient.storeFile(fileName, is);
|
|
|
- is.close();
|
|
|
|
|
|
|
+
|
|
|
if (!saveFlag) {
|
|
if (!saveFlag) {
|
|
|
throw new IllegalArgumentException("存储文件失败");
|
|
throw new IllegalArgumentException("存储文件失败");
|
|
|
}
|
|
}
|
|
@@ -98,6 +98,9 @@ public class FtpUploadTemplate {
|
|
|
} finally {
|
|
} finally {
|
|
|
try {
|
|
try {
|
|
|
ftpClient.disconnect();
|
|
ftpClient.disconnect();
|
|
|
|
|
+ if (is != null) {
|
|
|
|
|
+ is.close();
|
|
|
|
|
+ }
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
logger.error("关闭ftpClient 失败", e);
|
|
logger.error("关闭ftpClient 失败", e);
|
|
@@ -117,7 +120,7 @@ public class FtpUploadTemplate {
|
|
|
try {
|
|
try {
|
|
|
// request.setCharacterEncoding("utf-8");
|
|
// request.setCharacterEncoding("utf-8");
|
|
|
ftpClient = new FTPClient();
|
|
ftpClient = new FTPClient();
|
|
|
- if(!ftpClient.isConnected()){
|
|
|
|
|
|
|
+ if (!ftpClient.isConnected()) {
|
|
|
ftpClient.connect(server, port);
|
|
ftpClient.connect(server, port);
|
|
|
}
|
|
}
|
|
|
ftpClient.login(userName, userPassword);
|
|
ftpClient.login(userName, userPassword);
|
|
@@ -166,7 +169,7 @@ public class FtpUploadTemplate {
|
|
|
FTPClient ftpClient = null;
|
|
FTPClient ftpClient = null;
|
|
|
try {
|
|
try {
|
|
|
ftpClient = new FTPClient();
|
|
ftpClient = new FTPClient();
|
|
|
- if(!ftpClient.isConnected()){
|
|
|
|
|
|
|
+ if (!ftpClient.isConnected()) {
|
|
|
ftpClient.connect(server, port);
|
|
ftpClient.connect(server, port);
|
|
|
}
|
|
}
|
|
|
ftpClient.login(userName, userPassword);
|
|
ftpClient.login(userName, userPassword);
|
|
@@ -212,18 +215,19 @@ public class FtpUploadTemplate {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
FtpUploadTemplate ftpUploadTemplate = new FtpUploadTemplate();
|
|
FtpUploadTemplate ftpUploadTemplate = new FtpUploadTemplate();
|
|
|
- String img = ftpUploadTemplate.download("/hc/img/20200518/","ed05abae-2eca-40ff-81a8-b586ff2e6a36.jpg",
|
|
|
|
|
- "118.89.243.11",617,"hcdemo","45j74jpWTf7bNhnC");
|
|
|
|
|
|
|
+ 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);
|
|
|
|
|
|
|
+ System.out.printf("img=" + img);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public String download(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
|
|
public String download(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
|
|
|
InputStream is = null;
|
|
InputStream is = null;
|
|
|
ByteArrayOutputStream bos = null;
|
|
ByteArrayOutputStream bos = null;
|
|
|
|
|
+ ByteArrayInputStream fis = null;
|
|
|
FTPClient ftpClient = new FTPClient();
|
|
FTPClient ftpClient = new FTPClient();
|
|
|
try {
|
|
try {
|
|
|
- if(!ftpClient.isConnected()){
|
|
|
|
|
|
|
+ if (!ftpClient.isConnected()) {
|
|
|
ftpClient.connect(server, port);
|
|
ftpClient.connect(server, port);
|
|
|
}
|
|
}
|
|
|
ftpClient.login(userName, userPassword);
|
|
ftpClient.login(userName, userPassword);
|
|
@@ -246,11 +250,9 @@ public class FtpUploadTemplate {
|
|
|
while (-1 != (length = is.read(buf, 0, buf.length))) {
|
|
while (-1 != (length = is.read(buf, 0, buf.length))) {
|
|
|
bos.write(buf, 0, length);
|
|
bos.write(buf, 0, length);
|
|
|
}
|
|
}
|
|
|
- ByteArrayInputStream fis = new ByteArrayInputStream(
|
|
|
|
|
|
|
+ fis = new ByteArrayInputStream(
|
|
|
bos.toByteArray());
|
|
bos.toByteArray());
|
|
|
bos.flush();
|
|
bos.flush();
|
|
|
- is.close();
|
|
|
|
|
- bos.close();
|
|
|
|
|
byte[] buffer = new byte[fis.available()];
|
|
byte[] buffer = new byte[fis.available()];
|
|
|
int offset = 0;
|
|
int offset = 0;
|
|
|
int numRead = 0;
|
|
int numRead = 0;
|
|
@@ -260,18 +262,32 @@ public class FtpUploadTemplate {
|
|
|
if (offset != buffer.length) {
|
|
if (offset != buffer.length) {
|
|
|
throw new IOException("Could not completely read file ");
|
|
throw new IOException("Could not completely read file ");
|
|
|
}
|
|
}
|
|
|
- fis.close();
|
|
|
|
|
return Base64Convert.byteToBase64(buffer);
|
|
return Base64Convert.byteToBase64(buffer);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
logger.error("ftp通过文件名称获取远程文件流", e);
|
|
logger.error("ftp通过文件名称获取远程文件流", e);
|
|
|
} finally {
|
|
} finally {
|
|
|
- try {
|
|
|
|
|
- if(bos != null){
|
|
|
|
|
- bos.close();
|
|
|
|
|
|
|
+ if (bos != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ bos.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- if(is !=null) {
|
|
|
|
|
- is.close();
|
|
|
|
|
|
|
+ if (is != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ is.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ if(fis != null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ fis.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
closeConnect(ftpClient);
|
|
closeConnect(ftpClient);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|