Some time ago, the company was writing an interface to invoke the FTP service provided by the three-party developer for downloading files. Some code backups have been made to this.
Here is the complete test code to be pasted up to share with you.
Introduction Package
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import java.util.List;
Import Org.jfree.util.Log;
Import Sun.net.TelnetInputStream;
Import Sun.net.TelnetOutputStream;
Import sun.net.ftp.FtpClient;
public class Ftptest
{
Defining private variables
Private FtpClient ftpclient;
Public FtpClient getftpclient () {
return ftpclient;
}
public void Setftpclient (FtpClient ftpclient) {
This.ftpclient = ftpclient;
}
Public String getaddress () {
return address;
}
public void setaddress (String address) {
this.address = address;
}
public int Getport () {
return port;
}
public void Setport (int port) {
This.port = port;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Private String address; FTP address
private int port; FTP port
Private String username; FTP User Name
private String password; FTP password
The reason for using this file connector is to block out the effects of different platforms and improve compatibility.
Private static final String code = file.separator;//file connector, which is in Windows \ \ is not the same inside the Linux system
constructor function
Public Ftptest (string address, int port, string Username, string password)
{
this.address = address;
This.port = port;
This.username = Username;
This.password = password;
}
public static void Main (string[] args)
{
Instantiation of
Ftptest client = new Ftptest ("***.***.***.***", * *, "* * *", "* * *");
Try
{
Call example to get a list of files
List List = client.getlist ("path");
Download file
Client.downloadfile ("path");
Uploading files
Client.uploadfile ("path");
}
catch (Exception ex)
{
Log.info (Ftptest.class.getName () + ex.getmessage ());
}
}
Close connection
public void CloseServer () throws Exception {
Ftpclient.closeserver ();
}
public Boolean ConnectServer (string address, int port, string user, string pwd)
Throws Exception {
Boolean issuccess = false;
try {
FtpClient = new FtpClient ();
Ftpclient.openserver (address, port);
Ftpclient.login (user, PWD);
Issuccess = true;
} catch (Exception ex) {
Ex.printstacktrace ();
Log.info (Ftptest.class.getName () + ex.getmessage ());
}
return issuccess;
}
Directory Access
Public List getList (String remotepath) {
List List = new ArrayList ();
try {
Connection Successful
if (ConnectServer (address, port, username, password))
{
BufferedReader br = new BufferedReader (New InputStreamReader (Ftpclient.namelist (RemotePath)));
The following two sentences are used to verify that the permission to open a data download removes the BEGIN
System.out.println (RemotePath);
String temp = ftpclient.getresponsestring ();
SYSTEM.OUT.PRINTLN (temp);
The following two sentences are used to verify that the permission to open the data download can be removed from the end
String readLine = null;
int lastIndex = 0;
String readLine1 = Br.readline ();
System.out.println (READLINE1);
Remove the RemotePath suffix, which may be '/', or it may be another symbol
if ((LastIndex = Remotepath.lastindexof ("/")) > 0
|| (LastIndex = Remotepath.lastindexof ("//")) > 0
|| (LastIndex = remotepath.lastindexof ("\ \")) > 0
|| (LastIndex = remotepath.lastindexof (code)) > 0)
{
System.out.println (RemotePath);
RemotePath = remotepath.substring (0, LastIndex);
}
while ((ReadLine = Br.readline ()) = null)
{
int index = READLINE.LASTINDEXOF (code);
SYSTEM.OUT.PRINTLN (index);
List.add (Readline.substring (index+1, Readline.length ()));
System.out.println (Readline.substring (index+1, Readline.length ()));
System.out.println (ReadLine);
}
Ftpclient.closeserver ();
}
}
catch (Exception ex)
{
Ex.printstacktrace ();
}
return list;
}
File download
public void DownloadFile (String remotepath) throws Exception {
try {
if (ConnectServer (address, port, username, password))
{
if (Remotepath.length ()! = 0)
{
FTPCLIENT.CD (RemotePath);
}
Ftpclient.binary ();
If you want to connect with the above file list, you need to loop the call, not find the saved code
Telnetinputstream is = ftpclient.get ("filename");
File FP = new files (filename path);
if (!fp.exists ())
{
Fp.mkdirs ();
}
File File_out = new file ("Download to that path/filename");
FileOutputStream OS = new FileOutputStream (file_out);
byte[] bytes = new byte[1024];
int Readbye;
while ((Readbye = is.read (bytes))! =-1)
{
Os.write (bytes, 0, Readbye);
}
Is.close ();
Os.close ();
Ftpclient.closeserver ();
}
}
catch (Exception ex)
{
Ex.printstacktrace ();
}
}
File Upload
public void UploadFile (String remotepath) throws Exception {
try {
if (ConnectServer (address, port, username, password))
{
if (Remotepath.length ()! = 0)
{
FTPCLIENT.CD (RemotePath);
}
Ftpclient.binary ();
Telnetoutputstream OS = ftpclient.put ("Upload to There");
File file_in = new file ("Upload from that path");
FileInputStream is = new FileInputStream (file_in);
byte[] bytes = new byte[1024];
int Readbye;
while ((Readbye = is.read (bytes))! =-1)
{
Os.write (bytes, 0, Readbye);
}
Is.close ();
Os.close ();
Ftpclient.closeserver ();
}
}
catch (Exception ex)
{
Ex.printstacktrace ();
}
}
}
The above is some of the sharing, we have questions can be put together to discuss.
Java calls the FTP service