分享

java 通过流的方式读取远程图片并显示在jsp页面(类型以jpg、png等结尾的图片)

 小丑g22xft6chp 2017-04-26

Java 代码:


String path = "http://61.148.200.38:16980/project/1GS/DNA_online/map/";


                URL url;

                try {

                    url = new URL(path+"1-ref.png");

                    HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();

                    String messagePic = urlconn.getHeaderField(0);//文件存在‘HTTP/1.1 200 OK’ 文件不存在 ‘HTTP/1.1 404 Not Found’

                    if (messagePic.startsWith("HTTP/1.1 200")) {

                        InputStream is = url.openStream();

                        int i = is.available(); // 得到文件大小  

                        byte[] buffer = new byte[i];

                        is.read(buffer); // 读数据  

                        is.close();  

                        response.setContentType("image/*"); // 设置返回的文件类型  

                        OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象  

                        toClient.write(buffer); // 输出数据  

                        toClient.close();  

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }




jsp 页面代码:


<img id="mutationImage" style="height:150px;width:750px;" src="${pageContext.request.contextPath}/two-check/validate-order-mutation/seeChrom.action?chromId=${request.chromId}&t=<s:property value='verifyResultNote'/>"/>





t=<s:property value='verifyResultNote'/>表示时间戳 
  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多