You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					65 lines
				
				1.9 KiB
			
		
		
			
		
	
	
					65 lines
				
				1.9 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								package com.ynxbd.common.helper.common;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import lombok.AllArgsConstructor;
							 | 
						||
| 
								 | 
							
								import lombok.extern.slf4j.Slf4j;
							 | 
						||
| 
								 | 
							
								import org.apache.commons.io.IOUtils;
							 | 
						||
| 
								 | 
							
								import org.apache.commons.net.ftp.FTP;
							 | 
						||
| 
								 | 
							
								import org.apache.commons.net.ftp.FTPClient;
							 | 
						||
| 
								 | 
							
								import org.apache.commons.net.ftp.FTPFile;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import java.io.*;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @author 李进才
							 | 
						||
| 
								 | 
							
								 * @ClassName FtpHelper
							 | 
						||
| 
								 | 
							
								 * @Description description
							 | 
						||
| 
								 | 
							
								 * @date 2023/3/21 15:34
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								@Slf4j
							 | 
						||
| 
								 | 
							
								@AllArgsConstructor
							 | 
						||
| 
								 | 
							
								public class FtpHelper {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    private final String server;
							 | 
						||
| 
								 | 
							
								    private final Integer port;
							 | 
						||
| 
								 | 
							
								    private final String user;
							 | 
						||
| 
								 | 
							
								    private final String pass;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public byte[] getFile(String path){
							 | 
						||
| 
								 | 
							
								        FTPClient ftpClient = new FTPClient();
							 | 
						||
| 
								 | 
							
								        ftpClient.setConnectTimeout(60*000); //连接超时为60秒
							 | 
						||
| 
								 | 
							
								        ftpClient.setControlEncoding("utf-8");
							 | 
						||
| 
								 | 
							
								        ftpClient.enterLocalPassiveMode();
							 | 
						||
| 
								 | 
							
								        try {
							 | 
						||
| 
								 | 
							
								            ftpClient.connect(server);
							 | 
						||
| 
								 | 
							
								            ftpClient.login(user, pass);
							 | 
						||
| 
								 | 
							
								            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
							 | 
						||
| 
								 | 
							
								            log.info("[FTP]登陆成功");
							 | 
						||
| 
								 | 
							
								            FTPFile ftpFile = ftpClient.mlistFile(path);
							 | 
						||
| 
								 | 
							
								            if (ftpFile != null) {
							 | 
						||
| 
								 | 
							
								                log.info("[FTP]读取到文件:{}",ftpFile.getName());
							 | 
						||
| 
								 | 
							
								                InputStream inputStream = ftpClient.retrieveFileStream(path);
							 | 
						||
| 
								 | 
							
								                byte[] fileData = IOUtils.toByteArray(inputStream);
							 | 
						||
| 
								 | 
							
								                return fileData;
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								                log.error("[FTP]读取共享文件失败 路径---{}",path);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            ftpClient.logout();
							 | 
						||
| 
								 | 
							
								            ftpClient.disconnect();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        } catch (IOException ex) {
							 | 
						||
| 
								 | 
							
								            log.error("[FTP]ftp连接失败");
							 | 
						||
| 
								 | 
							
								            ex.printStackTrace();
							 | 
						||
| 
								 | 
							
								        } finally {
							 | 
						||
| 
								 | 
							
								            if (ftpClient.isConnected()) {
							 | 
						||
| 
								 | 
							
								                try {
							 | 
						||
| 
								 | 
							
								                    ftpClient.disconnect();
							 | 
						||
| 
								 | 
							
								                } catch (IOException ex) {
							 | 
						||
| 
								 | 
							
								                    ex.printStackTrace();
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return null;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |