Java Implementation FTP function (source code)
Last Update:2017-02-28
Source: Internet
Author: User
source code import sun.net.ftp.*;
Import sun.net.*;
Import java.awt.*;
Import java.awt.event.*;
Import java.applet.*;
Import java.io.*;
public class Ftpapplet extends Applet
{
FtpClient AFTP;
DataOutputStream outputs;
Telnetinputstream ins;
Telnetoutputstream outs;
TextArea Lsarea;
Label lblprompt;
Button Btnconn;
Button btnclose;
TextField Txtuid;
TextField txtpwd;
TextField Txthost;
int ch;
Public String a= "no host Connected";
String hostname= "";
public void init () {
SetBackground (Color.White);
SetLayout (New GridBagLayout ());
Gridbagconstraints GBC = new Gridbagconstraints ();
Lblprompt = new Label ("No host Connected");
Lblprompt.setalignment (Label.left);
Btnconn = New button ("Connection");
btnclose = New button ("Disconnected");
Btnclose.enable (FALSE);
Txtuid = new TextField ("", 15);
Txtpwd = new TextField ("", 15);
Txtpwd.setechocharacter (' * ');
Txthost = new TextField ("", 20);
Label Lbluid = new Label ("User ID:");
Label lblpwd = new Label ("PWD:");
Label Lblhost = new Label ("Host:");
Lsarea = new TextArea (30,80);
Lsarea.seteditable (FALSE);
Gbc.gridwidth= Gridbagconstraints.remainder;
Gbc.fill = Gridbagconstraints.horizontal;
((GridBagLayout) getlayout ()). Setconstraints (LBLPROMPT,GBC);
Add (lblprompt);
gbc.gridwidth=1;
((GridBagLayout) getlayout ()). Setconstraints (LBLHOST,GBC);
Add (Lblhost);
Gbc.gridwidth=gridbagconstraints.remainder;
((GridBagLayout) getlayout ()). Setconstraints (TXTHOST,GBC);
Add (Txthost);
gbc.gridwidth=1;
((GridBagLayout) getlayout ()). Setconstraints (LBLUID,GBC);
Add (Lbluid);
gbc.gridwidth=1;
((GridBagLayout) getlayout ()). Setconstraints (TXTUID,GBC);
Add (Txtuid);
gbc.gridwidth=1;
((GridBagLayout) getlayout ()). Setconstraints (LBLPWD,GBC);
Add (LBLPWD);
gbc.gridwidth=1;
((GridBagLayout) getlayout ()). Setconstraints (TXTPWD,GBC);
Add (TXTPWD);
gbc.gridwidth=1;
gbc.weightx=2;
((GridBagLayout) getlayout ()). Setconstraints (BTNCONN,GBC);
Add (Btnconn);
Gbc.gridwidth=gridbagconstraints.remainder;
((GridBagLayout) getlayout ()). Setconstraints (BTNCLOSE,GBC);
Add (btnclose);
Gbc.gridwidth=gridbagconstraints.remainder;
Gbc.fill = Gridbagconstraints.horizontal;
((GridBagLayout) getlayout ()). Setconstraints (LSAREA,GBC);
Add (Lsarea);
}
public boolean connect (string hostname, string uid,string pwd)
{
This.hostname = hostname;
Lblprompt.settext ("Connecting, please wait ...");
try{
AFTP =new ftpclient (hostname);
Aftp.login (UID,PWD);
Aftp.binary ();
Showfilecontents ();
}
catch (Ftploginexception e) {
A= "No Permissions and Host:" +hostname+ "Connection!"
Lblprompt.settext (a);
return false;
}
catch (IOException e) {
A= "Connection host:" +hostname+ "Failure!";
Lblprompt.settext (a);
return false;
}
catch (SecurityException e)
{
A= "No Permissions and Host:" +hostname+ "Connection!"
Lblprompt.settext (a);
return false;
}
Lblprompt.settext ("Connect host:" +hostname+ "Success!");
return true;
}
public void Stop ()
{
Try
{
Aftp.closeserver ();
}
catch (IOException E)
{
}
}
public void Paint (Graphics g) {
}
Public boolean action (Event evt,object obj)
{
if (Evt.target = = Btnconn)
{
Lblprompt.settext ("Connecting, please wait ...");
if (Connect (Txthost.gettext (), Txtuid.gettext (), Txtpwd.gettext ()))
{
Btnconn.setenabled (FALSE);
Btnclose.setenabled (TRUE);
}
return true;
}
if (Evt.target = = btnclose)
{
Stop ();
Btnconn.enable (TRUE);
Btnclose.enable (FALSE);
Lblprompt.settext ("Connected with host" +hostname+ "Disconnected!");
return true;
}
Return super.action (evt,obj);
}
public boolean sendFile (String filepathname) {
Boolean result=true;
if (AFTP!= null)
{
Lblprompt.settext ("You are pasting the file, please be patient ...");
String Contentperline;
try{
A= "Paste success!";
String FG =new string ("\ \");
int index = FILEPATHNAME.LASTINDEXOF (FG);
String filename = filepathname.substring (index+1);
File LocalFile;
LocalFile = new File (filepathname);
Randomaccessfile sendFile = new Randomaccessfile (filepathname, "R");
//
Sendfile.seek (0);
Outs = Aftp.put (filename);
outputs = new DataOutputStream (outs);
while (Sendfile.getfilepointer () < Sendfile.length ())
{
ch = sendfile.read ();
Outputs.write (CH);
}
Outs.close ();
Sendfile.close ();
}
catch (IOException e) {
A = "Paste failed!";
result = false;
}
Lblprompt.settext (a);
Showfilecontents ();
}
else{
result = false;
}
return result;
}
public void Showfilecontents ()
{
StringBuffer buf = new StringBuffer ();
Lsarea.settext ("");
Try
{
Ins= aftp.list ();
while ((Ch=ins.read ()) >=0) {
Buf.append ((char) ch);
}
Lsarea.appendtext (Buf.tostring ());
Ins.close ();
}
catch (IOException E)
{
}
}
public static void Main (String args[]) {
Frame f = new Frame ("FTP Client");
F.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
Ftpapplet ftp = new Ftpapplet ();
Ftp.init ();
Ftp.start ();
F.add (FTP);
F.pack ();
F.setvisible (TRUE);
}
}