UReport2报表图片显示
- 看UReport2图片加载的源码
package com.bstek.ureport.provider.image;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.WebApplicationContext;
import com.bstek.ureport.exception.ReportComputeException;
/**
* @author Jacky.gao
* @since 2017年3月6日
*/
public class DefaultImageProvider implements ImageProvider,ApplicationContextAware {
private ApplicationContext applicationContext;
private String baseWebPath;
@Override
public InputStream getImage(String path) {
try {
if(path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX) || path.startsWith("/WEB-INF")){
return applicationContext.getResource(path).getInputStream();
}else{
path=baseWebPath+path;
return new FileInputStream(path);
}
} catch (IOException e) {
throw new ReportComputeException(e);
}
}
@Override
public boolean support(String path) {
if(path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)){
return true;
}else if(baseWebPath!=null && (path.startsWith("/") || path.startsWith("/WEB-INF"))){
return true;
}
return false;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(applicationContext instanceof WebApplicationContext){
WebApplicationContext context=(WebApplicationContext)applicationContext;
baseWebPath=context.getServletContext().getRealPath("/");
}
this.applicationContext=applicationContext;
}
}
可以看出源码需要在设计页面配置“classpath:”开头的地址,并且图片得在本地源码指定目录,如图:
2.自定义图片路径
自定义就需要重新实现接口ImageProvider,ApplicationContextAware,自定义默认路径,代码如下:
package io.renren.ureport.provider;
import com.bstek.ureport.exception.ReportComputeException;
import com.bstek.ureport.provider.image.ImageProvider;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@Component
@Primary
public class ImageReportForm implements ImageProvider, ApplicationContextAware {
private ApplicationContext applicationContext;
private String baseWebPath;
@Override
public InputStream getImage(String path) {
System.out.println(path);
try {
if( path.startsWith("E:\\ureportfiles\\picture")){
//返回一个输入流,该输入流的读取位置是传入进来的path,也就是界面上输入的地址
File file=new File(path);
InputStream inputStream=new FileInputStream(file);
return inputStream;
}else if(path.startsWith("http://")||path.startsWith("https://")) {
URL url = new URL(path);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
return inStream;
} else{
path=baseWebPath+path;
return new FileInputStream(path);
}
} catch (IOException e) {
throw new ReportComputeException(e);
}
}
@Override
public boolean support(String path) {
if(path.startsWith("E:\\ureportfiles\\picture")){
return true;
}else if(path.startsWith("http://")||path.startsWith("https://")){
return true;
}else if(baseWebPath!=null && path.startsWith("E:\\ureportfiles\\picture")){
return true;
}
return false;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(applicationContext instanceof WebApplicationContext){
WebApplicationContext context=(WebApplicationContext)applicationContext;
baseWebPath=context.getServletContext().getRealPath("E:\\ureportfiles\\picture");
}
this.applicationContext=applicationContext;
}
}
自定义实现接口后,此时就可以在设计页面配置自定义的 E:\ureportfiles\picture目录下图片或http https开头的的网络图片了
注意自定义实现接口后,@Primary注解必须加上 否则还走默认实现类