近期编写jsp遇到一个问题。是jsp读取模块生成html。发现有一些文章不能生成html。但是直接使用ID进行读取却没有问题。 内容比如: http://www.webxx.com.cn/Article.jsp?id=9853
代码如下:
<%@ page contentType="text/html;charset=gbk" import="java.util.*" import="java.io.*" import="java.net.*" import="java.sql.*" import="com.mysql.jdbc.Driver" pageEncoding="GBK"%>
<jsp:useBean id="border" scope="page" class="border.dbconn" />
<%
ResultSet rs=null;
Connection conn=null;
int articleid=0;
int catid=0;
String filehtml="Article.html";
String title=null;
String catdir=null;
String content=null;
conn = border.getConn();
rs = border.executeSQL("SELECT articleid,catid,title,content from webxx where articleid=9853");
while(rs.next())
{
articleid=rs.getInt("articleid");
catid=rs.getInt("catid");
title=new String(rs.getString("title").getBytes("gb2312"),"GBK");
content=new String(rs.getString("content").getBytes("gb2312"),"GBK");
switch(catid)
{
case 1:
catdir="html";
break;
case 2:
catdir="js";
break;
case 3:
catdir="xml";
break;
case 4:
catdir="css";
break;
case 5:
catdir="net";
break;
case 6:
catdir="asp";
break;
case 7:
catdir="jsp";
break;
case 8:
catdir="php";
break;
case 9:
catdir="database";
break;
}
try{
String filePath = "";
filePath = request.getRealPath("/")+"template/"+filehtml;
out.print("正在生成:");
String templateContent="";
FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
int lenght = fileinputstream.available();
out.println(lenght);
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content.toString());
String fileame = String.valueOf(articleid) +".html";
out.print(fileame);
//String path=request.getRealPath("/")+catdir+"/Data";
String path=request.getRealPath("/");
out.println(path);
File fe=new File(path);
if(!fe.exists())
{
fe.mkdir();
}
fileame = path+"/"+fileame; //生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}catch(Exception e){out.print(e.toString());}
Thread.sleep(100);
out.println("生成html成功!!!");
}
%>报的错误是:java.lang.IndexOutOfBoundsException: No group 1但是一个非常奇怪的问题:就是我把文章里面的字数删除到一半就可以生成了? "
要生成的文章内容:http://www.webxx.com.cn/Article.jsp?id=9853
想贴出来.但是文章太长了.麻烦大家了..
代码如下:
<%@ page contentType="text/html;charset=gbk" import="java.util.*" import="java.io.*" import="java.net.*" import="java.sql.*" import="com.mysql.jdbc.Driver" pageEncoding="GBK"%>
<jsp:useBean id="border" scope="page" class="border.dbconn" />
<%
ResultSet rs=null;
Connection conn=null;
int articleid=0;
int catid=0;
String filehtml="Article.html";
String title=null;
String catdir=null;
String content=null;
conn = border.getConn();
rs = border.executeSQL("SELECT articleid,catid,title,content from webxx where articleid=9853");
while(rs.next())
{
articleid=rs.getInt("articleid");
catid=rs.getInt("catid");
title=new String(rs.getString("title").getBytes("gb2312"),"GBK");
content=new String(rs.getString("content").getBytes("gb2312"),"GBK");
switch(catid)
{
case 1:
catdir="html";
break;
case 2:
catdir="js";
break;
case 3:
catdir="xml";
break;
case 4:
catdir="css";
break;
case 5:
catdir="net";
break;
case 6:
catdir="asp";
break;
case 7:
catdir="jsp";
break;
case 8:
catdir="php";
break;
case 9:
catdir="database";
break;
}
try{
String filePath = "";
filePath = request.getRealPath("/")+"template/"+filehtml;
out.print("正在生成:");
String templateContent="";
FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
int lenght = fileinputstream.available();
out.println(lenght);
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content.toString());
String fileame = String.valueOf(articleid) +".html";
out.print(fileame);
//String path=request.getRealPath("/")+catdir+"/Data";
String path=request.getRealPath("/");
out.println(path);
File fe=new File(path);
if(!fe.exists())
{
fe.mkdir();
}
fileame = path+"/"+fileame; //生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}catch(Exception e){out.print(e.toString());}
Thread.sleep(100);
out.println("生成html成功!!!");
}
%>报的错误是:java.lang.IndexOutOfBoundsException: No group 1但是一个非常奇怪的问题:就是我把文章里面的字数删除到一半就可以生成了? "
要生成的文章内容:http://www.webxx.com.cn/Article.jsp?id=9853
想贴出来.但是文章太长了.麻烦大家了..
extends RuntimeException
下列类的父类:
ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
表示某排序索引(例如对数组、字符串或向量的排序)越界时的异常信息类。 应用能继承此类,表示相似的异常信息。
---------------------------------------
这是对IndexOutOfBoundsException的解释,可能是你的文章太长了造成String不支持那么多字符
建议使用StringBuffer试试
也是没有成功..希望能出上例子.谢谢...