moonlight_Answer 2021-07-19 20:37 采纳率: 0%
浏览 32

TCP/IP传输之后文件变大,格式不正确打不开

客户端发送文件给服务端,服务端将文件保存在本地。
进行图片的传输,传输成功后,文件比原来打了好多倍,本来只有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();
    }

}

img
打开之后是这样的

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-07-19 21:37
    关注
    评论

报告相同问题?

问题事件

  • 创建了问题 7月19日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog