最近學習java編程是一個實驗讓我困惑了很久就是:
編寫實現類似FTP功能的程序,客戶程序向服務器發送字符串”ls”,服務器返回幾個文件名,客戶程序從中選擇一個文件,服務器將該文件發送給客戶端,客戶程序接受該文件並保存在本地硬盤中。(說明:不要求採用FTP協議實現)
下面把代碼貼出來,可以實現文件傳送,
下面把代碼貼出來,可以實現文件傳送,
傳送文件中,服務器端吧文件對象內容寫到byte數組中然後由socket發送
接收端主要也是讀getinputstream然後保存到byte數組中,在寫入到文件中
客戶端程序:
package conan;
import java.io.*;
import java.net.*;
class FTPClient extends Thread{
Socket sl;
DataOutputStream dos;
DataInputStream dis;
public static void main (String[] args) {
FTPClient ftpc=new FTPClient();
ftpc.start();
}
public String GetCom()throws Exception{
System.out.println("請輸入命令: ");
InputStreamReader is=new InputStreamReader(System.in);
char[] buf=new char[100];
int len = is.read(buf);
String str=new String(buf,0,len).trim();
return str;
}
public String GetFileName()throws Exception{//獲得輸入的文件名
System.out.println("輸入文件名: ");
InputStreamReader is=new InputStreamReader(System.in);
char[] buf=new char[100];
int len2=is.read(buf);
String str=new String(buf,0,len2).trim();
return str;
}
public void receiveFile(Socket s) throws Exception //接收文件
{
sl= s;
String filename=GetFileName();
dos=new DataOutputStream(sl.getOutputStream());
dos.writeUTF(filename);
System.out.println("發送文件名成功");
File f=new File(File.separator);//當前根目錄
File file =new File(f,"Downloads"+File.separator+filename); //保存到當前目錄得Downloads文件裏面
sl=new Socket("127.0.0.1",5432); //創建一個socket對象用來傳送文件
BufferedOutputStream buff=new BufferedOutputStream(new FileOutputStream(file));
dis=new DataInputStream(sl.getInputStream());
byte[] line=new byte[102400];
int mount;
System.out.println("開始接收文件");
System.out.println("文件傳送中....");
while((mount=dis.read(line))!=-1){
buff.write(line,0,mount);
buff.flush();//刷新
Thread.sleep(50);//設置一個等待時間,有利於接收文件
}
sl.close();
buff.close();
System.out.println("finished");
}
public void run(){
try{
Socket sc;
sc=new Socket("localhost",4321);
dos=new DataOutputStream(sc.getOutputStream());
dos.writeUTF(GetCom());
dis=new DataInputStream(sc.getInputStream());
for(int i=0;i<30;i++){
System.out.println(dis.readUTF());
}
receiveFile(sc);
}
catch(Exception e){
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
class FTPClient extends Thread{
Socket sl;
DataOutputStream dos;
DataInputStream dis;
public static void main (String[] args) {
FTPClient ftpc=new FTPClient();
ftpc.start();
}
public String GetCom()throws Exception{
System.out.println("請輸入命令: ");
InputStreamReader is=new InputStreamReader(System.in);
char[] buf=new char[100];
int len = is.read(buf);
String str=new String(buf,0,len).trim();
return str;
}
public String GetFileName()throws Exception{//獲得輸入的文件名
System.out.println("輸入文件名: ");
InputStreamReader is=new InputStreamReader(System.in);
char[] buf=new char[100];
int len2=is.read(buf);
String str=new String(buf,0,len2).trim();
return str;
}
public void receiveFile(Socket s) throws Exception //接收文件
{
sl= s;
String filename=GetFileName();
dos=new DataOutputStream(sl.getOutputStream());
dos.writeUTF(filename);
System.out.println("發送文件名成功");
File f=new File(File.separator);//當前根目錄
File file =new File(f,"Downloads"+File.separator+filename); //保存到當前目錄得Downloads文件裏面
sl=new Socket("127.0.0.1",5432); //創建一個socket對象用來傳送文件
BufferedOutputStream buff=new BufferedOutputStream(new FileOutputStream(file));
dis=new DataInputStream(sl.getInputStream());
byte[] line=new byte[102400];
int mount;
System.out.println("開始接收文件");
System.out.println("文件傳送中....");
while((mount=dis.read(line))!=-1){
buff.write(line,0,mount);
buff.flush();//刷新
Thread.sleep(50);//設置一個等待時間,有利於接收文件
}
sl.close();
buff.close();
System.out.println("finished");
}
public void run(){
try{
Socket sc;
sc=new Socket("localhost",4321);
dos=new DataOutputStream(sc.getOutputStream());
dos.writeUTF(GetCom());
dis=new DataInputStream(sc.getInputStream());
for(int i=0;i<30;i++){
System.out.println(dis.readUTF());
}
receiveFile(sc);
}
catch(Exception e){
e.printStackTrace();
}
}
}
服務器端程序:
package conan;
import java.net.*;
import java.io.*;
class FTPServer extends Thread
{
ServerSocket server,server1;
Socket s;
DataInputStream dis;
DataOutputStream dos;
public static void main (String[] args) {
FTPServer ftps=new FTPServer();
ftps.start();
}
public void sendFile(String filename)throws Exception
{
try{
server1=new ServerSocket(5432);
s=server1.accept();
File path=new File(File.separator);//當前根目錄
File file=new File(path,filename);//創建該文件對象
System.out.println("將文件獨到緩衝流中");
BufferedInputStream buffile=new BufferedInputStream(new FileInputStream(file));
byte[] buf=new byte[102400];//每次發送的塊打大小
int mount;
dos=new DataOutputStream(s.getOutputStream());
System.out.println("開始傳送文件");
while((mount=buffile.read(buf))!=-1){//傳送文件,將文件內容以字節形式保存到buf中再發送
dos.write(buf,0,mount);
Thread.sleep(50);
}
System.out.println("文件傳送完畢");
server1.close();
s.close();//關閉清理工作
buffile.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
server=new ServerSocket(4321);//4321端口用來接接收命令
System.out.println("Listen at 4321 port");
while(true)
{
s=server.accept();//監聽
dis=new DataInputStream(s.getInputStream());
byte [] buf=new byte[100];
int len=dis.read(buf);
String str=new String(buf,0,len).trim();
System.out.println("接收命令成功");
if(str.startsWith("ls")){
File localfile=new File(File.separator);
String[] filelist=localfile.list();
dos=new DataOutputStream(s.getOutputStream());//輸出文件列表
dos.writeUTF("-------------------File List--------------------");
for(int i=0;i<30;i++){
dos.writeUTF(filelist[i]);
}
}
dis=new DataInputStream(s.getInputStream());
String filename_from_client=dis.readUTF().trim();
System.out.println("接收文件名成功");
System.out.println("傳送得文件名 :"+filename_from_client);
sendFile(filename_from_client);
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
class FTPServer extends Thread
{
ServerSocket server,server1;
Socket s;
DataInputStream dis;
DataOutputStream dos;
public static void main (String[] args) {
FTPServer ftps=new FTPServer();
ftps.start();
}
public void sendFile(String filename)throws Exception
{
try{
server1=new ServerSocket(5432);
s=server1.accept();
File path=new File(File.separator);//當前根目錄
File file=new File(path,filename);//創建該文件對象
System.out.println("將文件獨到緩衝流中");
BufferedInputStream buffile=new BufferedInputStream(new FileInputStream(file));
byte[] buf=new byte[102400];//每次發送的塊打大小
int mount;
dos=new DataOutputStream(s.getOutputStream());
System.out.println("開始傳送文件");
while((mount=buffile.read(buf))!=-1){//傳送文件,將文件內容以字節形式保存到buf中再發送
dos.write(buf,0,mount);
Thread.sleep(50);
}
System.out.println("文件傳送完畢");
server1.close();
s.close();//關閉清理工作
buffile.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
server=new ServerSocket(4321);//4321端口用來接接收命令
System.out.println("Listen at 4321 port");
while(true)
{
s=server.accept();//監聽
dis=new DataInputStream(s.getInputStream());
byte [] buf=new byte[100];
int len=dis.read(buf);
String str=new String(buf,0,len).trim();
System.out.println("接收命令成功");
if(str.startsWith("ls")){
File localfile=new File(File.separator);
String[] filelist=localfile.list();
dos=new DataOutputStream(s.getOutputStream());//輸出文件列表
dos.writeUTF("-------------------File List--------------------");
for(int i=0;i<30;i++){
dos.writeUTF(filelist[i]);
}
}
dis=new DataInputStream(s.getInputStream());
String filename_from_client=dis.readUTF().trim();
System.out.println("接收文件名成功");
System.out.println("傳送得文件名 :"+filename_from_client);
sendFile(filename_from_client);
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
終於把實驗做完了,從中也學到不少socket和io得知識,還真不錯。