Repository navigation
Expand file tree
/
Copy pathFileHandler.java
More file actions
203 lines (165 loc) · 5.66 KB
/
Copy pathFileHandler.java
File metadata and controls
203 lines (165 loc) · 5.66 KB
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
/*ASSIGNMENT NUMERO 6 LABORATORIO DI RETI A.A 2019-2020
Nome Assignment : HTTPBasedFileTransfer
Autore : Enrico Tomasi
Numero Matricola : 503527
OVERVIEW : Implementazione di un server HTTP che gestisce richieste di diverso tipo
provenienti da un browser web
*/
package httpbasedfiletransfer;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Scanner;
/*
@CLASS FileHandler
@OVERVIEW Classe che implementa la gestione dell'invio di file a partire
da una richiesta HTTP ricevuta dal browser.
*/
public class FileHandler
{
private final File file;
private final String filepath;
private Date date;
public BufferedWriter wr;
public DataOutputStream dt;
/*
@METHOD FileHandler
@OVERVIEW Costruttore di un generico FileHandler.
@PAR Path Locazione (pathname) del file.
@PAR w Buffer di scrittura della connessione da cui derivano le richieste.
@THROWS FileNotFoundException se il file non esiste.
*/
public FileHandler(String path,BufferedWriter w,DataOutputStream dat) throws FileNotFoundException, IOException
{
filepath = path;
file = new File(path);
wr = w;
dt = dat;
}
/*
@METHOD Handle
@OVERVIEW Metodo di gestione delle richieste di trasferimento file
*/
public synchronized void Handle() throws IOException
{
//Ricava il formato del file dal suo percorso
String format = filepath.substring(filepath.lastIndexOf(".")+1).trim();
//Assicurandosi prima che il file esista, gestisce la richiesta in base
//al tipo di file indicato dalla richiesta HTTP del browser
if(file.exists())
{
switch(format)
{
case "txt":
{
SendText(file);
break;
}
case "jpg":
{
SendFile(file);
break;
}
case "gif":
{
SendFile(file);
}
case "ico":
{
SendFile(file);
break;
}
default:
{
System.out.println("ERRORE: Formato di file non supportato\n");
wr.write("HTTP/1.1 400 BAD REQUEST\r\n");
wr.write("Date: "+date);
Thread.dumpStack();
throw new IllegalArgumentException();
}
}
}
else//NEL CASO IL FILE NON VENGA TROVATO, VIENE MANDATA UNA RISPOSTA DI ERRORE AL BROWSER
{
wr.write("HTTP/1.1 404 NOT FOUND\r\n");
wr.write("\r\n");
}
}
/*
@METHOD SendText
@OVERVIEW Metodo che implementa l'invio di un file di testo al browser.
@PAR t File di testo da inviare
@THROWS IOException nel caso la scrittura fallisca
*/
public void SendText(File t) throws IOException
{
System.out.println("RICHIESTO INVIO DI UN FILE DI TESTO: "+t.getName());
date = new Date();
wr.write("HTTP/1.1 201 CREATED\r\n");
wr.write("Date: "+date);
FileReader Content = new FileReader(t);
try (Scanner textscan = new Scanner(Content))
{
String ts;
wr.write("Content-type: text/plain\r\n");
wr.write("Content-length:"+file.length()+"\r\n");
wr.write("\r\n");
while(textscan.hasNext())
{
ts = textscan.nextLine();
wr.write(ts);
}
}
}
/*
@METHOD SendFile
@OVERVIEW Metodo che implementa l'invio di un file immagine in formato
jpeg o gif al browser.
@PAR f File da inviare
@THROWS FileNotFoundException nel caso il file non venga trovato
@THROWS IOException in caso la scrittura fallisca.
*/
public void SendFile(File f) throws FileNotFoundException, IOException
{
System.out.println("RICHIESTO INVIO DI UN'IMMAGINE: "+f.getName());
date = new Date();
/*
PER OTTENERE RICHIESTE VALIDE, OGNI STRINGA DELLA RISPOSTA
HTTP VIENE CONVERTITA IN ARRAY DI BYTE ED INVIATA AL BROWSER
MEDIANTE IL CANALE DI TRASFERIMENTO BUFFERIZZAO DEI DATI.
*/
String t = "HTTP/1.1 201 CREATED\r\n";
byte[] b = t.getBytes();
dt.write(b);
t = "HTTP/1.1 201 CREATED\r\n";
b = t.getBytes();
dt.write(b);
t = ("Date: "+date);
b = t.getBytes();
dt.write(b);
t = "Content-type: image/jpeg\r\n";
b = t.getBytes();
dt.write(b);
t = "Content-length:"+file.length()+"\r\n";
b = t.getBytes();
dt.write(b);
t = "\r\n";
b = t.getBytes();
dt.write(b);
FileInputStream Fstream = new FileInputStream(f);
byte[] data = new byte[(int)f.length()];
int x;
while((x = Fstream.read(data)) >= 0)
{
dt.write(data,0,x);
}
}
}