forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendImageServlet.java
More file actions
50 lines (36 loc) · 1.56 KB
/
SendImageServlet.java
File metadata and controls
50 lines (36 loc) · 1.56 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
package com.zetcode;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
// The method returns null if no file is found. The file name should be
// preceded by a forward slash ("/") which indicates the file is available
// in a folder relative to the current context root directory.
//https://stackoverflow.com/questions/1829784/should-i-close-the-servlet-outputstream
@WebServlet(name = "SendImageServlet", urlPatterns = {"/image"})
public class SendImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
ServletContext sc = getServletContext();
try (InputStream is = sc.getResourceAsStream("/images/sid.jpg")) {
// it is the responsibility of the container to close output stream
OutputStream os = response.getOutputStream();
if (is == null) {
response.setContentType("text/plain");
os.write("Failed to send image".getBytes());
} else {
byte[] buffer = new byte[1024];
int bytesRead;
response.setContentType("image/png");
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
}
}
}