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()">