(); BtnConn.enable(true); BtnClose.enable(false); LblPrompt.setText("与主机"+hostname+"连接已断开!"); return true; } return super.action(evt,obj); } public boolean sendFile(String filepathname){ boolean result=true; if (aftp != null) { LblPrompt.setText("正在粘贴文件,请耐心等待...."); String contentperline; try{ a="粘贴成功!"; 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 = "粘贴失败!"; 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); } }
Java URL 编程 import java.io.*; import java.net.*; // // // GetHost.java // // public class GetHost { public static void main (String arg[]){ if (arg.length>=1){ InetAddress[] Inet; int i=1; try{ for (i=1;i<=arg.length;i++){ Inet = InetAddress.getAllByName(arg[i-1]); for (int j=1;j<=Inet.length;j++){ System.out.print(Inet[j-1].toString()); System.out.print("\n"); } } } catch(UnknownHostException e){ System.out.print("Unknown HostName!"+arg[i-1]); } } else{ System.out.print("Usage java/jview GetIp
"); } } }
Example 2 download now
//GetHTML.java /** * This is a program which can read information from a web server. * @version 1.0 2000/01/01 * @author jdeveloper **/ import java.net.*; import java.io.*; public class GetHTML { public static void main(String args[]){ if (args.length < 1){ System.out.println("USAGE: java GetHTML httpaddress"); System.exit(1); } String sURLAddress = new String(args[0]); URL url = null; try{ url = new URL(sURLAddress); }catch(MalformedURLException e){ System.err.println(e.toString()); System.exit(1); } try{ InputStream ins = url.openStream(); BufferedReader breader = new BufferedReader(new InputStreamReader(ins)); String info = breader.readLine(); while(info != null){ System.
out.println(info); info = breader.readLine(); } } catch(IOException e){ System.err.println(e.toString()); System.exit(1); } } } Java RMI 编程 Step 1: Implements the interface of Remote Server as SimpleCounterServer.java public interface SimpleCounterServer extends java.rmi.Remote
{ public int getCount() throws java.rmi.RemoteException; } Compile it with javac SimpleCounterServer.java Step 2: Produce the implement file SimpleCounterServerImpl.java as import java.rmi.*; import java.rmi.server.UnicastRemoteObject; // // // Simp