socket输入流连续文件,如何把各个文件的数据分开来?
我在些一个局域网传输文件夹的程序,在连续传送文件时,发送端连续写入文件,接受端接收时却无法鉴别各个文件,将所有数据写入了一个文件,请各位指教。
接收端代码:
private void output(BufferedReader br,BufferedInputStream bis,Socket s)
{
String readStr;
byte[] buf = new byte[1024*10];
int len;
try
{
while(true)
{
readStr=br.readLine();
System.out.println(readStr);
String isFile=readStr.substring(0,1);
file=readStr.substring(1);
if(isFile.equals( "1 "))
{
FileOutputStream fos = new FileOutputStream( "E:\\ "+file);
BufferedOutputStream bos=new BufferedOutputStream(fos,1024*10);
System.out.println( "creating "+file+ "...... ");
while((len=bis.read(buf,0,1024*10))!=-1)
{
bos.write(buf,0,len);
bos.flush();
}
fos.close();
bis.close();
System.out.println( "creating "+file+ " completed! ");
}
else if(isFile.equals( "0 "))
{
File f=new File( "E:\\ "+file);
System.out.println( "creating directory..... "+f);
if(f.mkdir())
System.out.println( "creating completed..... ");
}
else
return;
}
}
catch(
IOException e)
{
e.printStackTrace();
}
}
发送端代码:
private void sender(File file,BufferedWriter bw,BufferedOutputStream bos)
{
if(file.isFile())
{
sendFile(file,bw,bos);
}
else
{
sendDirec(file,bw);
File[] fileList=file.listFiles();
for(int i=0;i <fileList.length;i++)
{
if(fileList[i].isFile())
{
sendFile(fileList[i],bw,bos);
}
else if(i <fileList.length)
{
sender(fileList[i],bw,bos);
}
else
{
return;
}
}
}