当我做完使用API Servlet部署能够接受参数以后 发现一个新的问题又来了,就是在报表中生成的
图片和chart都是以链接本地地址存在的,那要是放在服务器上,别的机子访问时会出现图片不可用的问题。经过查一系列资料终于把问题解决了。具体原理是什么其实我也不太了解,BIRT的API我还是没怎么好好读,先把代码贴到这里再说吧,具体用的方式前面已经写过一个详细的文档了。
1 package cn.com.maxtech.fileupload;
2
3 import java.io.IOException;
4 import java.util.Enumeration;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9
10 import javax.servlet.ServletContext;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16
17 import org.eclipse.birt.report.engine.api.EngineConstants;
18
19 import org.eclipse.birt.report.engine.api.HTMLRenderContext;
20 import org.eclipse.birt.report.engine.api.HTMLRenderOption;
21
22 import org.eclipse.birt.report.engine.api.EngineConfig;
23 import org.eclipse.birt.report.engine.api.HTMLActionHandler;
24 import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
25 import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
26 import org.eclipse.birt.report.engine.api.IReportEngine;
27 import org.eclipse.birt.report.engine.api.IReportRunnable;
28 import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
29
30
31 public class WebReport extends HttpServlet {
32
33 /**
34 *
35 */
36 private static final long serialVersionUID = 1L;
37 /**
38 * Constructor of the object.
39 */
40 private IReportEngine birtReportEngine = null;
41 protected static Logger logger = Logger.getLogger( "org.eclipse.birt" );
42
43 public WebReport() {
44 super();
45 }
46
47 /**
48 * Destruction of the servlet.
49 */
50 public void destroy() {
51 super.destroy();
52 BirtEngine.destroyBirtEngine();
53 }
54
55
56 /**
57 * The doGet method of the servlet.
58 *
59 */
60 public void doGet(HttpServletRequest req, HttpServletResponse resp)
61 throws ServletException, IOException {
62
63 Map m = new HashMap();
64 Enumeration names = req.getParameterNames();
65 while (names.hasMoreElements()) {
66 String name =(String)names.nextElement();
67 String value = req.getParameter(name);
68 if (name.trim().equals("ReportName") || name.trim().equals("action") || value == null || value.trim().equals("")) {
69
70 } else {
71 m.put(name, value);
72 System.out.print(name + "--->");
73 System.out.println(m.get(name));
74 }
75
76 }
77
78 ServletContext sc = req.getSession().getServletContext();
79
80 this.birtReportEngine = BirtEngine.getBirtEngine(sc);
81 EngineConfig config = birtReportEngine.getConfig();
82 HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
83 emitterConfig.setActionHandler( new HTMLActionHandler( ) );
84 HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
85 emitterConfig.setImageHandler( imageHandler );
86
87 config.getEmitterConfigs( ).put( "html", emitterConfig );
88
89
90 resp.setContentType("text/html");
91 //resp.setContentType( "application/pdf" );
92 //resp.setHeader ("Content-Disposition","inline; filename=test.pdf");
93 String reportPath = req.getParameter("reportPath");
94 String reportName = req.getParameter("ReportName");
95
96
97 //setup image directory
98 HTMLRenderContext renderContext = new HTMLRenderContext();
99 renderContext.setBaseImageURL(req.getContextPath()+"/images");
100 renderContext.setImageDirectory(sc.getRealPath("/images"));
101
102 renderContext.setSupportedImageFormats("JPG;PNG;BMP;SVG");
103
104 logger.log( Level.FINE, "image directory " + sc.getRealPath("/images"));
105 System.out.println("stdout image directory " + sc.getRealPath("/images"));
106 System.out.println("stdout image directory " + renderContext.getBaseImageURL());
107
108 HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
109 contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
110
111 IReportRunnable design;
112 try
113 {
114 //Open report design
115 design = birtReportEngine.openReportDesign( sc.getRealPath("/upload/baobiao/" + reportPath)+"/"+reportName );
116 //create task to run and render report
117 IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design );
118 task.setAppContext( contextMap );
119
120 //set output options
121 HTMLRenderOption options = new HTMLRenderOption();
122 options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
123 //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
124 options.setOutputStream(resp.getOutputStream());
125 task.setRenderOption(options);
126
127 //run report
128 task.setParameterValues(m);
129 task.run();
130 task.close();
131 }catch (Exception e){
132
133 e.printStackTrace();
134 throw new ServletException( e );
135 }
136 }
137
138 /**
139 * The doPost method of the servlet.
140 *
141 */
142 public void doPost(HttpServletRequest request, HttpServletResponse response)
143 throws ServletException, IOException {
144
145 this.doGet(request, response);
146 }
147
148 /**
149 * Initialization of the servlet.
150 *
151 * @throws ServletException if an error occure
152 */
153 public void init() throws ServletException {
154
155 BirtEngine.initBirtConfig();
156
157 }
158
159 }
160
posted on 2008-06-18 23:51
leweslove 阅读(1133)
评论(0) 编辑 收藏 所属分类:
Other