java110
2020-12-24 66119b66f1009157d662c9e9869e4c04f9472bf2
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
@@ -2,7 +2,6 @@
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;
@@ -43,10 +42,11 @@
                         String userName, String userPassword, String ftpPath) {
        String fileName = "";
        FTPClient ftpClient = null;
        ByteArrayInputStream is = null;
        try {
            ftpClient = new FTPClient();
            // request.setCharacterEncoding("utf-8");
            if(!ftpClient.isConnected()){
            if (!ftpClient.isConnected()) {
                ftpClient.connect(server, port);
            }
            ftpClient.login(userName, userPassword);
@@ -86,9 +86,9 @@
            byte[] context = Base64Convert.base64ToByte(imageBase64);
            ByteArrayInputStream is = new ByteArrayInputStream(context);
            is = new ByteArrayInputStream(context);
            boolean saveFlag = ftpClient.storeFile(fileName, is);
            is.close();
            if (!saveFlag) {
                throw new IllegalArgumentException("存储文件失败");
            }
@@ -98,6 +98,9 @@
        } finally {
            try {
                ftpClient.disconnect();
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("关闭ftpClient 失败", e);
@@ -117,7 +120,7 @@
        try {
            // request.setCharacterEncoding("utf-8");
            ftpClient = new FTPClient();
            if(!ftpClient.isConnected()){
            if (!ftpClient.isConnected()) {
                ftpClient.connect(server, port);
            }
            ftpClient.login(userName, userPassword);
@@ -166,7 +169,7 @@
        FTPClient ftpClient = null;
        try {
            ftpClient = new FTPClient();
            if(!ftpClient.isConnected()){
            if (!ftpClient.isConnected()) {
                ftpClient.connect(server, port);
            }
            ftpClient.login(userName, userPassword);
@@ -212,18 +215,19 @@
    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");
        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) {
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream fis = null;
        FTPClient ftpClient = new FTPClient();
        try {
            if(!ftpClient.isConnected()){
            if (!ftpClient.isConnected()) {
                ftpClient.connect(server, port);
            }
            ftpClient.login(userName, userPassword);
@@ -246,11 +250,9 @@
            while (-1 != (length = is.read(buf, 0, buf.length))) {
                bos.write(buf, 0, length);
            }
            ByteArrayInputStream fis = new ByteArrayInputStream(
            fis = new ByteArrayInputStream(
                    bos.toByteArray());
            bos.flush();
            is.close();
            bos.close();
            byte[] buffer = new byte[fis.available()];
            int offset = 0;
            int numRead = 0;
@@ -260,18 +262,32 @@
            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 {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            try {
                if(bos != null){
                    bos.close();
                }
                if(is !=null) {
                    is.close();
                }
                closeConnect(ftpClient);
            } catch (Exception e) {
                e.printStackTrace();