Java Implementation FTP function (source code)

Source: Internet
Author: User
Tags add ftp gettext connect net string ftp client stringbuffer
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);
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.