亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

socket傳送文件,不能正常運行,自己又找不到問題,急求解

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?{

	/**
	?*?服務端:實現接收客戶端發送的文件
	?*?
	?*?@throws?IOException
	?*/
	
	
	public?static?void?main(String[]?args)?throws?IOException?{
		String?filename?=?null;
		String?filesize?=?null;
		File?file=null;
		
		//?1.創建ServerSocket實例,指定綁定的端口號,并監聽次端口
		ServerSocket?serverSocket?=?new?ServerSocket(8235);
		//?2.調用accept()方法開始監聽,等待客戶端的連接
		Socket?socket?=?serverSocket.accept();
		System.out.println("***啟動服務器,等待連接…………………………");
		//?3.讀取客戶端發送的請求信息
		InputStream?is?=?socket.getInputStream();
		InputStreamReader?isr?=?new?InputStreamReader(is);
		BufferedReader?br?=?new?BufferedReader(isr);
		String?info=br.readLine()?;
			//?4.解析客戶端發送的文件信息,正確的,保存文件名以及文件的大小,為下面接收文件內容做準備
			int?index?=?info.indexOf("/#");//?查找/#在字符串中的索引位置
			String?daima?=?info.substring(0,?index);//?獲取字符串從0位置-index(不包括)之間的字符串內容
			if?(!daima.equals("111"))?{
				System.out.println("服務器收到的文件的協議代碼和客戶端的文件協議代碼不匹配??!");
			}?else?{
				System.out.println("文件的協議代碼一致!??!");
				String?infoChild?=?info.substring(index?+?2);
				//?獲得info字符串的子字符串,該子字符串從指定索引處的字符開始,直到此字符串末尾部分的內容
				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.服務器接收文件內容,并將內容保存到本地文件中
		InputStream?is1?=?socket.getInputStream();//?獲取傳送文件的字節流
		ObjectInputStream?ois?=?new?ObjectInputStream(is1);
		//?long?fileSize?=?Long.parseLong(filesize);//將文件大小轉換成有符號的十進制形式
		/**?將文件包裝到文件輸出流對象中?*/
		FileOutputStream?fos?=?new?FileOutputStream(file);
		byte[]?data?=?new?byte[ois.available()];
		//?ObjectInputStream的available()方法,讀取字節數
		int?len;
		while?((len?=?ois.read())?!=?-1)?{//?ObjectInputStream的read()方法,讀取數據字節;如果讀取的字節已到達流的末尾,則返回
											//?-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?{

	/**
	?*?客戶端:向服務端發送文件
?????*?1、建立與服務器端的連接,
?????*?2、將文件的名字和大小通過自定義的文件傳輸協議,發送到服務器
?????*?3、循環讀取本地文件,將文件打包發送到數據輸出流中
?????*?4、關閉文件,結束傳輸
	?*
	?*/
	
	
	public?static?void?main(String[]?args)?throws?UnknownHostException,?IOException?{
		
		String?filename=null;
		?int?filesize;
		
		//1.指定要發送的文件,并確保文件存在
????????File?sendfile?=?new?File("C:\\Users\\Lbb\\Desktop\\API.CHM");
????????if(!sendfile.exists()){
????????????System.out.println("客戶端:要發送的文件不存在");?????????
????????}
??????//2.創建Socket實例,指定服務器的IP地址和端口號
????????Socket?socket=new?Socket("localhost",8235);
??????//3.向服務器發送文件的協議編碼(/#),文件名(/#)和文件大小的信息
????????FileInputStream?fis=new?FileInputStream(sendfile);
????????ObjectInputStream?ois=new?ObjectInputStream(fis);
????????filename=sendfile.getName();//獲取文件名
????????filesize=ois.available();//獲取文件的字節數(文件大?。?????????
????????OutputStream?os=socket.getOutputStream();
????????PrintWriter?pw=new?PrintWriter(os);
????????pw.write("111/#"+filename+"/#"+filesize);
????????pw.flush();??
????????socket.shutdownOutput();

????????//4.向服務器傳送文件的內容
????????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();


	}

}


正在回答

2 回答

感覺你的好復雜,只是我的http://www.xianlaiwan.cn/article/11793

還有你這部分我看不太懂,請將以下,/#是干什么用的

OutputStream?os=socket.getOutputStream();

????????PrintWriter?pw=new?PrintWriter(os);

????????pw.write("111/#"+filename+"/#"+filesize);

????????pw.flush();??

????????socket.shutdownOutput();


0 回復 有任何疑惑可以回復我~

服務端代碼為什么,一會是index 一會是indexc

0 回復 有任何疑惑可以回復我~
#1

慕粉Coder 提問者

int index = info.indexOf("/#"); int indexC = infoChild.indexOf("/#");,因為代表的索引位置不同,所以我就用index和indexC來區分,然后 filename = infoChild.substring(0, index).trim();filesize = infoChild.substring(index + 2).trim();是我寫錯了,應該是indexC,但是改過之后仍然異常^^^^^
2016-07-07 回復 有任何疑惑可以回復我~
#2

染紅_街道 回復 慕粉Coder 提問者

異常信息是啥 貼出來
2016-07-11 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

socket傳送文件,不能正常運行,自己又找不到問題,急求解

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號