A więc próbuję przesłać po sieci plik binarny wykorzystując do tego klasę PrintWriter.
Tego kodu używam do odczytania pliku, przesłania go i utworzenia logu, gdzie ‘out’ to też jest obiekt PrintWriter:
private static void sendFile(File file)
{
FileInputStream fReader = null;
try
{
fReader = new FileInputStream(file.getAbsolutePath());
}
catch(Exception e){e.printStackTrace(); return;}
int c,len;
try
{
PrintWriter pw = new PrintWriter(new FileWriter("a.log"));
len = fReader.available();
out.println("file:"+file.getName()+":"+len);
for (int i=0; i
{
c = fReader.read();
out.write(c);
//System.out.println(c);
pw.println(c);
}
pw.close();
fReader.close();
}
catch(Exception e){e.printStackTrace();}
}
a oto kod, który używam do odebrania wysłanej wiadomości
input = in.readLine();
String [] args = input.split(":");
System.out.println(input);
int len = Integer.parseInt(args[2]);
int i=0;
int c = 0;
root.println(input);
PrintWriter pw = new PrintWriter(new FileWriter("b.log"));
while (i
{
c = in.read();
pw.println(c);
i++;
}
pw.close();
nie wiem dlaczego, ale logi się od siebie częściowo różnią, a jeśli utworzę plik binarny z danych, które odbiorę, to nie chce się on uruchamiać tak jak powinien… oto fragmenty logów: a.log (oryginalne wartości bajtów, które chcę przesłać)
77
90
144
0
3
0
0
0
4
0
0
0
255
255
0
0
184
0
0
0
0
0
0
0
64
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
128
0
0
0
14
31
186
14
0
180
9
205
33
184
1
76
205
33
84
104
105
115
32
112
114
111
103
114
97
109
32
99
97
110
110
111
116
32
98
101
b.log (wartości, które otrzymuję)
77
90
63
0
3
0
0
0
4
0
0
0
63
63
0
0
184
0
0
0
0
0
0
0
64
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
63
0
0
0
14
31
63
14
0
180
9
205
33
184
1
76
205
33
84
104
105
115
32
112
114
111
103
114
97
109
32
99
97
110
110
111
116
32
98
101
różnice pojawiają się tylko w niektórych bajtach… skąd się one biorą?