salut ;
j' ai installé un serveur dans mon pc. mais comment je peux connecter directement à partir de java au serveur en utilsant l'adress URL http://127.0.0.1:4223
je suis debutant en java svp je besoin de vous aide et merci
Version imprimable
salut ;
j' ai installé un serveur dans mon pc. mais comment je peux connecter directement à partir de java au serveur en utilsant l'adress URL http://127.0.0.1:4223
je suis debutant en java svp je besoin de vous aide et merci
Quel type de serveur ?
c'est un serveur avec la language JAVA.
mais maintenant je veux telecharger un fichier .txt à partir de ce serveur en utilsant la language JAVA ME (pour les mobiles)*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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 package principale; import java.io.*; import java.net.*; import java.util.Vector; import javax.swing.JTextArea; public class SocketApp { public String CurrentDir; //Dossier racine a afficher public Vector<Socket> vectSocket; //Les clients public ServerSocket servSocket; //Mon serveur protected JTextArea texteOut; //Sortie public SocketApp(JTextArea texte) { vectSocket = new Vector<Socket>(); texteOut = texte; try { Print("Ouverture sur le port 4850..."); servSocket = new ServerSocket(4850); Print("Serveur en écoute..."); } catch (IOException e) { Print("Echec de l'ouverture..."); return; } ListenServerSocket listen = new ListenServerSocket(); listen.start(); } public void SetCurrentDir(String dir) { CurrentDir = dir; if (CurrentDir.isEmpty() || CurrentDir.charAt(CurrentDir.length() - 1) != '/') CurrentDir += "/"; Print("Répertoire changé : " + dir); } private String TailleToString(long taille) { String retour = new String(); if (taille < 1024) retour = new String(taille + " o"); else if (taille < 1024 * 1024) retour = new String(String.valueOf((double) taille / 1024.0)+ " Ko"); else if (taille < 1024 * 1024 * 1024) retour = new String(String.valueOf((double)taille / (1024.0 * 1024.0)) + " Mo"); else if (taille < 1024 * 1024 * 1024 * 1024) retour = new String(String.valueOf((double)taille / (1024.0 * 1024.0 * 1024.0)) + " Go"); if (retour.length() > 8 ) retour = retour.substring(0, 5) + retour.substring(retour.length() - 3); return retour; } private synchronized void Print(String monTexte) { texteOut.append(monTexte + "\n"); texteOut.setCaretPosition(texteOut.getText().length()); } class ListenServerSocket extends Thread { public void run() { while(true) { try { Print("En attente de clients..."); Socket sock = servSocket.accept(); Print("Client détécté. Ip : " + sock.getInetAddress().getHostAddress() + " - Ajout à la liste."); vectSocket.add(sock); sock = null; ActionSocket action = new ActionSocket(vectSocket.size() - 1); action.start(); } catch (IOException e) { Print("L'application n'a pas pu attendre, un problème est survenu..."); } } } } class ActionSocket extends Thread { InputStream inputS; OutputStream outputS; int n; Socket s; public ActionSocket (int numSocket) throws IOException { n = numSocket; s = vectSocket.get(n); inputS = s.getInputStream(); outputS = s.getOutputStream(); } public void run() { String adressS = s.getInetAddress().getHostAddress(); try { Print("Client " + adressS + " : en attente d'une requête..."); while(inputS.available() == 0) //On attends des données { } //Extraction du corps de la requete byte[] tblBytes = new byte[inputS.available() + 1000]; int lus = inputS.read(tblBytes); String requete = new String(tblBytes,0, lus); //Extraction du lien demandé //int posDeb = 4; // int posFin = requete.indexOf("HTTP") - 1; //String lien = requete.substring(posDeb, posFin); String lien ; lien ="/7777.txt"; Print("Client " + adressS + " : demande : " + lien); //Transformation Url->String (on enleve les %20 et autres) java.net.URI url = null; try { url = new java.net.URI (lien); } catch (URISyntaxException e) { } //On enleve le '/' du début lien = url.getPath().substring(1); //Chemin d'acces demandé File file = new File(CurrentDir + lien); String pathfile = CurrentDir + lien; if (file.isDirectory()) { //Si c'est un répertoire on affiche le contenu sous format html if (pathfile.isEmpty() || pathfile.charAt(pathfile.length() - 1) != '/') { pathfile += "/"; lien += "/"; file = new File(pathfile); } Print("Client " + adressS + " : envoie une vue sur : " + file.getPath()); String toSendSocket = new String("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>Vue repertoire</title>"); toSendSocket += "<BODY BGCOLOR=\"#000000\" text=#00FF00></BODY><b>Repertoire : " + lien + "</b>" + "<br><br>"; toSendSocket += "Dossier : " + "<i>" + "<a href=\"" + "/" + lien + ".." + "\">" + ".." + "</a>" + "</i>" + "<br>"; String[] liste = file.list(new DossierFiltre(pathfile)); //Liste les dossiers for (String elem : liste) { File f = new File(pathfile + elem); System.out.println(lien); toSendSocket += "Dossier : " + "<i>" + "<a href=\"" + "/" + lien + f.getName() + "\">" + f.getName() + "</a>" + "</i>" + "<br>"; } toSendSocket += "<br>"; liste = file.list(new FichierFiltre(pathfile)); //Liste les dossiers for (String elem : liste) { File f = new File(pathfile + elem); toSendSocket += "Fichier : " + "<i>" + "<a href=\"" + "/" + lien + f.getName() + "\">" + f.getName() + "</a>" + "</i> - " + TailleToString(f.length()) + "<br>"; } toSendSocket += "<br> Programme par Zives</head></html>"; outputS.write(toSendSocket.getBytes()); //On envoie } else if (file.isFile()) { //C'est un fichier on transmet l'entete HTTP de la réponse + les données du fichier Print("Client " + adressS + " : envoie fichier : " + file.getPath()); String toSendSocket = new String("HTTP/1.1 200 OK" + "\n" + "Accept-Ranges: " + "bytes" + "\n" + "Content-Length: " + file.length() + "\n" + "Connection: Close" + "\n" + "Content-Type: " + "application/octet-stream" + "\n\n"); outputS.write(toSendSocket.getBytes()) ; FileInputStream fileStream = new FileInputStream(file); byte[] data = new byte[10240]; int longueur = 10240; while (longueur == 10240) { longueur = fileStream.read(data); outputS.write(data, 0, longueur); } fileStream.close(); Print("Client " + adressS + " : envoie fichier terminé : " + file.getPath()); } else { //On sait pas ce que c'est Print("Client " + adressS + " : requete non valable"); String toSendSocket = new String("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>Vue répertoire</title>"); toSendSocket += "<BODY BGCOLOR=\"#000000\" text=#00FF00></BODY>La source demandée n'existe pas <br>"; toSendSocket += "</head></html>"; outputS.write(toSendSocket.getBytes()) ; } //Le client devient mort outputS.close(); inputS.close(); s.close(); } catch (IOException e) { Print("Client " + adressS + " : l'envoi des données n'a pu être terminé correctement."); } vectSocket.remove(n); Print("Client " + adressS + " : client supprimé."); } } class DossierFiltre implements FilenameFilter { String acces; public DossierFiltre(String chem) { acces = chem; } public boolean accept(File dir, String name) { if (!new File(acces + name).isFile()) return true; return false; } } class FichierFiltre implements FilenameFilter { String acces; public FichierFiltre(String chem) { acces = chem; } public boolean accept(File dir, String name) { if (new File(acces + name).isFile()) return true; return false; } } }
et je ne sais pas comment ????
Su qu'apparement ce n'est pas un sevreur Web mais un serveur qui fait une connection directe via un socket, il te faut regarder du coté de la classe:javax.microedition.io.SocketConnection