Bonjour,
j'entraine de développer une application web j2ee et j'ai besoin de votre aide pour développer la tache d'upload de fichier à partir de chemin du pc de l'utilisateur et l'enregistrer dans un dossier .
merci.
Version imprimable
Bonjour,
j'entraine de développer une application web j2ee et j'ai besoin de votre aide pour développer la tache d'upload de fichier à partir de chemin du pc de l'utilisateur et l'enregistrer dans un dossier .
merci.
j'utilise fileUpload de richefaces voici mon codemais je n'ai pas réussi à ajouter mon fichier créer dans le dossierCode:
1
2
3
4
5
6
7
8
9
10
11
12 public void listener(UploadEvent event) throws Exception{ File f = new File("2013"); //Indique les dossiers à créer (si ils n'existent pas déjà). f.mkdirs(); //Créer les dossiers inexistant UploadItem item = event.getUploadItem(); Filee file = new Filee();//Créer mon fichier file.setLength(item.getData().length); file.setName(item.getFileName()); file.setData(item.getData()); files.add(file); uploadsAvailable--; }
aidez moi à trouver la solution s'il vous pait :oops::oops:
Je suis dans un cas similaire, alors je ne sais pas si ça peut t'aider par ce que moi je n'ai pas tout compris mais dans la FAQ il y a ça :
http://javaweb.developpez.com/faq/ja...UPLOAD_FICHIER
Si jamais... Bon courage !
merci cheepcheep
Sauf erreur de ma part... Il semblerait que nous ayons des recherches similaires ;)
Sinon au pire... elles restent proches ;)
Nouvelle méthode
http://www.codejava.net/java-ee/serv...servlet-30-api
Ancienne Méthode avec API de Apache Foundation...
http://www.codejava.net/java-ee/serv...ommon-file-api
N'hésitez pas à copier un lien dans Google Translate pour une traduction...
je résolue le probléme avec JSP
voici le code
File.JSP
upFile.jspCode:
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 <%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upFile.jsp" METHOD=POST> <br><br><br> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center> <td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td><b>Choose the file To Upload:</b></td> <td><INPUT NAME="file" TYPE="file"></td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Send File"> </td> </tr> <table> </center> </FORM> </BODY> </HTML>
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 <%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.zip.*"%> <%@ page import="java.util.Date,java.text.SimpleDateFormat" %> <%@ page import="java.net.*" %> <%@ page import="com.bean.*" %> <% String saveFile=""; String contentType = request.getContentType(); if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while(totalBytesRead < formDataLength){ byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br><table border="2"><tr><td><b>You have successfully upload the file:</b> <% out.println(saveFile);%></td></tr></table> <% Connection connection = null; String connectionURL = "jdbc:mysql://localhost:3306/test"; ResultSet rs = null; PreparedStatement psmnt = null; FileInputStream fis; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", ""); File f = new File(saveFile); //inserer l'url de fichier dans la bd psmnt= connection.prepareStatement("insert into dossier(urls) values(?)"); String path = new File(f.getName()).getAbsolutePath(); DossierBean ds=new DossierBean(); String nom=ds.getNomdos(); System.out.println("nom dossier"+nom); //creer un nauveau dossier File f1=new File("dossier"); f1.mkdir(); System.out.println(new File("").getAbsolutePath()); //enregister le fichier dans le dossier File dest = new File(f1.getPath()+"\\"+f.getName() ); psmnt.setNString(1, (String)dest.getAbsolutePath()); int s = psmnt.executeUpdate(); try{ // Declaration et ouverture des flux java.io.FileInputStream sourceFile = new java.io.FileInputStream(f); String path1 = new File(f.getName()).getAbsolutePath(); File f2=new File(path1); f.delete(); try{ java.io.FileOutputStream destinationFile = null; try{ destinationFile = new FileOutputStream(dest); // Lecture par segment de 0.5Mo byte buffer[] = new byte[512 * 1024]; int nbLecture; while ((nbLecture = sourceFile.read(buffer)) != -1){ destinationFile.write(buffer, 0, nbLecture); } } finally { destinationFile.close(); } } finally { sourceFile.close(); } } catch (IOException e){ e.printStackTrace(); //return false; // Erreur } // return true; // Résultat OK System.out.println("red"+dest.setReadOnly()); if(s>0){ System.out.println("Uploaded successfully !"); boolean success = f.delete(); if (!success) { System.out.println("Suppression n'a pas réussi"); } } else{ System.out.println("Error!"); } } catch(Exception e){e.printStackTrace();} } %>