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
|
public void doGet( HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Le chargement du driver fonctionne");
}
catch (Exception ex) {
System.out.println("erreur");
}
Connection conn;
FileInputStream fis;
try{
String url = "jdbc:mysql://localhost:xxx";
conn = DriverManager.getConnection(url, "mdp", "kwj30eqe") ;
System.out.println("URL ok!");
System.out.println(request.getParameter("nom"));
File img2 = new File(request.getParameter("fichier"));
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("insert into fichier(cat_fic,nom_fic,fic_fic) VALUES (?,?,?)");
pstmt.setInt(1,Integer.parseInt(request.getParameter("categorie")));
pstmt.setString(2,request.getParameter("nom"));
fis = new FileInputStream(img2);
pstmt.setBinaryStream(3,(InputStream)fis, (int)(img.length()));
int s =pstmt.executeUpdate();
if (s>0)
{
System.out.println("Fichier uploadé");
}
else
{
System.out.println("Erreur, le fichier n'a pas pu etre uploadé");
}
request.getRequestDispatcher("uploadResult.jsp").forward(request, response);
pstmt.close();
conn.close();
}
catch( SQLException ex ) {
System.out.println("Erreur dans URL!");
// cas d'erreur
}
}
public void doPost( HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
doGet(request, response);
} |
Partager