您的位置:首页 > 编程语言 > Java开发

java静态页面生成技术--循环输出列表--BUG临时修复

2010-10-28 09:26 549 查看
/**
* 生成静态页面
*
* 添加信息后执行,执行完调用显示全部商家信息
*/
@Action(value = "crebusin", results = { @Result(name = "crebusinok", type = "chain", location = "flshowbusin") })
public String busincre() throws Exception {
req = ServletActionContext.getRequest();

String fpath = req.getRealPath("/");// 获得服务器绝对路径

String templateContent = null;

FileInputStream fileinputstream = new FileInputStream(fpath
+ "bk-hqxxmb.html");// 读取模板页,位于网站根目录
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];

StringBuffer sb=new StringBuffer();

while(fileinputstream.read(bytes)!=-1)
{
sb.append(new String(bytes,"GBK"));
}

fileinputstream.close();
templateContent = new String(bytes);

session.beginTransaction();

/* 循环8大板块 */
for (int n = 1; n <= 8; n++) {
qy = session
.createQuery("from BusinBean bus where bus.businshow='1' and bus.busbk='"
+ n + "' order by  bus.busid desc");

qy.setFirstResult(0);
qy.setMaxResults(8);

businlist = qy.list();

for (Iterator iterator = businlist.iterator(); iterator.hasNext();) {
BusinBean b = (BusinBean) iterator.next();
templateContent = templateContent.replaceAll(
"##" + n + x + "#", b.getBustitle());
if (b.getBusxs().equals("0")) {
templateContent = templateContent.replaceAll("/"#r" + n + x
+ "#/"", "ac/busindet?buscid=" + b.getBusid()
+ "&&t=2");
} else if (b.getBusxs().equals("1")) {
templateContent = templateContent.replaceAll("/"#r" + n + x
+ "#/"", b.getBusurl());
}
x++;
}

qy = null;
businlist = null;
x = 1;
}

/* 输出静态页面 */
FileOutputStream fileoutputstream = new FileOutputStream(fpath
+ "hqxxbk.html");
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();

session.getTransaction().commit();
return "crebusinok";
}


解法:在读取文件时已发现中文乱码,所以在读取时追加一段代码

/*转码解决输出中文乱码问题*/
StringBuffer sb=new StringBuffer();

while(fileinputstream.read(bytes)!=-1)
{
sb.append(new String(bytes,"GBK"));
}


注意点:模板页面和输出目标页统一编码为GBK,目前UTF-8编码测试未通过,存在乱码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: