Java USES FTP to upload files for example sharing
- 2020-04-01 02:57:40
- OfStack
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
public class FTPFileTransmit {
private String ftpPath;
private String ftpName;
private String ftpPassword;
private String ftpServerIP;
public FTPFileTransmit() {
this.ftpPath = "xxx/xxx/";
this.ftpName = "name";
this.ftpPassword = "pass";
this.ftpServerIP = "192.168.0.xx";
}
/**
* Method name: saveInFTP <BR>
* Description: Store the file in FTP on <BR>
* Remark: <BR>
* @param FolderName The sample "xxx/xxx/"
* @param FileName The sample "thefilename"
* @param data byte[] An array of
* @return boolean<BR>
*/
public boolean saveInFTP (String FolderName, String FileName, byte[] data) {
boolean flag = false;
//Create FTP client & NBSP;
FTPClient ftpClient = new FTPClient();
//The input stream is used to read the file & NBSP;
// FileInputStream fis = null;
ByteArrayInputStream bis = null;
try {
//If neither FolderName nor FileName meet the basic requirements, there is no need to do an FTP operation & NBSP;
if (FolderName != null
&& FolderName.compareTo("") != 0
&& FileName != null
&& FileName.compareTo("") != 0) {
//Establish FTP connection & NBSP;
ftpClient.connect(this.ftpServerIP);
//If the login is successful, the input stream is created & NBSP;
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
// File srcClientFile = new File("C:/ParseXML.xml");
//Instantiate the input stream & NBSP;
// fis = new FileInputStream(srcClientFile);
if (ftpClient.changeWorkingDirectory(FolderName)) {
//Write byte[] to the input stream, instantiate & NBSP;
bis = new ByteArrayInputStream(data);
//Setting buffers & NBSP;
ftpClient.setBufferSize(1024);
//Set the file type (binary type)& NBSP;
if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {
flag = ftpClient.storeFile(FileName, bis);
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
//Close the input stream & NBSP;
IOUtils.closeQuietly(bis);
//Close the connection & NBSP;
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public boolean getFromFTP () {
boolean flag = false;
//Create FTP client & NBSP;
FTPClient ftpClient = new FTPClient();
//The output stream is used for the output file & NBSP;
FileOutputStream fos = null;
try {
//Establish FTP connection & NBSP;
ftpClient.connect(this.ftpServerIP);
//If the login succeeds, the output stream is created & NBSP;
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
//FTP file
String distinationFile = "/name/xxx/xxx/xxx file ";
//Instantiate the output stream & NBSP;
fos = new FileOutputStream("C:/ParseXML_InFTP.xml");
//Setting buffers & NBSP;
ftpClient.setBufferSize(1024);
//Set the file type (binary type)& NBSP;
if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {
ftpClient.retrieveFile(distinationFile, fos);
flag = true;
}
}
} catch (SocketException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
//Close the output stream & NBSP;
IOUtils.closeQuietly(fos);
//Close the connection & NBSP;
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public boolean createDirectory() {
boolean flag = false;
//Create FTP client & NBSP;
FTPClient ftpClient = new FTPClient();
try {
//Establish FTP connection & NBSP;
ftpClient.connect(this.ftpServerIP);
//If the login is successful, the operation is performed & NBSP;
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
//Switch file path to "NNDD3" folder on FTP & PI;
if (this.ftpPath != null && this.ftpPath.compareTo("") != 0
&& ftpClient.changeWorkingDirectory(this.ftpPath)) {
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
String time = f.format(new Date());
String FolderName = time + "_ReTransmit";
ftpClient.makeDirectory(FolderName);
flag = true;
}
}
} catch (SocketException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
//Close the connection & NBSP;
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public String[] getAllFolderNames () {
//Create FTP client & NBSP;
FTPClient ftpClient = new FTPClient();
try {
//Establish FTP connection & NBSP;
ftpClient.connect(this.ftpServerIP);
//If the login is successful, the operation is performed & NBSP;
if (ftpClient.login(this.ftpName, this.ftpPassword)) {
//Switch file path to "NNDD3" folder on FTP & PI;
if (this.ftpPath != null && this.ftpPath.compareTo("") != 0
&& ftpClient.changeWorkingDirectory(this.ftpPath)) {
//Subtract 2 days from the current time to delete the previous two days' data package & NBSP;
String time = minusTime();
String[] allNames = ftpClient.listNames();
String[] temp = new String[allNames.length];
//Initializing array & NBSP;
for (int j = 0; j < allNames.length; j ++) {
temp[j] = "";
}
//Find the name of the folder you want to delete & NBSP;
for (int i = 0; i < allNames.length; i ++) {
if (allNames[i].substring(0, 8).compareTo(time) <= 0) {
temp[i] = allNames[i];
}
}
return temp;
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//Close the connection & NBSP;
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private String minusTime() {
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
Date d = new Date();
String timeMinus2 = df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000));
return timeMinus2;
}
public static void main(String[] args) {
FTPFileTransmit ftpFileTransmit = new FTPFileTransmit();
ftpFileTransmit.deleteFoldersInFTP();
// boolean flag = ftpFileTransmit.createDirectory();
// if (flag) {
// System.out.println("****** * FTP folder created successfully ****** **");
// }
// String FolderName = ftpFileTransmit.ftpPath + "20110809_ReTransmit/";
// byte[] data = new byte[1024];
// for (int i = 0; i < data.length; i ++) {
// data[i] = 'a';
// }
// boolean flag = ftpFileTransmit.saveInFTP(FolderName, "2011080912345678" ,data);
// if (flag) {
// System.out.println("****** * FTP folder created successfully ****** **");
// }
}
}