Подскажите как правильно начитать InputStream в Фильтре, что имею
MD5ServletInputStream
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.
public class MD5ServletInputStream extends ServletInputStream {
private final ServletInputStream input;
private final MessageDigest md5;
{
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new ExceptionInInitializerError(e);
}
}
public MD5ServletInputStream(ServletInputStream input) {
this.input = input;
}
public byte[] getHash() {
return md5.digest();
}
@Override
public int read() throws IOException {
int read = input.read();
if (read != -1) {
md5.update((byte) read);
}
return read;
}
public int read(byte b[]) throws IOException {
int read = input.read(b, 0, b.length);
if (read != -1) {
md5.update(b);
}
return read;
}
public int read(byte b[], int off, int len) throws IOException {
int read = input.read(b, off, len);
if (read != -1) {
md5.update(b);
}
return read;
}
}
MD5ServletRequest
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.
public class MD5ServletRequest extends HttpServletRequestWrapper {
private final MD5ServletInputStream input;
private byte[] bytes = null;
public MD5ServletRequest(HttpServletRequest request) throws IOException {
super(request);
input = new MD5ServletInputStream(request.getInputStream());
}
public ServletInputStream getInputStream() throws IOException {
return input;
}
public byte[] getHash() {
return input.getHash();
}
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
public String getMD5() {
byte[] hash = getHash();
StringBuilder hashAsHexString = new StringBuilder(
hash.length * 2);
for (byte b : hash) {
hashAsHexString.append(String.format("%02x", b));
}
return hashAsHexString.toString();
}
}
И часть из фильтра
1.
2.
3.
4.
5.
6.
7.
8.
9.
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
MD5ServletRequest md5Request = new MD5ServletRequest(httpRequest);
chain.doFilter(md5Request, response);
}
Хочу, чтоб когда запрос пришел на сервлет
Я мог сделать MD5ServletRequest md5RequestюgetMD5() и получить хэш загружаемого файла