*创建日期:2008年12月23日
*类名:Ftp.java
*类路径:组织结构
*更改日志:
* / 包组织结构
进口的java.io.File
进口java.io.FileInputStream中
进口java.io.FileOutputStream中
进口的java。 io.IOException
进口sun.net.TelnetInputStream
进口sun.net.TelnetOutputStream
进口sun.net.ftp.FtpClient
>/ **
* @作者南山地狱
* @说明FTP操作
* /
公共类的Ftp {
/ **
* BR />获取FTP目录* / 公共无效getftpList(){
字符串服务器=“IP地址 /输入FTP服务器/>弦乐用户=”“/ / FTP服务器的登录用户名
字符串密码=“”/ /登录FTP服务器的用户名
字符串路径密码=“”/ / FTP路径上的服务器
尝试{
>FtpClient的FTP客户端=新FtpClient的()/ /创建FtpClient的对象
ftpClient.openServer(服务器)/ /连接到FTP服务器
ftpClient.login(用户名,密码)/ / FTP服务器 BR />如果(path.length()= 0){
ftpClient.cd(路径)
}
TelnetInputStream是= ftpClient.list()
诠释三
而{
System.out.print((char)的C)((C = is.read())= -1!)
}
掉} is.close ()
ftpClient.closeServer()/ /退出FTP服务器
}赶上(IOException异常前){
System.out.println(ex.getMessage())
}
}
/ **
*
* /
公共无效getFtpFile(){
字符串服务器=“”/ / IP地址中输入FTP服务器
弦乐用户=“”/ / FTP服务器的登录用户名
字符串密码=“”/ /登录密码为FTP服务器的用户名
字符串路径=“路径
字符串文件名“/ /上=的FTP服务器”“/ /下载文件名称
尝试{
FtpClient的FTP客户端=新FtpClient的()
ftpClient.openServer(服务器)
ftpClient.login(用户名,密码)
如果(路径。长度()= 0)
ftpClient.cd(路径)!
ftpClient.binary()
TelnetInputStream是= ftpClient.get(文件名)
文件file_out =新的文件(文件名)
文件输出流OS =新的文件输出流(file_out)
字节[]字节=新字节[1024]
诠释三
而((C = is.read(字节))= -1){
os.write (字节,0,C)
}!
掉} is.close()
os.close()
ftpClient.closeServer()
}赶上(IOException异常前){
System.out.println (ex.getMessage())
}
FTP}
/ **
*文件上传到FTP
* /
公共无效putFtpFile() {
字符串服务器=“”/ /输入IP地址对服务器
字符串用户的地址=“”/ / FTP服务器的登录用户名
字符串密码=“”/ / FTP服务器登录用户名密码
字符串路径=“”就 / FTP服务器/>字符串文件名=“”/ /上传的文件名
FtpClient的FTP客户端=新的try { FtpClient的()
ftpClient.openServer(服务器)
ftpClient.login(用户名,密码)
如果(!path.length()= 0)
ftpClient.cd (路径)
ftpClient.binary()
TelnetOutputStream OS = ftpClient.put(文件名)
文件file_in =新的文件(文件名)
文件输入流是=新的文件输入流(file_in)
字节[]字节=新字节[1024]
诠释三
同时(! (C = is.read(字节))= -1){
操作系统。写(字节,0,C)
}
掉} is.close()
os.close()
ftpClient.closeServer()
}赶上(IOException异常前){
System.out.println(ex.getMessage())
}
}
}
这个是可以向服务器端发送文字的程序,就是在客户端发送一句hello在服务器也可以接受到hello,这个程序可以修改一下就可以了。具体修改方法是增加一个定时器,然后把字符流改成字节流,现在有点忙,你先研究啊,近两天帮你写写看。服务器端:
import java.net.*
import java.io.*
public class DateServer {
public static void main(String[] args) {
ServerSocket server=null
try{
server=new ServerSocket(6666)
System.out.println(
"Server start on port 6666...")
while(true){
Socket socket=server.accept()
new SocketHandler(socket).start()
/*
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
)
out.println(new java.util.Date().toLocaleString())
out.close()
*/
}
}catch(Exception e){
e.printStackTrace()
}finally{
if(server!=null) {
try{
server.close()
}catch(Exception ex){}
}
}
}
}
class SocketHandler extends Thread {
private Socket socket
public SocketHandler(Socket socket) {
this.socket=socket
}
public void run() {
try{
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
)
out.println(
new java.util.Date().
toLocaleString())
out.close()
}catch(Exception e){
e.printStackTrace()
}
}
}
客户端:
package com.briup
import java.io.*
import java.net.*
public class FtpClient {
public static void main(String[] args) {
if(args.length==0) {
System.out.println("Usage:java FtpClient file_path")
System.exit(0)
}
File file=new File(args[0])
if(!file.exists()||!file.canRead()) {
System.out.println(args[0]+" doesn't exist or can not read.")
System.exit(0)
}
Socket socket=null
try{
socket=new Socket(args[1],Integer.parseInt(args[2]))
BufferedInputStream in=new BufferedInputStream(
new FileInputStream(file)
)
BufferedOutputStream out=new BufferedOutputStream(
socket.getOutputStream()
)
byte[] buffer=new byte[1024*8]
int i=-1
while((i=in.read(buffer))!=-1) {
out.write(buffer,0,i)
}
System.out.println(socket.getInetAddress().getHostAddress()+" send file over.")
in.close()
out.close()
}catch(Exception e){
e.printStackTrace()
}finally{
if(socket!=null) {
try{
socket.close()
}catch(Exception ex){}
}
}
}
}