栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

http服务器于浏览器简单的沟通,发送图片

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

http服务器于浏览器简单的沟通,发送图片

在yg帮助下用Java做的,读取本地一张图片,发给浏览器(Client)

import javax.imageio.stream.FileImageInputStream;
import java.io.* ;
import java.net.* ;

public class Phttpd{
    public static void main(String[] args) throws  IOException{
        ServerSocket servsock;
        Socket sock ;
        OutputStream out ;
        BufferedReader in ;
        FileInputStream infile = null;
        byte[] buff = new byte[1024];
        boolean cont;
        int i ;
        File file=new File("/Users/hwg/Desktop/大三上/EX2_NETWORK/web/a.html");
        try{
            servsock = new ServerSocket(8099);
            while(true){
                sock = servsock.accept();
                try{
                    infile = new FileInputStream(file) ;
                }
                catch(FileNotFoundException e){
                    System.err.println("No found the file!!") ;
                    e.printStackTrace();
                }
                in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                out = sock.getOutputStream() ;

                for(i=0;i<2;i++) {
                    System.out.println(in.readLine());
                }
                cont = true ;
                //响应报文
                out.write("http/1.1 200 OKrn".getBytes());
                out.write("Content-Type: image/jpeg;rncontent-length: 144759rn".getBytes());
                out.write("rn".getBytes());
                
                String img="/Users/hwg/Desktop/IMG_2111.jpeg";
                FileInputStream fiis=new FileInputStream(new File(img));
                int it=0;
                while ((it=fiis.read(buff))!=-1){
                    out.write(buff);
                }
                out.flush();
                fiis.close();
                sock.close() ;
                infile.close() ;
            }
        }catch(IOException e){
            e.printStackTrace();
            System.exit(1) ;
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298288.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号