0

我想通过存储在数据库表中的路径(本地服务器路径)下载文件,我已经完成了编码部分以在 html 表中查看数据库,但我不知道如何超链接表以便从存储在服务器中的输出文件夹下载文件(任何类型和大小)。这是jsp代码:

<%@ page import="java.io.*,java.sql.*"%>

<%
String id=request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver").newInstance();  
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306  /ksa","root","root");  
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select id,file_path,file_date from file12 where id like '"+id+"%'"); 
%>
<table cellpadding="15" border="1">
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString("id")%> </td>
<td><%=rs.getString("file_path")%> </td>
<td><%=rs.getString("file_date")%> </td>
</tr>
<%}%>
</table>

上面的代码将从数据库中检索表到 html 表。

4

2 回答 2

1
 <%@ page import="java.io.*,java.sql.*"%>
 <%
   String id=request.getParameter("id");
   Class.forName("com.mysql.jdbc.Driver").newInstance();  
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ksa","root","root");  
   Statement st=con.createStatement();
   ResultSet rs=st.executeQuery("Select id,file_path,file_date from file12 where id like '"+id+"%'"); 
   %>
  <table cellpadding="15" border="1">
  <%
    while(rs.next()){
  %>
  <tr>
    <td><a href="<%=rs.getString("file_path")%>"> click here to download the file with  id :<%=rs.getString("id")%> </a> </td>

    </tr>
 <%}%>
</table>
于 2012-05-08T10:07:21.547 回答
1

如果 rs.getString("file_path") 返回的路径是 /home/Desktop/output/something.jpeg 表示您无法下载。因为当您单击给定链接时,它肯定会显示 PAGE NOT FOUND (404) 异常。

"> 点击此处下载文件

请注意您的网址,它看起来像

http://localhost:8080/prjname/home/Desktop/output/something.jpeg

所以在这种情况下我们可以将路径传递给一个servlet,通过这个servlet我们可以下载文件。

于 2012-05-10T09:32:39.880 回答