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 org.apache.struts.action.Action;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35
36
37 /**
38 * Read image from resource given as ActionMapping parameter
39 * and copy to output stream.
40 *
41 * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
42 */
43
44 public class ImageAction extends Action {
45
46
47 /**
48 * Read image from resource given as ActionMapping parameter
49 * and copy to output stream.
50 *
51 * @exception java.lang.Exception on input/output error
52 */
53 public ActionForward execute(
54 ActionMapping mapping,
55 ActionForm form,
56 HttpServletRequest request,
57 HttpServletResponse response)
58 throws Exception {
59
60
61
62 String image = mapping.getParameter();
63
64 byte[] buffer = new byte[2048];
65 int bytesRead;
66 InputStream input =
67 getServlet().getServletContext().getResourceAsStream(image);
68 OutputStream
69 out = response.getOutputStream();
70
71 while ((bytesRead = input.read(buffer)) != -1) {
72 out.write(buffer, 0, bytesRead);
73 }
74 out.close();
75
76 return null;
77 }
78
79 }