客户端发送文件给服务端,服务端将文件保存在本地。
进行图片的传输,传输成功后,文件比原来打了好多倍,本来只有1.13MB,传输成功写入到硬盘的时候变成了143.57MB?这是什么原因呢。
public class TCPTest2 {
@Test
public void client() throws Exception{
// 1.
InetAddress inet1 = InetAddress.getByName("127.0.0.1");
Socket soc = new Socket(inet1, 9966);
// 2.
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("mm.jpg"));
// 3.
OutputStream os = soc.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
// 4.
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read()) != -1) {
bos.write(buffer, 0, len);
}
// 5.
bos.close();
os.close();
bis.close();
soc.close();
}
@Test
public void server() throws Exception{
// 1.
ServerSocket ss = new ServerSocket(9966);
// 2.
Socket soc = ss.accept();
// 3.
InputStream is = soc.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
// 4.
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("mm1.jpg"));
// 5.
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
// 6.
bos.close();
bis.close();
is.close();
soc.close();
ss.close();
}
}
打开之后是这样的