【问题标题】:How to download an image with Sockets [duplicate]如何使用套接字下载图像[重复]
【发布时间】:2019-01-09 14:46:52
【问题描述】:

所以我正在练习使用 Sockets 从 Web 获取图像。我不确定在存储图像字节然后创建文件时使用哪个类。到目前为止我的代码是:

    import java.io.*;
    import java.net.*;
    import java.awt.image.*;
    import javax.imageio.stream.ImageInputStream;
    import javax.imageio.ImageIO;

    class MyClass{
        public static void main(String[] args)throws IOException{
            Socket s = new Socket();
            ImageInputStream s_in = null; //I'm not sure about this
            PrintWriter s_out = null;

            try{
                s.connect(new InetSocketAddress("data.pr4e.org",80));
                System.out.println("Connected");

                s_out = new PrintWriter(s.getOutputStream(), true);

                s_in = ImageIO.createImageInputStream(s.getInputStream());//nor this
            }
            catch (UnknownHostException e){
                System.err.println("Don't know about host");
                System.exit(1);
            }

            //Message to server
            String message = "GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n";
            s_out.println(message);

            //This is where it gets confusing
            OutputStream out = null;
            while (true){
            try{
            out = new BufferedOutputStream(new 
            FileOutputStream("C:\\Users\\Steff\\Desktop\\Java ejemplos\\cover3.jpg"));
            out.write(s_in.read());
            }
            finally{
              if(out != null){
              out.close();
           }  
        }
    }
}

}

【问题讨论】:

    标签: java image sockets


    【解决方案1】:

    我稍微修正了你的代码。它将所有数据保存到文件中。

    public static void main(String[] args) throws IOException {
        Socket s = new Socket();
        InputStream s_in = null;
        PrintWriter s_out = null;
    
        try {
            s.connect(new InetSocketAddress("data.pr4e.org", 80));
            System.out.println("Connected");
            s_out = new PrintWriter(s.getOutputStream(), true);
            s_in = s.getInputStream();
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        }
    
        // Message to server
        String message = "GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n";
        s_out.println(message);
    
        OutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Steff\\Desktop\\Java ejemplos\\cover3.jpg"));
        int count;
        byte[] buffer = new byte[4096];
        while ((count = s_in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
        }
        out.close();
    }
    

    不幸的是,图像不可读。为什么?因为保存的数据包含整个流以及 HTTP 响应。这就是您在不使用诸如 Apache HttpClient 之类的 HTTP 感知库的情况下使用套接字的结果。您可以使用任何文本编辑器打开文件以查看内容。

    如果您确定并想继续使用套接字,请咨询duplicate linked by gtgaxiola。它包含更多代码来处理将标头与实际数据分开的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 2012-01-30
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2020-10-22
      相关资源
      最近更新 更多