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

时间:2025-03-20 21:47:34

页面

<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("/img/");//远程图片地址
	    	httpUrl = (HttpURLConnection) ();  
	    	();
	        //fis = new FileInputStream("d:/img/");//本地图片地址
	    	fis = ();
	        os = ();  
	        int count = 0;  
	        byte[] buffer = new byte[1024 * 8];  
	        while ((count = (buffer)) != -1) {  
	            (buffer, 0, count);  
	            ();  
	        }  
	    } catch (Exception e) {  
	        ();  
	    } finally {  
	        try {  
	            ();  
	            (); 
	            ();
	        } catch (IOException e) {  
	            ();  
	        }  
	    }   
	}

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