服務端*************
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.io.ObjectInputStream;
import?java.net.ServerSocket;
import?java.net.Socket;
public?class?Server?{
/**
?*?服務端:實現(xiàn)接收客戶端發(fā)送的文件
?*?
?*?@throws?IOException
?*/
public?static?void?main(String[]?args)?throws?IOException?{
String?filename?=?null;
String?filesize?=?null;
File?file=null;
//?1.創(chuàng)建ServerSocket實例,指定綁定的端口號,并監(jiān)聽次端口
ServerSocket?serverSocket?=?new?ServerSocket(8235);
//?2.調(diào)用accept()方法開始監(jiān)聽,等待客戶端的連接
Socket?socket?=?serverSocket.accept();
System.out.println("***啟動服務器,等待連接…………………………");
//?3.讀取客戶端發(fā)送的請求信息
InputStream?is?=?socket.getInputStream();
InputStreamReader?isr?=?new?InputStreamReader(is);
BufferedReader?br?=?new?BufferedReader(isr);
String?info=br.readLine()?;
//?4.解析客戶端發(fā)送的文件信息,正確的,保存文件名以及文件的大小,為下面接收文件內(nèi)容做準備
int?index?=?info.indexOf("/#");//?查找/#在字符串中的索引位置
String?daima?=?info.substring(0,?index);//?獲取字符串從0位置-index(不包括)之間的字符串內(nèi)容
if?(!daima.equals("111"))?{
System.out.println("服務器收到的文件的協(xié)議代碼和客戶端的文件協(xié)議代碼不匹配!!");
}?else?{
System.out.println("文件的協(xié)議代碼一致?。?!");
String?infoChild?=?info.substring(index?+?2);
//?獲得info字符串的子字符串,該子字符串從指定索引處的字符開始,直到此字符串末尾部分的內(nèi)容
int?indexC?=?infoChild.indexOf("/#");
filename?=?infoChild.substring(0,?index).trim();//?trim忽略前導空白和尾部空白
filesize?=?infoChild.substring(index?+?2).trim();
}
//?將接收到的文件保存在本地
????file?=?new?File(filename);
if?(!file.exists())?{
file.createNewFile();
}?else?{
System.out.println("本路徑已存在相同文件,進行覆蓋");
}
//?5.服務器接收文件內(nèi)容,并將內(nèi)容保存到本地文件中
InputStream?is1?=?socket.getInputStream();//?獲取傳送文件的字節(jié)流
ObjectInputStream?ois?=?new?ObjectInputStream(is1);
//?long?fileSize?=?Long.parseLong(filesize);//將文件大小轉換成有符號的十進制形式
/**?將文件包裝到文件輸出流對象中?*/
FileOutputStream?fos?=?new?FileOutputStream(file);
byte[]?data?=?new?byte[ois.available()];
//?ObjectInputStream的available()方法,讀取字節(jié)數(shù)
int?len;
while?((len?=?ois.read())?!=?-1)?{//?ObjectInputStream的read()方法,讀取數(shù)據(jù)字節(jié);如果讀取的字節(jié)已到達流的末尾,則返回
//?-1。
fos.write(data,?0,?len);
fos.flush();
}
serverSocket.close();
socket.close();
is.close();
isr.close();
br.close();
is1.close();
ois.close();
fos.close();
}
}
客戶端*************
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.ObjectInputStream;
import?java.io.OutputStream;
import?java.io.PrintWriter;
import?java.net.Socket;
import?java.net.UnknownHostException;
public?class?Client?{
/**
?*?客戶端:向服務端發(fā)送文件
?????*?1、建立與服務器端的連接,
?????*?2、將文件的名字和大小通過自定義的文件傳輸協(xié)議,發(fā)送到服務器
?????*?3、循環(huán)讀取本地文件,將文件打包發(fā)送到數(shù)據(jù)輸出流中
?????*?4、關閉文件,結束傳輸
?*
?*/
public?static?void?main(String[]?args)?throws?UnknownHostException,?IOException?{
String?filename=null;
?int?filesize;
//1.指定要發(fā)送的文件,并確保文件存在
????????File?sendfile?=?new?File("C:\\Users\\Lbb\\Desktop\\API.CHM");
????????if(!sendfile.exists()){
????????????System.out.println("客戶端:要發(fā)送的文件不存在");?????????
????????}
??????//2.創(chuàng)建Socket實例,指定服務器的IP地址和端口號
????????Socket?socket=new?Socket("localhost",8235);
??????//3.向服務器發(fā)送文件的協(xié)議編碼(/#),文件名(/#)和文件大小的信息
????????FileInputStream?fis=new?FileInputStream(sendfile);
????????ObjectInputStream?ois=new?ObjectInputStream(fis);
????????filename=sendfile.getName();//獲取文件名
????????filesize=ois.available();//獲取文件的字節(jié)數(shù)(文件大?。?????????
????????OutputStream?os=socket.getOutputStream();
????????PrintWriter?pw=new?PrintWriter(os);
????????pw.write("111/#"+filename+"/#"+filesize);
????????pw.flush();??
????????socket.shutdownOutput();
????????//4.向服務器傳送文件的內(nèi)容
????????byte[]data=new?byte[ois.available()];
????????int?len;
????????while((len=ois.read())!=-1){
???????? os.write(data,?0,?len);
???????? os.flush();
????????}
????????
????????socket.close();
????????fis.close();
????????ois.close();
????????os.close();
????????pw.close();
}
}
************************************錯誤信息***************************************
Exception?in?thread?"main"?java.net.SocketException:?Connection?reset
at?java.net.SocketInputStream.read(SocketInputStream.java:168)
at?sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at?sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at?sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at?java.io.InputStreamReader.read(InputStreamReader.java:167)
at?java.io.BufferedReader.fill(BufferedReader.java:136)
at?java.io.BufferedReader.readLine(BufferedReader.java:299)
at?java.io.BufferedReader.readLine(BufferedReader.java:362)
at?com.imooc.Server.main(Server.java:36)
2016-07-01
首先你應該把你的錯誤提示也貼出來,其次你在服務端并沒有關閉流。
2016-07-07
老師,我想問一下,服務端沒有關閉流指的是?
?serverSocket.close();
????????socket.close();
????????is.close();
????????isr.close();
????????br.close();
????????is1.close();
????????ois.close();
????????fos.close();
我自己認為是都關閉了的,請指教