Your Name
2023-06-06 fe3b0f4a3c46892f08421dd4c4d0937fb8a87f93
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";
            }
@@ -166,6 +163,60 @@
        return fileName;
    }
    /*
     *文件上传工具方法
     */
    public String upload(InputStream inputStream, String server, int port,
                         String userName, String userPassword, String fileName) {
        FTPClient ftpClient = null;
        String ftpPath = "/";
        if(fileName.contains("/")){
            ftpPath = fileName.substring(0,fileName.lastIndexOf("/")+1);
        }
        try {
            // request.setCharacterEncoding("utf-8");
            ftpClient = new FTPClient();
            if (!ftpClient.isConnected()) {
                ftpClient.connect(server, port);
            }
            ftpClient.login(userName, userPassword);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            mkDir(ftpClient, ftpPath);// 创建目录
            // 设置上传目录 must
            ftpClient.changeWorkingDirectory(ftpPath);
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
                LOCAL_CHARSET = "UTF-8";
            }
            //fileName = new String(uploadFile.getOriginalFilename().getBytes(LOCAL_CHARSET), SERVER_CHARSET);
            /*fileName = id + "-" + fileName;// 构建上传到服务器上的文件名 20-文件名.后缀*/
            FTPFile[] fs = ftpClient.listFiles(fileName);
            if (fs.length == 0) {
                System.out.println("this file not exist ftp");
            } else if (fs.length == 1) {
                System.out.println("this file exist ftp");
                ftpClient.deleteFile(fs[0].getName());
            }
            boolean saveFlag = ftpClient.storeFile("/"+fileName, inputStream);
            if (!saveFlag) {
                throw new IllegalArgumentException("存储文件失败");
            }
        } catch (Exception e) {
            logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }
    /*
     *文件下载工具方法
     */
@@ -349,4 +400,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;
    }
}