Add commons-net to your pom.xml
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency>
FtpService
package test.service; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; @Slf4j @Service public class FtpService { @Value("${ftp.host}") private String host; @Value("${ftp.port}") private Integer port; @Value("${ftp.user}") private String user; @Value("${ftp.password}") private String password; private FTPClient client; /** * Obtain a list of file information for specific working directory. * * @param directory * specific working directory * @return list of file * @throws IOException */ public List<String> listFiles(String directory) throws IOException { FTPFile[] files = null; try { connect(); files = client.listFiles(directory); if (files == null || files.length == 0) { throw new IllegalArgumentException("cannot find any files in " + directory); } } catch (IOException e) { throw new IOException("fail to list files", e); } finally { disconnect(); } return Arrays.stream(files).map(FTPFile::getName).collect(Collectors.toList()); } /** * Stores a file on the server using the given name. * * @param directory * specific working directory * @param file * local file * @throws IOException */ public void uploadFile(String directory, File file) throws IOException { try (InputStream is = new FileInputStream(file);) { connect(); client.changeWorkingDirectory(directory); client.storeFile(new String(file.getName().getBytes(), "iso-8859-1"), is); } catch (IOException e) { throw new IOException("fail to upload file", e); } finally { disconnect(); } } /** * Retrieves a named file from the server. * * @param directory * specific working directory * @param remoteFileName * remote file name * @param downloadFile * local file * @throws IOException */ public void downloadFile(String directory, String remoteFileName, File downloadFile) throws IOException { try (OutputStream outputStream1 = new BufferedOutputStream( new FileOutputStream(downloadFile));) { connect(); client.retrieveFile(directory + "/" + new String(remoteFileName.getBytes(), "iso-8859-1"), outputStream1); } catch (IOException e) { throw new IOException("fail to download file", e); } finally { disconnect(); } } /** * Delete file from FTP server. * * @param directory * specific working directory * @param remoteFileName * remote file name * @throws IOException */ public void deleteFile(String directory, String remoteFileName) throws IOException { try { connect(); client.changeWorkingDirectory(directory); client.deleteFile(new String(remoteFileName.getBytes(), "iso-8859-1")); } catch (IOException e) { throw new IOException("fail to delete file from FTP server", e); } finally { disconnect(); } } /** * Connect to FTP server. * * @throws IOException */ private void connect() throws IOException { try { client = new FTPClient(); client.addProtocolCommandListener( new PrintCommandListener(new PrintWriter(System.out), true)); client.connect(host, port); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); throw new IOException("Exception in connecting to FTP Server"); } client.login(user, password); client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); log.debug("connect to FTP server succesfully"); } catch (IOException e) { throw new IOException("fail to connect to FTP server", e); } } /** * Disconnect from FTP server. * * @throws IOException */ private void disconnect() throws IOException { try { if (client.isConnected()) { client.logout(); client.disconnect(); } log.debug("disconnect from FTP server succesfully"); } catch (IOException e) { throw new IOException("fail to disconnect from FTP server", e); } } }
Client code
package test; import java.io.File; import java.util.List; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import lombok.extern.slf4j.Slf4j; import test.service.FtpService; @SpringBootApplication @Slf4j public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean CommandLineRunner run(FtpService svc) { return args -> { // list files List<String> files = svc.listFiles("/tcpb"); files.forEach(f -> log.debug(f)); // upload file svc.uploadFile("/tcpb", new File("C:\\中文測試.zip")); // download file svc.downloadFile("/tcpb", "中文測試.zip", new File("C:\\中文測試1.zip")); // delete file svc.deleteFile("/tcpb", "中文測試.zip"); }; } }
No comments:
Post a Comment