2 k876537 k876537 于 2016.04.20 21:22 提问

java socket程序,为什么接受失败? 5C
 public class connection_server {

     static BASE64Encoder encoder = new sun.misc.BASE64Encoder();      
     static BASE64Decoder decoder = new sun.misc.BASE64Decoder();  
     static String getImageBinary(){      
            File f = new File("e://123.jpg");             
            BufferedImage bi;      
            try {      
                bi = ImageIO.read(f);      
                ByteArrayOutputStream baos = new ByteArrayOutputStream();      
                ImageIO.write(bi, "jpg", baos);      
                byte[] bytes = baos.toByteArray();      

                return encoder.encodeBuffer(bytes).trim();      
            } catch (IOException e) {      
                e.printStackTrace();      
            }      
            return null;      
        }      
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try{

            Socket socket = new Socket("139.129.39.131",5000);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

            PrintWriter os = new PrintWriter(socket.getOutputStream());
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));


            os.println(getImageBinary());
            os.flush();

            String getInfo = is.readLine();

            System.out.println(getInfo);
            base64StringToImage(getInfo);

            os.close();
            is.close();
            socket.close();
        }catch(Exception e)
        {
            System.out.println("error" + e);
        }


    }
     static void base64StringToImage(String base64String){      
            try {      
                byte[] bytes1 = decoder.decodeBuffer(base64String);      

                ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);      
                BufferedImage bi1 =ImageIO.read(bais);      
                File w2 = new File("f://QQ.jpg");//可以是jpg,png,gif格式      
                ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动      
            } catch (IOException e) {      
                e.printStackTrace();      
            }      
        }      

}

图片说明

求大神解救。

2个回答

caozhy
caozhy   Ds   Rxr 2016.04.20 22:40

ip对不对?是公网ip?有没有端口映射,防火墙的配置检查下。

CSDNXIAON
CSDNXIAON   2016.04.20 21:32

编写Java Socket程序的几种结构
基于socket的Java文件传输的程序
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!