淘先锋技术网

首页 1 2 3 4 5 6 7

jsp咋启命令提示符?

首先创建一个读取文件的类,方法readFile读取文件,参数1:文件路径,参数2:字符集(gb2312,utf-8等)

FileUtil.java源码:

package core.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class FileUtil {

public static String readFile(String filePath, String charset)

{

String s = "";

try

{

File f = new File(filePath);

if (f.exists())

{

FileInputStream bw = new FileInputStream(f);

int len = bw.available();

byte[] str = new byte[len];

if (bw.read(str) == -1)

{

s = "";

}

else

{

s = new String(str, charset);

}

bw.close();

}

}

catch (IOException e)

{

e.printStackTrace();

}

return s;

}

}

在thispage.jsp中调用:

<%

String show = request.getParameter("show");

if ("yes".equals(show))

{

String str = FileUtil.readFile("c:\123.txt","gn2312");

out.print(str);

}

%>

<script language="javascript">

function doShow()

{

window.location.href="thispage.jsp?show=yes";

}

</script>

<input typ="button" name=button value="显示" onclick="doShow()">