1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts.webapp.exercise;
24
25
26 import java.io.InputStream;
27 import java.io.OutputStream;
28
29 import jakarta.servlet.http.HttpServletRequest;
30 import jakarta.servlet.http.HttpServletResponse;
31
32 import org.apache.struts.action.Action;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36
37
38
39
40
41
42
43
44
45 public class ImageAction extends Action {
46 private static final long serialVersionUID = -917797528635967028L;
47
48
49
50
51
52
53
54
55 public ActionForward execute(
56 ActionMapping mapping,
57 ActionForm form,
58 HttpServletRequest request,
59 HttpServletResponse response)
60 throws Exception {
61
62
63
64 String image = mapping.getParameter();
65
66 byte[] buffer = new byte[2048];
67 int bytesRead;
68
69 try (InputStream input = getServlet().getServletContext().getResourceAsStream(image)) {
70 final OutputStream out = response.getOutputStream();
71
72 while ((bytesRead = input.read(buffer)) != -1) {
73 out.write(buffer, 0, bytesRead);
74 }
75 }
76
77 return null;
78 }
79 }