package cn.bestwiz.jhf.register.iphone.common.taglibs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.validator.GenericValidator;
import cn.bestwiz.jhf.core.util.LogUtil;
/**
* 指定网络URL读取内容
*
* @author fangyp <fangyp@adv.emcom.jp>
* @copyright 2006-2010, emcom(Dalian) Co.,Ltd
*/
public class ReadOnlineContentTag extends SimpleTagSupport {
protected transient final Log log = LogUtil.getLog(getClass());
private String url;
private String charset;
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
/**
* (non-Javadoc)
*
* @return
* @throws JspException
* @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
* @author fangyp <fangyp@adv.emcom.jp>
* @throws IOException
*/
@Override
public void doTag() throws JspException, IOException {
StringBuilder output = new StringBuilder();
if (GenericValidator.isBlankOrNull(url)) {
return;
}
if (GenericValidator.isBlankOrNull(charset)) {
charset="utf-8";
}
try {
URL httpurl = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) httpurl.openConnection();
httpURLConnection.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),
charset));
String line = null;
while ((line = reader.readLine()) != null) {
output.append(line + "\r\n");
}
JspWriter jspWriter = getJspContext().getOut();
jspWriter.print(output.toString());
} catch (Exception e) {
log.info("can not find the URL file...filename=" + url);
}
}
}