1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.struts.webapp.validator;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26
27 import org.apache.struts.action.Action;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.commons.logging.Log;
33
34 /***
35 * Action which retrieves a file specified in the parameter
36 * and stores its contents in the request, so that they
37 * can be displayed.
38 */
39 public class ShowFileAction extends Action {
40
41 /*** Logging Instance. */
42 private static final Log log = LogFactory.getLog(ShowFileAction.class);
43
44 public ActionForward execute(ActionMapping mapping,
45 ActionForm form,
46 HttpServletRequest request,
47 HttpServletResponse response)
48 throws Exception {
49
50
51 String fileName = mapping.getParameter();
52 StringBuffer fileContents = new StringBuffer();
53
54 if(fileName != null) {
55
56 InputStream input = servlet.getServletContext().getResourceAsStream(fileName);
57 if (input == null) {
58 log.warn("File Not Found: "+fileName);
59 } else {
60 InputStreamReader inputReader = new InputStreamReader(input);
61 char[] buffer = new char[1000];
62 while (true) {
63 int lth = inputReader.read(buffer);
64 if (lth < 0) {
65 break;
66 } else {
67 fileContents.append(buffer, 0, lth);
68 }
69 }
70 }
71 } else {
72 log.error("No file name specified.");
73 }
74
75
76
77 request.setAttribute("fileName", fileName);
78 request.setAttribute("fileContents", fileContents.toString());
79
80 return mapping.findForward("success");
81 }
82 }