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