File f = new File("F:/head.jpg");
     try {
String str_attachmemt = "";
FileInputStream in = new FileInputStream(f);
System.out.println("可读大小:" + in.available()); byte[] b = new byte[in.available()];
int len = in.read(b, 0, b.length);
str_attachmemt += new String(b, 0, len, "GB2312");
System.out.println(str_attachmemt + "\n" + str_attachmemt.getBytes("GB2312").length); } catch (FileNotFoundException e) {         
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
打印如下:
可读大小:27569
20564
以上是一个测试socket那边传过来的数据为:size:27569
图片字节信息
........
........ 
那边的数据是一段一段分包过来的
我如何根据这个size正确接收到图片

解决方案 »

  1.   

    我的博客
    http://yijianfengvip.blog.163.com/blog/static/1752734322012113112733610/ClientTcpSend.java   客户端发送类package com.yjf.test;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;public class ClientTcpSend {
        
        public static String clientip = "127.0.0.1";
        public static int port = 33456;    public static void main(String[] args) {
            int length = 0;
            byte[] sendBytes = null;
            Socket socket = null;
            DataOutputStream dos = null;
            FileInputStream fis = null;
            try {
                try {
                    socket = new Socket();
                    socket.connect(new InetSocketAddress(clientip, port),30 * 1000);
                    dos = new DataOutputStream(socket.getOutputStream());
                    File file = new File("F:\\aa.xml");
                    fis = new FileInputStream(file);
                    sendBytes = new byte[1024*4];
                    while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
                        dos.write(sendBytes, 0, length);
                        dos.flush();
                    }
                } finally {
                    if (dos != null)
                        dos.close();
                    if (fis != null)
                        fis.close();
                    if (socket != null)
                        socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    ServerTcpListener.java 服务器监听类package com.yjf.test;
    import java.net.*;
    import java.io.*;public class ServerTcpListener implements Runnable {    public static void main(String[] args) {        try {
                final ServerSocket server = new ServerSocket(ClientTcpSend.port);
                Thread th = new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            try {
                                System.out.println("开始监听...");
                                Socket socket = server.accept();
                                System.out.println("有链接");
                                receiveFile(socket);
                            } catch (Exception e) {
                            }
                        }
                    }            });            th.run(); //启动线程运行
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public void run() {
        }    public static void receiveFile(Socket socket) {        byte[] inputByte = null;
            int length = 0;
            DataInputStream dis = null;
            FileOutputStream fos = null;
            try {
                try {                dis = new DataInputStream(socket.getInputStream());
                    fos = new FileOutputStream(new File("E:\\aa.xml"));
                    inputByte = new byte[1024*4];
                    System.out.println("开始接收数据...");
                    while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
                        fos.write(inputByte, 0, length);
                        fos.flush();
                    }
                    System.out.println("完成接收");
                } finally {
                    if (fos != null)
                        fos.close();
                    if (dis != null)
                        dis.close();
                    if (socket != null)
                        socket.close();
                }
            } catch (Exception e) {        }    }
    }java socket通信-传输文件图片--传输图片java socket通信-传输文件图片--传输图片
      

  2.   


    主要是返回给我的是:
    size:27569
    图片字节信息
    ........
    ........
    上面多个字符串size:27569
      

  3.   

    如果没有size:27569这一段 
    就能接收了...
      

  4.   

    回答你的第一个问题
    由于F:/head.jpg并不是gb2312的格式,所以你读取
    str_attachmemt += new String(b, 0, len, "GB2312");为乱码
    在乱码的基础上使用
    str_attachmemt.getBytes("GB2312").length会丢失信息
    改为iso-8859-1就不会有丢失的情况了try {
    String str_attachmemt = "";
    FileInputStream in = new FileInputStream(f);
    System.out.println("可读大小:" + in.available());
    byte[] b = new byte[in.available()];
    int len = in.read(b, 0, b.length);
    str_attachmemt += new String(b, 0, len, "iso-8859-1");
    System.out.println(str_attachmemt + "\n"
    + str_attachmemt.getBytes("iso-8859-1").length);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  5.   

    回答你的第二个问题:
    size:27569通常使用固定的字节数,比如是5个字节
    byte[] size=new byte[5];
    先读取大小信息
    byte[] bs=new byte[byteToInt(size)];
    接下来读取文件字节就可以了