Afficher un lien vers un document et le télécharger par servlet
salut,
bon j'arrive a le faire mais j'ai un autre problème.
j'ai créer une page basic.jsp pour tracer le tableau est afficher des infos(etat,nom,date)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<HTML>
<HEAD>
<TITLE>Fetching Data From a Database</TITLE>
</HEAD>
<BODY>
<H1>Liste des Documents</H1>
<%
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/cdgcapitall", "root", "root");
Statement statement = connection.createStatement();
ResultSet resultset =
statement.executeQuery("select nom_pdf, date_pdf, nom_etat,nom_user from pdf,etat,user,service,direction, categorie where user.id_pdf=pdf.id and user.id_etat=etat.id and id_service=1 and id_direction=1 and id_categorie=1") ;
if(!resultset.next()) {
out.println("Sorry, could not find that publisher. ");
} else {
%>
<TABLE BORDER="1">
<TR>
<TH>nom</TH>
<TH>date</TH>
<TH>etat</TH>
<TH>responsable</TH>
</TR>
<% do { %>
<TR>
<TD> <a href="retrive.jsp"> <%= resultset.getString(1) %></a> </TD>
<TD> <%= resultset.getString(2) %> </TD>
<TD> <%= resultset.getString(3) %> </TD>
<TD> <%= resultset.getString(4) %> </TD>
</TR>
<% } while(resultset.next()); %>
</TABLE>
<BR>
<%
}
%>
</BODY>
</HTML> |
apres j'ai creer une page retrive.jsp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="javax.servlet.http.HttpSession"%>
<%@ page language="java"%>
<%@ page session="true"%>
<%
try{
//PrintWriter out=response.getWriter();
out.println("Retrieve Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/cdgcapitall";
String userName = "root";
String password = "root";
Connection con = null;
Class.forName(driverName);
con = DriverManager.getConnection(url,userName,password);
Statement st = con.createStatement();
%>
<%
PreparedStatement pre1 = con.prepareStatement("select pdf from pdf where nom_pdf='doc1'");
ResultSet rs1=pre1.executeQuery();
while(rs1.next())
{byte[] bytearray1 = new byte[4096];
int size1=0;
InputStream sImage1;
sImage1 = rs1.getBinaryStream(1);
response.reset();
response.setContentType("op/pdf");
response.addHeader("Content-Disposition","filename=logo.pdf");
while((size1=sImage1.read(bytearray1))!= -1 )
{
response.getOutputStream().write(bytearray1,0,size1);
}
response.flushBuffer();
sImage1.close();
rs1.close();
}
out.println("Retrieved Successfully!");
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>
</body>
</html> |
bon mon problème est que quand j'affiche le nom de document je fais un click puis il m'affiche le document mais le document ne correspond au nom merci de m'aider.