通过它们在JSP路径检索数据库文件中(retrieving files from database

2019-07-29 07:20发布

我想下载它的路径(本地服务器路径)的文件存储在数据库中的表,我也做了编码部分在一个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表的表。

Answer 1:

如果rs.getString(“FILE_PATH”)返回的路径是/home/Desktop/output/something.jpeg意味着当你点击链接给予肯定,它会显示找不到网页(404)异常,你不能download.Because。

“>点击这里下载文件

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

HTTP://本地主机:8080 / prjname /家用/桌面/输出/ something.jpeg

所以在这种情况下,我们可以通过路径到一个servlet中,通过这个servlet,我们可以下载该文件。



Answer 2:

 <%@ 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>


文章来源: retrieving files from database by their path in jsp