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 java.io.InputStream;
25 import java.io.InputStreamReader;
26
27 import jakarta.servlet.http.HttpServletRequest;
28 import jakarta.servlet.http.HttpServletResponse;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42 public class ShowFileAction extends Action {
43 private static final long serialVersionUID = -3817516672582496435L;
44
45
46
47
48 private final static Logger LOG =
49 LoggerFactory.getLogger(ShowFileAction.class);
50
51 public ActionForward execute(ActionMapping mapping,
52 ActionForm form,
53 HttpServletRequest request,
54 HttpServletResponse response)
55 throws Exception {
56
57
58 String fileName = mapping.getParameter();
59 StringBuilder fileContents = new StringBuilder();
60
61 if(fileName != null) {
62
63 try (InputStream input = servlet.getServletContext().getResourceAsStream(fileName)) {
64 if (input == null) {
65 LOG.warn("File Not Found: {}", fileName);
66 } else {
67 try (InputStreamReader inputReader = new InputStreamReader(input)) {
68 char[] buffer = new char[1000];
69 while (true) {
70 int lth = inputReader.read(buffer);
71 if (lth < 0) {
72 break;
73 } else {
74 fileContents.append(buffer, 0, lth);
75 }
76 }
77 }
78 }
79 }
80 } else {
81 LOG.error("No file name specified.");
82 }
83
84
85
86 request.setAttribute("fileName", fileName);
87 request.setAttribute("fileContents", fileContents.toString());
88
89 return mapping.findForward("success");
90 }
91 }