前端图片通过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.上传文件到远程的文件服务器,待续,,,,,,,,,, 

 

 

上一篇:小菜鸟之JAVA输入输出


下一篇:Matlab FIS编辑器基本操作