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

来源:互联网 发布:球球代点刷棒棒源码 编辑:程序博客网 时间:2024/05/17 21:54

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
原创粉丝点击