是这样的,我有两个jsp页面,第一个页面如下
,我上传的文件已经保存在一个文件夹中,文件名写入了数据库的Filetable中,现在我希望点击相应文件的下载链接就可以下载该文件,我的代码如下<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><%!
public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<%//接收要下载的文件ID
long Filetable_id=Long.parseLong(request.getParameter("Filetable_id"));
%>
<%//构造修改记录SQL语句
String sqlString=null;//SQL语句
sqlString="select Filetable_name from Filetable where Filetable_id="+Filetable_id;
%>
<%//执行SQL语句
try
{
Connection con;
Statement sql;
ResultSet rs;
// 加载JDBC驱动器类
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 建立到TestDB的数据库连接
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=testsystem", "sa", "1987915"); Statement stmt=con.createStatement();
rs=stmt.executeQuery(sqlString);
session.putValue("MM_text",rs);
con.close();
}
catch(SQLException el)
{
out.print("SQL异常");
}
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE3 {font-size: 24px}
-->
</style>
</head><body>
<% //得到向客户输出的输出流:
OutputStream o=response.getOutputStream();
//输出文件用的字节数组,每次发送600个字节到输出流:
byte b[]=new byte[600];
//要下载的文件:
File fileLoad=new File("c:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/root/upload/",session.getValue("MM_test"));
//客户使用保存文件的对话框:
response.setHeader("Content-disposition","attachment;filename=session.getValue(MM_test)");
//OutputStream o=response.getOutputStream();
//通知客户文件的MIME类型:
response.setContentType("application/msword");
//通知客户文件的长度:
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
//读取文件,并发送给客户下载:
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1)
{ o.write(b,0,n);
}
out.clear();
out = pageContext.pushBody();
%></body></html>
错误提示是
exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 67 in the jsp file: /DownloadFile2.jsp
The constructor File(String, Object) is undefined
64: //输出文件用的字节数组,每次发送600个字节到输出流:
65: byte b[]=new byte[600];
66: //要下载的文件:
67: File fileLoad=new File("c:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/root/upload/",session.getValue("MM_test"));
68: //客户使用保存文件的对话框:
69: response.setHeader("Content-disposition","attachment;filename=session.getValue(MM_test)");
70: //OutputStream o=response.getOutputStream();请问我 哪里错了呢?谢谢大家帮忙
,我上传的文件已经保存在一个文件夹中,文件名写入了数据库的Filetable中,现在我希望点击相应文件的下载链接就可以下载该文件,我的代码如下<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><%!
public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<%//接收要下载的文件ID
long Filetable_id=Long.parseLong(request.getParameter("Filetable_id"));
%>
<%//构造修改记录SQL语句
String sqlString=null;//SQL语句
sqlString="select Filetable_name from Filetable where Filetable_id="+Filetable_id;
%>
<%//执行SQL语句
try
{
Connection con;
Statement sql;
ResultSet rs;
// 加载JDBC驱动器类
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 建立到TestDB的数据库连接
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=testsystem", "sa", "1987915"); Statement stmt=con.createStatement();
rs=stmt.executeQuery(sqlString);
session.putValue("MM_text",rs);
con.close();
}
catch(SQLException el)
{
out.print("SQL异常");
}
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE3 {font-size: 24px}
-->
</style>
</head><body>
<% //得到向客户输出的输出流:
OutputStream o=response.getOutputStream();
//输出文件用的字节数组,每次发送600个字节到输出流:
byte b[]=new byte[600];
//要下载的文件:
File fileLoad=new File("c:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/root/upload/",session.getValue("MM_test"));
//客户使用保存文件的对话框:
response.setHeader("Content-disposition","attachment;filename=session.getValue(MM_test)");
//OutputStream o=response.getOutputStream();
//通知客户文件的MIME类型:
response.setContentType("application/msword");
//通知客户文件的长度:
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
//读取文件,并发送给客户下载:
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1)
{ o.write(b,0,n);
}
out.clear();
out = pageContext.pushBody();
%></body></html>
错误提示是
exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 67 in the jsp file: /DownloadFile2.jsp
The constructor File(String, Object) is undefined
64: //输出文件用的字节数组,每次发送600个字节到输出流:
65: byte b[]=new byte[600];
66: //要下载的文件:
67: File fileLoad=new File("c:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/root/upload/",session.getValue("MM_test"));
68: //客户使用保存文件的对话框:
69: response.setHeader("Content-disposition","attachment;filename=session.getValue(MM_test)");
70: //OutputStream o=response.getOutputStream();请问我 哪里错了呢?谢谢大家帮忙
The constructor File(String, Object) is undefined这里的提示很明确,楼主你的File类只是引用了,没有定义