问题描述
- java socket程序,为什么接受失败?
-
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(); } } }
求大神解救。
解决方案
ip对不对?是公网ip?有没有端口映射,防火墙的配置检查下。
解决方案二:
编写Java Socket程序的几种结构
基于socket的Java文件传输的程序
时间: 2024-11-01 19:11:26