我用Tomee创建超文本传输协议服务器,我把jasper报告文件(.jasper)放在webapp目录下。如果我在浏览器中访问http://localhost:8080/test.jasper,浏览器会提示下载文件。
在我的java项目中,我创建了简单的代码来访问该链接,然后预览报表。我使用async-http-client库来请求。
DefaultAsyncHttpClient client = new DefaultAsyncHttpClient();
BoundRequestBuilder brb = client.prepareGet("http://localhost:8765/qa/test.jasper");
Future<InputStream> f = brb.execute(new AsyncCompletionHandler<InputStream>() {
@Override
public InputStream onCompleted(Response resp) {
try {
String[][] data = {{"Jakarta"},{"Surabaya"},{"Solo"},{"Denpasar"}};
String[] columnNames = {"City"};
DefaultTableModel dtm = new DefaultTableModel(data, columnNames);
Map<String,Object> params = new HashMap<>();
JasperPrint jPrint = JasperFillManager.fillReport(
resp.getResponseBodyAsStream(),
params,
new JRTableModelDataSource(dtm)
);
JasperViewer jpView = new JasperViewer(jPrint,false);
jpView.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jpView.setSize(800, 600);
jpView.setLocationRelativeTo(null);
jpView.setVisible(true);
} catch (JRException ex) {
System.out.println(ex.getMessage());
}
return resp.getResponseBodyAsStream();
}
});从上面的代码中,我得到了一个错误Error loading object from InputStream
通常我可以使用
InputStream input = MainContext.class.getResourceAsStream(filename);但是我想用http请求替换文件输入流(也是流)。
如何准确地使用http服务器提供.jasper文件...?
发布于 2017-06-06 15:27:12
Error loading object from InputStream错误来自损坏的InputStream,如果我通过浏览器正常下载.jasper文件并用JRLoader.loadObjectFromFile(path to file)执行报告,它也不能工作,因为我给损坏的文件(源文件没有损坏)。
我自己的解决方案是以流的形式读取源文件,将其转换为base64编码,并通过HTTP API协议提供。
finput = new FileInputStream(sPath);
byte[] bFile = Base64.getEncoder().encode(IOUtils.toByteArray(finput));
String sFile = new String(bFile);在客户端内部,我将其作为正文字符串接收,解码base64字符串,将其转换为InputStream,最后使用InputStream执行报告。
byte[] bBody = Base64.getDecoder().decode(sBody);
InputStream mainReport = new ByteArrayInputStream(bBody);
return JasperFillManager.fillReport(mainReport, params);https://stackoverflow.com/questions/44361887
复制相似问题