Bonjour
J'aimerais envoyer un fichier d'un client vers un serveur. La lecture du fichier s’effectuera ligne par ligne en considérant chaque ligne comme une trame.
Mais le problème c'est que je ne maîtrise pas vraiment le java Socket pour l'envoi des trames.
Est-ce que vous pouvez m'aider, j'ai fait le code sans trame. Comment intégrer les trames dans mon code. Voici mon code :
cote client
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 import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Arrays; import java.lang.*; import java.util.Scanner; public class UploadC { private static int BUFFER_SIZE; public static void main(String[] args) throws Exception { String fileName = null; try { fileName = args[0]; } catch (Exception e) { System.out.println("Enter the name of the file :"); Scanner scanner = new Scanner(System.in); String file_name = scanner.nextLine(); File file = new File(file_name); Socket socket = new Socket("localhost", 3332); ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(file.getName()); FileInputStream fis = new FileInputStream(file); byte [] buffer = new byte[UploadC.BUFFER_SIZE]; Integer bytesRead = 0; while ((bytesRead = fis.read(buffer)) > 0) { oos.writeObject(bytesRead); oos.writeObject(Arrays.copyOf(buffer, buffer.length)); } oos.close(); ois.close(); System.exit(0); } } }
cote serveur
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
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 package uploadS; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.io.File; public class UploadS extends Thread { public static final int PORT = 3332; public static final int BUFFER_SIZE = 100; @Override public void run() { try { ServerSocket serverSocket = new ServerSocket(PORT); while (true) { Socket s = serverSocket.accept(); saveFile(s); } } catch (Exception e) { e.printStackTrace(); } } private void saveFile(Socket socket) throws Exception { ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); FileOutputStream fos = null; byte [] buffer = new byte[BUFFER_SIZE]; // 1. Read file name. Object o = ois.readObject(); /* if (o instanceof String) { fos = new FileOutputStream(o.toString()); } else { throwException("Something is wrong"); } */ if (o instanceof String) { File dir = new File("/home/user/"); fos = new FileOutputStream(o.toString()); FileOutputStream location = new FileOutputStream(new File(dir,o.toString())); } // 2. Read file to the end. Integer bytesRead = 0; do { o = ois.readObject(); if (!(o instanceof Integer)) { throwException("Something is wrong"); } bytesRead = (Integer)o; o = ois.readObject(); if (!(o instanceof byte[])) { throwException("Something is wrong"); } buffer = (byte[])o; // 3. Write data to output file. fos.write(buffer, 0, bytesRead); } while (bytesRead == BUFFER_SIZE); System.out.println("File transfer success"); fos.close(); ois.close(); oos.close(); } public static void throwException(String message) throws Exception { throw new Exception(message); } public static void main(String[] args) { new UploadS().start(); } }
Partager