wuxw
2024-01-09 05a7f827b7ff555f88982e2dcfb45c2ef5a34a54
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
@@ -79,9 +79,6 @@
            } 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";
            }
@@ -175,7 +172,7 @@
        String ftpPath = "/";
        if(fileName.contains("/")){
            ftpPath = fileName.substring(0,fileName.lastIndexOf("/"));
            ftpPath = fileName.substring(0,fileName.lastIndexOf("/")+1);
        }
        try {
@@ -191,6 +188,7 @@
            mkDir(ftpClient, ftpPath);// 创建目录
            // 设置上传目录 must
            ftpClient.changeWorkingDirectory(ftpPath);
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
                LOCAL_CHARSET = "UTF-8";
            }
@@ -203,7 +201,7 @@
                System.out.println("this file exist ftp");
                ftpClient.deleteFile(fs[0].getName());
            }
            boolean saveFlag = ftpClient.storeFile(fileName, inputStream);
            boolean saveFlag = ftpClient.storeFile("/"+fileName, inputStream);
            if (!saveFlag) {
                throw new IllegalArgumentException("存储文件失败");
            }
@@ -231,6 +229,8 @@
                ftpClient.connect(server, port);
            }
            ftpClient.login(userName, userPassword);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (ftpClient != null) {
                String f = new String(
@@ -402,4 +402,41 @@
            return false;
        }
    }
    public InputStream download(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()) {
                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(
                    ("/"+fileName).getBytes("GBK"),
                    FTP.DEFAULT_CONTROL_ENCODING);
            is = ftpClient.retrieveFileStream(f);// 获取远程ftp上指定文件的InputStream
            if (null == is) {
                throw new FileNotFoundException(fileName);
            }
        } catch (Exception e) {
            logger.error("ftp通过文件名称获取远程文件流", e);
        } finally {
            try {
                closeConnect(ftpClient);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return is;
    }
}