jsp: img src
//=======================jsp======防盗链处理开始================================
//内容
var contentHtml = dynamicVo.content;
//秀米静态图片
contentHtml = contentHtml.replaceAll('http://statics.xiumi.us',''+baseURL+'/portal/errcode/errcodeAction!loadImageEx.action?imgUrl=http://statics.xiumi.us');
//秀米动态图片
contentHtml = contentHtml.replaceAll('http://img.xiumi.us',baseURL+'/portal/errcode/errcodeAction!loadImageEx.action?imgUrl=http://img.xiumi.us');
contentHtml = contentHtml.replaceAll('src="//img.xiumi.us','src="${httpsBaseUrl}${baseURL}/portal/errcode/errcodeAction!loadImageEx.action?imgUrl=http://img.xiumi.us');
//=======================jsp======防盗链处理结束================================
//=======================action======防盗链处理开始================================
/**
* 获取网络图片以流的方式输出-代理
*
* @author Sun Qinghai
* @2014-10-27
* @version 1.0
*/
public void loadImageEx(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream toClient = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
String imgUrl = request.getParameter("imgUrl");
// 是否使用代理方式访问外网
String isUseProxy = ConfigMgr.getStringCfg("shdz_proxy", "isUseProxy","1");
if ("0".equals(isUseProxy)) {
try {
if(AssertUtil.isEmpty(imgUrl)){
return;
}
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//通过URL获取图片
URL url = null;
int BUFFER_SIZE = 1024;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
url = new URL(imgUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.setConnectTimeout(5000);
httpUrl.setReadTimeout(30000);
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
//得到向客户端输出二进制数据的对象
toClient=response.getOutputStream();
while ((size = bis.read(buf)) != -1) {
//输出数据
toClient.write(buf, 0, size);
}
toClient.flush();
} catch (Exception e) {
logger.error("获取图片流失败,imgUrl="+imgUrl,e);
}finally{
if(toClient!=null){
try {
toClient.close();
} catch (IOException e) {
logger.error("获取图片流,关闭response.getOutputStream异常!", e);
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
logger.error("获取图片流,关闭BufferedInputStream异常!", e);
}
}
if(httpUrl != null){
httpUrl.disconnect();
}
}
} else {
try {
String proxyUrl = ProxyExUtil.proxyUrlMain;
int proxyPort = ProxyExUtil.proxyPortMain;
URL server = new URL(imgUrl);
Authenticator.setDefault(new ProxyUtil.MyAuthenticator("", ""));
ProxyUtil.initProxyEx(proxyUrl, proxyPort, "", "");
HttpURLConnection connection = (HttpURLConnection) server.openConnection();
//connection.setRequestProperty("Connection", "Keep-Alive");
//connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");
connection.setConnectTimeout(5000);
connection.setReadTimeout(30000);
connection.connect();
int BUFFER_SIZE = 1024;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
bis = new BufferedInputStream(connection.getInputStream());
//得到向客户端输出二进制数据的对象
toClient=response.getOutputStream();
while ((size = bis.read(buf)) != -1) {
//输出数据
toClient.write(buf, 0, size);
}
toClient.flush();
} catch (Exception e) {
logger.error("获取图片流失败,imgUrl="+imgUrl,e);
}finally{
if(toClient!=null){
try {
toClient.close();
} catch (IOException e) {
logger.error("获取图片流,关闭response.getOutputStream异常!", e);
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
logger.error("获取图片流,关闭BufferedInputStream异常!", e);
}
}
if(httpUrl != null){
httpUrl.disconnect();
}
}
}
}
//=======================action======防盗链处理结束================================
< END >
程序员NBA
一个有故事的程序员
或者加我微信1414200300一起讨论java技术,有什么问题群里人也可以帮你一起解决