黑马程序员技术交流社区
标题:
BufferedInputStream包装s.getInputStream的疑问?
[打印本页]
作者:
umbriel
时间:
2015-8-9 09:44
标题:
BufferedInputStream包装s.getInputStream的疑问?
在做TCP传输文件的题目时,发现服务端读取socket的输入流用BufferedInputStream包装后就会出错,直接调用s.getInputStream就没问题。这个问题耗费了我好久时间,哪位大神能给解答下?
作者:
umbriel
时间:
2015-8-9 09:45
in=new BufferedInputStream(s.getInputStream());读取文件就会错误,大小不一样
in=s.getInputStream 这样就没问题
作者:
umbriel
时间:
2015-8-9 09:49
客户端想服务端的outputStream同理,也不能用BufferedOutputStream类包装
作者:
真实姓名被注册
时间:
2015-8-9 09:54
我刚刚试了一下,感觉没问题
import java.net.*;
import java.io.*;
class TCPclient2{
public static void main(String[] args){
Socket s=null;
try{
s=new Socket(InetAddress.getLocalHost(),18888);
}catch(UnknownHostException e1){
throw new RuntimeException(e1);
}catch(IOException e2){
throw new RuntimeException(e2);
}
BufferedWriter bw=null;
BufferedReader br=null;
try{
bw=new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(s.getOutputStream())));
}catch(IOException e3){
throw new RuntimeException(e3);
}
try{
br=new BufferedReader(new InputStreamReader(new BufferedInputStream(s.getInputStream())));
}catch(IOException e4){
throw new RuntimeException(e4);
}
try{
bw.write("TCP客户端来了!");
bw.newLine();
bw.flush();
s.shutdownOutput();
}catch(IOException e5){
throw new RuntimeException(e5);
}
String buff=null;
try{
while((buff=br.readLine())!=null)
System.out.println(buff);
}catch(IOException e6){
throw new RuntimeException(e6);
}
try{
s.close();
}catch(IOException e7){
throw new RuntimeException(e7);
}
}
}
import java.net.*;
import java.io.*;
class TCPserver2{
public static void main(String[] args){
ServerSocket ss=null;
Socket s=null;
try{
ss=new ServerSocket(18888);
}catch(SocketException e1){
throw new RuntimeException(e1);
}catch(IOException e2){
throw new RuntimeException(e2);
}
try{
s=ss.accept();
}catch(IOException e3){
throw new RuntimeException(e3);
}
System.out.println("ip"+s.getLocalAddress().getHostAddress()+"......"+"已连接");
BufferedReader br=null;
BufferedWriter bw=null;
try{
br=new BufferedReader(new InputStreamReader(new BufferedInputStream(s.getInputStream())));
}catch(IOException e4){
throw new RuntimeException(e4);
}
try{
bw=new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(s.getOutputStream())));
}catch(IOException e5){
throw new RuntimeException(e5);
}
String buff=null;
try{
while((buff=br.readLine())!=null)
System.out.println("ip"+s.getLocalAddress().getHostAddress()+"......"+buff);
}catch(IOException e5){
throw new RuntimeException(e5);
}
try{
bw.write("服务器已连接");
bw.newLine();
bw.flush();
}catch(IOException e6){
System.out.println("服务器写异常");
throw new RuntimeException(e6);
}
try{
s.close();
}catch(IOException e7){
throw new RuntimeException(e7);
}
try{
ss.close();
}catch(IOException e8){
throw new RuntimeException(e8);
}
}
}
作者:
蓬头小道士
时间:
2015-8-9 10:59
能不能看看代码先?
作者:
umbriel
时间:
2015-8-9 11:41
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class ClientDemo {
public static void main(String[] args) {
//定义需用到的流,先赋值null
BufferedInputStream bis=null;
BufferedInputStream fin=null;
//OutputStream out=null;
BufferedOutputStream out=null;
File f=new File("E:\\Test01.java");
Socket s=null;
try{
s=new Socket("127.0.0.1",10002);
fin=new BufferedInputStream(new FileInputStream(f));
bis=new BufferedInputStream(s.getInputStream());
//out=s.getOutputStream();
out=new BufferedOutputStream(s.getOutputStream()); //就是这句,用BufferedOutputStream包装后复制的文件就出错了
byte[] buf=new byte[1024];
int len=0;
while((len=fin.read(buf,0,buf.length))!=-1){
out.write(buf, 0, len);
}
s.shutdownOutput();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(s!=null)s.close();
}catch (IOException e) {
e.printStackTrace();
}
try{
if(fin!=null)fin.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
class ServerDemo{
public static void main(String[] args) {
//定义需用到的流,先赋值null
InputStream in=null;
OutputStream bos=null;
BufferedOutputStream fout=null;
ServerSocket ss=null;
Socket s=null;
try{
ss=new ServerSocket(10002);
s=ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"-----已连接");
in=s.getInputStream();
bos=s.getOutputStream();
File f=new File("E:\\rec.java");
fout=new BufferedOutputStream(new FileOutputStream(f));
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf,0,buf.length))!=-1){
fout.write(buf,0,len);
}
bos.write("文件已上传至Server!".getBytes());
System.out.println("文件已上传至Server!");
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
if(s!=null)s.close();
}catch (IOException e) {
e.printStackTrace();
}
try{
if(ss!=null)ss.close();
}catch (IOException e) {
e.printStackTrace();
}
try{
if(fout!=null)fout.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制代码
把ClientDemo中的out用BufferedOutputString包装后复制的文件就出错了。
作者:
umbriel
时间:
2015-8-9 11:43
难道是和操作文件有关?我用的字节流,包装后复制文本和图片都有问题
作者:
真实姓名被注册
时间:
2015-8-9 12:05
兄弟,你没有flush()
作者:
umbriel
时间:
2015-8-9 12:48
真实姓名被注册 发表于 2015-8-9 12:05
兄弟,你没有flush()
字节流不需要flush啊
作者:
真实姓名被注册
时间:
2015-8-9 12:51
umbriel 发表于 2015-8-9 12:48
字节流不需要flush啊
你用的是缓冲字节流
作者:
蓬头小道士
时间:
2015-8-9 13:32
out=new BufferedOutputStream(s.getOutputStream()); 改为out=new BufferedOutputStream(new OutputStreamWriter(s.getOutputStream())); 原因你的文件为字符流需要转换为字节流,才能被Socket的输出流接收,OutputStreamWriter为字符到字节的转换流。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2