UReport2实现自定义路径图片加载

UReport是一款开源的报表编辑器,可以根据它提供的接口,来实现一些自定义的功能,今天就记录一下如何自定义图片加载。

从官方文档看出,要实现自定义,就需要实现这个接口
UReport2实现自定义路径图片加载_第1张图片

官方默认的图片上传路径的实现:

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;
    }
}

仿照上面的官方默认代码来实现自定义图片加载:

package indi.qiaolin.test.Picture;

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.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Component
public class PictureUpload 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{
                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(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;
    }

}

你可能感兴趣的:(java,spring,spring,boot)