class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.0.100", 10008);
FileInputStream fis = new FileInputStream
("D:\\班服图案\\44.bmp");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10008);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"....connected");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("server.bmp");
byte[] buf = new byte[1024];
int len = 0;
while ((len=in.read())!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket(InetAddress.getLocalHost().getHostAddress(), 10008);
FileInputStream fis = new FileInputStream
("D://JAVA//学习笔记//布局管理器.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
out.flush();
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10008);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"....connected");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("server.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len=in.read(buf))!=-1)//楼主你这里read方法没有读入缓冲区,len=in.read()返回的是一个字节
//所以你原来得到的那个几十M的文件,读取的都是缓冲数组buf里面N多的默认值,所以得到的文件就什么也看不见而且很大
//另外在测试的时候还有一个有趣的问题,服务端的流对象加了刷新方法比不加刷新所得到的文件占用空间要小,但是文件大小是一样的,有点疑惑,希望能有高人解
//答???
{
fos.write(len);
fos.flush();
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());