A project I used to do in the past, uploading files to the ftp server, and now I review it. It is relatively simple and you can understand it in one go.
Environment: First, install the ftp server first. I configured it locally in win8 with IIS. You can find the installation document in Baidu.
1. Create an ftp configuration file in your project directory, the directory is as shown below
01 ftpconfig.properties:
ftpIp=10.73.222.29
ftpPort=21
ftpUser=WP
ftpPwd=04143114wp
ftpRemotePath=d://share
02 Class that reads the specific content in ftpconfig.properties:
package com.java.core.util;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * @author wangpei * @version Created: May 6, 2017 at 9:42:40 pm Read the configuration file of the ftp file*/public class ReadFtpProperties { private InputStream is; private Properties properties; public ReadFtpProperties() { is = this.getClass().getResourceAsStream("/ftpconfig.properties");// Read the configuration file into the input stream properties = new Properties(); try { properties.load(is); } catch (IOException e) { System.out.println("Configuration file does not exist.."); e.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (IOException e) { System.out.println("Closed stream failed.."); e.printStackTrace(); } } } } public String getIp() {// Get the ftp server's ip address return properties.getProperty("ftpIp"); } public String getPort() {// Get the port of the ftp server's return properties.getProperty("ftpPort"); } public String getUser() {// Get the ftp login username return properties.getProperty("ftpUser"); } public String getPwd() {// Get the login password of the ftp server's return properties.getProperty("ftpPwd"); } public String getRemotePath() {// Get the directory where the files are stored in the ftp server are returned properties.getProperty("ftpRemotePath"); }}03 The interface class for file upload and download
package com.java.web.service;import java.io.InputStream;import org.apache.commons.net.ftp.FTPClient;import com.java.core.util.ReadFtpProperties;/** * @author wangpei * @version Created: May 6, 2017 at 6:39:03 pm * File upload and download Business logic interface layer*/public interface FtpService { /* * Log in to FTP */ public boolean loginFTP(FTPClient client, ReadFtpProperties rfp); /* * Log out of ftp */ public boolean logout(FTPClient client);// /* * Upload the file to remotePath, whose name on ftp is inputStream */ public boolean uploadFile(FTPClient client, String remotePath, String fileNewName, InputStream inputStream, ReadFtpProperties rfp); /* * Download the file fileName from the directory remotePath */ public InputStream downFileByFtp(FTPClient client, String remotePath, String fileName); /* * Delete the file with the directory pathName on ftp*/ public boolean delFile(FTPClient client, String pathName);}04 The interface implementation class for file upload and download
package com.java.web.service.serviceImpl;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.SocketException;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import com.java.core.util.ReadFtpProperties;import com.java.web.service.FtpService;/** * @author wangpei * @version Created time: May 6, 2017 at 10:02:28 pm Class Description*/public class FtpServiceImpl implements FtpService { public boolean loginFTP(FTPClient client, ReadFtpProperties rfp) { String ftpIp = rfp.getIp(); String ftpPort = rfp.getPort(); String ftpUser = rfp.getUser(); String ftpPwd = rfp.getPwd(); // String fgtpRemotePath = rfp.getRemotePath(); boolean b = false; try { client.connect(ftpIp, Integer.parseInt(ftpPort)); } catch (NumberFormatException e) { System.out.println("Cannot connect to ftp"); return false; } catch (SocketException e) { System.out.println("Cannot connect to ftp"); return false; } catch (IOException e) { System.out.println("Cannot connect to ftp"); return false; } catch (IOException e) { System.out.println("Cannot connect to ftp"); return false; } client.setControlEncoding("uft-8"); try { b = client.login(ftpUser, ftpPwd); } catch (IOException e) { System.out.println("Login ftp error"); logout(client);// Logout/disconnect FTP server link return false; } return b; } public boolean logout(FTPClient client) { boolean b = false; try { b = client.logout();// Logout client.disconnect();// Disconnect} catch (IOException e) { return false; } return b; } public boolean uploadFile(FTPClient client, String remotePath, String fileNewName, InputStream inputStream, ReadFtpProperties rfp) { boolean b = false; try { client.setFileType(FTPClient.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); if (remotePath != null && !"".equals(remotePath.trim())) { String[] paths = remotePath.split("/"); for (String onepath : paths) { if (onepath == null || "".equals(onepath.trim())) { continue; } onepath = new String(onepath.getBytes("utf-8"), "iso-8859-1"); System.out.println("onepath=" + onepath); if (!client.changeWorkingDirectory(onepath)) { client.makeDirectory(onepath);// Create an FTP server directory client.changeWorkingDirectory(onepath);// Change the FTP server directory} else { System.out.println("File single path"); } } } b = client.storeFile(new String(fileNewName.getBytes("utf-8"), "iso-8859-1"), inputStream); } catch (UnsupportedEncodingException e) { return false; } catch (IOException e) { return false; } return b; } public InputStream downFileByFtp(FTPClient ftpClient, String remotePath, String fileName) { FTPFile[] fs; InputStream is = null; try { // Set passive mode ftpClient.enterLocalPassiveMode(); // Set ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // Set edit format ftpClient.setControlEncoding("utf-8"); remotePath = remotePath.substring(0, remotePath.lastIndexOf(fileName)); fs = ftpClient.listFiles(remotePath);// Recursive target directory for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) {// Find the target file is = ftpClient.retrieveFileStream(new String( (remotePath + fileName).getBytes("utf-8"), "iso-8859-1")); break; } } } catch (IOException e) { e.printStackTrace(); } return is; } public boolean delFile(FTPClient ftpClient, String pathName) { boolean b = false; try { b = ftpClient.deleteFile(pathName); return b; } catch (Exception e) { return false; } finally { logout(ftpClient);// Exit/disconnect FTP server link} }}The code is easy to understand, it should be understood by reading it once. I won’t analyze it specifically here, mainly look at the comments in the code.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.