Salut
j'ai besoin de faire l'upload d'un fichier dans mon site web jee que je développe (jsp, servlet, hibernate3, mysql5) mais le problème c'est que je n'ai pas trouvé comment l'uploader et enregistrer son lien dans le champ correspondant de la table de ma base de données. j'ai beau essayé mais en vain, j'ai utilisé la documentation en apache commons pour construire une idée.
Pouvez vous m'aider? merci
servlet
page jsp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = FileUpload.isMultipartContent(request); if (!isMultipart) { System.out.print("not multipart"); } String nom_fichier = ""; long taille_fichier = 0; DocServices dservices = new DocServices(); List aux = dservices.checkFormation(); aux = new ArrayList(); Iterator I =aux.iterator(); DiskFileUpload upload = new DiskFileUpload(); List items = upload.parseRequest(request); Iterator itr = items.iterator(); try{ FileItem item = (FileItem) itr.next(); if (item.isFormField()) { String fieldName = item.getFieldName(); } else { File fullFile = new File(item.getName()); File savedFile = new File(getServletContext().getRealPath("/") + "/DOCUMENTS/", fullFile.getName()); nom_fichier = fullFile.getName(); taille_fichier = item.getSize() / 1024; item.write(savedFile); // Lien de téléchargement du document uploadé String link = "http://" + request.getServerName() + ":" + request.getServerPort() + "/Formation14/DOCUMENTS/" + nom_fichier; // Type du document uploadé(extension) String docType = nom_fichier.substring(nom_fichier.indexOf('.') + 1); } } } catch (Exception ex) { System.out.println("Cannot upload file\n"); request.getRequestDispatcher("upload.jsp").forward(request, response); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>uploader un fichier</title> </head> <body> <center> <h1 class="title"> Ajouter un Document </h1> </center> <br> <form name="myform" action="AjoutDoc" method="post" enctype="multipart/form-data"> Spécifier le fichier:<input type="file" name="mofile"><br/><br/> <input type="submit" name="Submit" value="Ajouter Document" /> </form> </body> </html>
Partager