his-uncles-father
2021-04-21 fbc8d20f0ae2f663e1a13560f314f05b870c998c
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
@@ -16,6 +16,7 @@
@Component
public class FtpUploadTemplate {
    private static Logger logger = LoggerFactory.getLogger(FtpUploadTemplate.class);
    /*
@@ -24,14 +25,11 @@
     * private static String userPassword ="MXUsssMjhssE+*=a3C4\\0";//密码
     */
    private static String ftpPath = "uploadFiles"; // 文件上传目录
    private static String LOCAL_CHARSET = "GBK";
    private static String SERVER_CHARSET = "ISO-8859-1";
    private final static String localpath = "F:/";//下载到F盘下
    private final static String fileSeparator = System.getProperty("file.separator");
    private final static String DEFAULT_IMG_SUFFIX = ".jpg";
    private final static String IMAGE_DEFAULT_PATH = "img/";
    /*
@@ -60,7 +58,6 @@
                LOCAL_CHARSET = "UTF-8";
            }
            fileName = UUID.randomUUID().toString();
            if (imageBase64.contains("data:image/png;base64,")) {
                imageBase64 = imageBase64.replace("data:image/png;base64,", "");
                fileName += ".png";
@@ -73,6 +70,9 @@
            } else if (imageBase64.contains("data:application/octet-stream;base64,")) {
                imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", "");
                fileName += ".jpg";
            } else if (imageBase64.contains("mp4") || imageBase64.contains("MP4") || imageBase64.contains("AVI") || imageBase64.contains("avi")
                    || imageBase64.contains("WMV") || imageBase64.contains("wmv")) {
                fileName += ".mp4";
            } else {
                fileName += ".jpg";
            }
@@ -83,12 +83,9 @@
                System.out.println("this file exist ftp");
                ftpClient.deleteFile(fs[0].getName());
            }
            byte[] context = Base64Convert.base64ToByte(imageBase64);
            is = new ByteArrayInputStream(context);
            boolean saveFlag = ftpClient.storeFile(fileName, is);
            if (!saveFlag) {
                throw new IllegalArgumentException("存储文件失败");
            }
@@ -108,7 +105,6 @@
        }
        return IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName;
    }
    /*
     *文件上传工具方法
@@ -149,7 +145,7 @@
                throw new IllegalArgumentException("存储文件失败");
            }
        } catch (Exception e) {
            // logger.error("上传文件失败", e);
            logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
        } finally {
            try {
@@ -217,7 +213,6 @@
        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);
    }
@@ -266,27 +261,27 @@
        } catch (Exception e) {
            logger.error("ftp通过文件名称获取远程文件流", e);
        } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (is != null) {
                    try {
                        is.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();
                    }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                closeConnect(ftpClient);
            } catch (Exception e) {
@@ -345,5 +340,4 @@
            return false;
        }
    }
}