如题:
client: Socket s2 = new Socket(s22,911);
InputStream is=s2.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
//当我把客户机上的文件通过
BufferedInputStream bfIn = new BufferedInputStream(new FileInputStream(
new File(f.getAbsolutePath())));
byte[] data = new byte[1444];
int byteRead;
while ((byteRead = bfIn.read(data)) != -1) {
dos.write(data, 0, byteRead);
dos.flush();
}
bfIn.close();
//写入到输出流后(准备发往server),上面结束后,再执行下面的语句就出问题了
dos.writeUTF("abc");
dos.flush();
//--上面就无法重新输出到server了。不知道什么问题
server:
ServerSocket ss=new ServerSocket(911);
System.out.println("连接成功");
Socket s2=ss.accept();
is=s2.getInputStream();
dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
dos=new DataOutputStream(os);
//--把客户端发送的流保存到文件
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(path));
byte[] data = new byte[1444];
int byteRead;
try {
while ((byteRead = dis.read(data)) != -1) {
bufOut.write(data, 0, byteRead);
bufOut.flush();
}
bufOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
请大家赐教!!
client: Socket s2 = new Socket(s22,911);
InputStream is=s2.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
//当我把客户机上的文件通过
BufferedInputStream bfIn = new BufferedInputStream(new FileInputStream(
new File(f.getAbsolutePath())));
byte[] data = new byte[1444];
int byteRead;
while ((byteRead = bfIn.read(data)) != -1) {
dos.write(data, 0, byteRead);
dos.flush();
}
bfIn.close();
//写入到输出流后(准备发往server),上面结束后,再执行下面的语句就出问题了
dos.writeUTF("abc");
dos.flush();
//--上面就无法重新输出到server了。不知道什么问题
server:
ServerSocket ss=new ServerSocket(911);
System.out.println("连接成功");
Socket s2=ss.accept();
is=s2.getInputStream();
dis=new DataInputStream(is);
OutputStream os=s2.getOutputStream();
dos=new DataOutputStream(os);
//--把客户端发送的流保存到文件
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(path));
byte[] data = new byte[1444];
int byteRead;
try {
while ((byteRead = dis.read(data)) != -1) {
bufOut.write(data, 0, byteRead);
bufOut.flush();
}
bufOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
请大家赐教!!
这个地方没问题,因为文件流可以被server读取,并且可以写入文件。就是客户dos.write(data, 0, byteRead);
后,就没办法再使用dos.writeUTF("aa");了。不知道什么原因。
然后再dos.writeUTF("xx");就不行了。继续求助。
while ((byteRead = bfIn.read(data)) != -1) {
dos.write(data, 0, byteRead);
dos.flush();
}
感觉怪怪的,bfIn.read(data)本身已把数据读取到字节数组中去了,下面又调用dos.write(data, 0, byteRead);重新写入,是不是这里有问题