其他分享
首页 > 其他分享> > 前端图片通过src请求后台读取远程图片

前端图片通过src请求后台读取远程图片

作者:互联网

1.jsp页面

<img src="${ctx}/xx/xxxx/testImgUrl">

2.后台

@RequestMapping("/testImgUrl")
public void testImgUrl(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//FileInputStream fis = null;
		InputStream fis = null;
	    OutputStream os = null; 
	    URL url = null;
	    HttpURLConnection httpUrl = null;
	    try {  
	    	url = new URL("http://www.xxxx.cn/img/test.jpg");//远程图片地址
	    	httpUrl = (HttpURLConnection) url.openConnection();  
	    	httpUrl.connect();
	        //fis = new FileInputStream("d:/img/test.jpg");//本地图片地址
	    	fis = httpUrl.getInputStream();
	        os = response.getOutputStream();  
	        int count = 0;  
	        byte[] buffer = new byte[1024 * 8];  
	        while ((count = fis.read(buffer)) != -1) {  
	            os.write(buffer, 0, count);  
	            os.flush();  
	        }  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    } finally {  
	        try {  
	            fis.close();  
	            os.close(); 
	            httpUrl.disconnect();
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }   
	}

3.上传文件到远程的文件服务器,待续,,,,,,,,,, 

 

 

标签:src,读取,fis,buffer,httpUrl,new,null,os,图片
来源: https://blog.csdn.net/lixu_csdn/article/details/90345757