第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

socket傳送文件,不能正常運(yùn)行,自己又找不到問(wèn)題,急求解

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

	/**
	?*?服務(wù)端:實(shí)現(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實(shí)例,指定綁定的端口號(hào),并監(jiān)聽(tīng)次端口
		ServerSocket?serverSocket?=?new?ServerSocket(8235);
		//?2.調(diào)用accept()方法開(kāi)始監(jiān)聽(tīng),等待客戶端的連接
		Socket?socket?=?serverSocket.accept();
		System.out.println("***啟動(dòng)服務(wù)器,等待連接…………………………");
		//?3.讀取客戶端發(fā)送的請(qǐng)求信息
		InputStream?is?=?socket.getInputStream();
		InputStreamReader?isr?=?new?InputStreamReader(is);
		BufferedReader?br?=?new?BufferedReader(isr);
		String?info=br.readLine()?;
			//?4.解析客戶端發(fā)送的文件信息,正確的,保存文件名以及文件的大小,為下面接收文件內(nèi)容做準(zhǔn)備
			int?index?=?info.indexOf("/#");//?查找/#在字符串中的索引位置
			String?daima?=?info.substring(0,?index);//?獲取字符串從0位置-index(不包括)之間的字符串內(nèi)容
			if?(!daima.equals("111"))?{
				System.out.println("服務(wù)器收到的文件的協(xié)議代碼和客戶端的文件協(xié)議代碼不匹配??!");
			}?else?{
				System.out.println("文件的協(xié)議代碼一致?。?!");
				String?infoChild?=?info.substring(index?+?2);
				//?獲得info字符串的子字符串,該子字符串從指定索引處的字符開(kāi)始,直到此字符串末尾部分的內(nèi)容
				int?indexC?=?infoChild.indexOf("/#");
				filename?=?infoChild.substring(0,?index).trim();//?trim忽略前導(dǎo)空白和尾部空白
				filesize?=?infoChild.substring(index?+?2).trim();
			}
			//?將接收到的文件保存在本地
		????file?=?new?File(filename);
			if?(!file.exists())?{
				file.createNewFile();
			}?else?{
				System.out.println("本路徑已存在相同文件,進(jìn)行覆蓋");
			}
		
		
		//?5.服務(wù)器接收文件內(nèi)容,并將內(nèi)容保存到本地文件中
		InputStream?is1?=?socket.getInputStream();//?獲取傳送文件的字節(jié)流
		ObjectInputStream?ois?=?new?ObjectInputStream(is1);
		//?long?fileSize?=?Long.parseLong(filesize);//將文件大小轉(zhuǎn)換成有符號(hào)的十進(jìn)制形式
		/**?將文件包裝到文件輸出流對(duì)象中?*/
		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é)已到達(dá)流的末尾,則返回
											//?-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?{

	/**
	?*?客戶端:向服務(wù)端發(fā)送文件
?????*?1、建立與服務(wù)器端的連接,
?????*?2、將文件的名字和大小通過(guò)自定義的文件傳輸協(xié)議,發(fā)送到服務(wù)器
?????*?3、循環(huán)讀取本地文件,將文件打包發(fā)送到數(shù)據(jù)輸出流中
?????*?4、關(guān)閉文件,結(jié)束傳輸
	?*
	?*/
	
	
	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實(shí)例,指定服務(wù)器的IP地址和端口號(hào)
????????Socket?socket=new?Socket("localhost",8235);
??????//3.向服務(wù)器發(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.向服務(wù)器傳送文件的內(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();


	}

}


正在回答

2 回答

感覺(jué)你的好復(fù)雜,只是我的http://idcbgp.cn/article/11793

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

OutputStream?os=socket.getOutputStream();

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

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

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

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


0 回復(fù) 有任何疑惑可以回復(fù)我~

服務(wù)端代碼為什么,一會(huì)是index 一會(huì)是indexc

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉Coder 提問(wèn)者

int index = info.indexOf("/#"); int indexC = infoChild.indexOf("/#");,因?yàn)榇淼乃饕恢貌煌?,所以我就用index和indexC來(lái)區(qū)分,然后 filename = infoChild.substring(0, index).trim();filesize = infoChild.substring(index + 2).trim();是我寫錯(cuò)了,應(yīng)該是indexC,但是改過(guò)之后仍然異常^^^^^
2016-07-07 回復(fù) 有任何疑惑可以回復(fù)我~
#2

染紅_街道 回復(fù) 慕粉Coder 提問(wèn)者

異常信息是啥 貼出來(lái)
2016-07-11 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

socket傳送文件,不能正常運(yùn)行,自己又找不到問(wèn)題,急求解

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)