当前位置: 首页 > 工具软件 > UReport > 使用案例 >

ureport自定义报表存储至数据库

徐丰茂
2023-12-01

转载:

https://blog.csdn.net/qq_35170213/article/details/80290425

 

参考源码类: 

/*******************************************************************************
 * Copyright 2017 Bstek
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.bstek.ureport.provider.report.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;

import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;

/**
 * @author Jacky.gao
 * @since 2017年2月11日
 */
public class FileReportProvider implements ReportProvider,ApplicationContextAware{
	private String prefix="file:";
	private String fileStoreDir;
	private boolean disabled;
	@Override
	public InputStream loadReport(String file) {
		if(file.startsWith(prefix)){
			file=file.substring(prefix.length(),file.length());
		}
		String fullPath=fileStoreDir+"/"+file;
		try {
			return new FileInputStream(fullPath);
		} catch (FileNotFoundException e) {
			throw new ReportException(e);
		}
	}
	
	@Override
	public void deleteReport(String file) {
		if(file.startsWith(prefix)){
			file=file.substring(prefix.length(),file.length());
		}
		String fullPath=fileStoreDir+"/"+file;
		File f=new File(fullPath);
		if(f.exists()){
			f.delete();
		}
	}

	@Override
	public List<ReportFile> getReportFiles() {
		File file=new File(fileStoreDir);
		List<ReportFile> list=new ArrayList<ReportFile>();
		for(File f:file.listFiles()){
			Calendar calendar=Calendar.getInstance();
			calendar.setTimeInMillis(f.lastModified());
			list.add(new ReportFile(f.getName(),calendar.getTime()));
		}
		Collections.sort(list, new Comparator<ReportFile>(){
			@Override
			public int compare(ReportFile f1, ReportFile f2) {
				return f2.getUpdateDate().compareTo(f1.getUpdateDate());
			}
		});
		return list;
	}

	@Override
	public String getName() {
		return "服务器文件系统";
	}
	
	@Override
	public void saveReport(String file,String content) {
		if(file.startsWith(prefix)){
			file=file.substring(prefix.length(),file.length());
		}
		String fullPath=fileStoreDir+"/"+file;
		FileOutputStream outStream=null;
		try{
			outStream=new FileOutputStream(new File(fullPath));
			IOUtils.write(content, outStream,"utf-8");
		}catch(Exception ex){
			throw new ReportException(ex);
		}finally{
			if(outStream!=null){
				try {
					outStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	@Override
	public boolean disabled() {
		return disabled;
	}
	
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	
	public void setFileStoreDir(String fileStoreDir) {
		this.fileStoreDir = fileStoreDir;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		File file=new File(fileStoreDir);
		if(file.exists()){
			return;
		}
		if(applicationContext instanceof WebApplicationContext){
			WebApplicationContext context=(WebApplicationContext)applicationContext;
			ServletContext servletContext=context.getServletContext();
			String basePath=servletContext.getRealPath("/");
			fileStoreDir=basePath+fileStoreDir;
			file=new File(fileStoreDir);
			if(!file.exists()){
				file.mkdirs();
			}
		}
	}

	@Override
	public String getPrefix() {
		return prefix;
	}
}

 

自己修改的后的类:

package com.qingtui.server;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.qingtui.bean.UreportFileEntity;
import com.qingtui.dao.UreportFileMapper;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 报表储存
 *
 * @author qiaofeng
 */
@Component
public class SqlProvider implements ReportProvider {

    private static final String NAME = "乔峰文件服务器系统";

    //特定前缀,ureport底层调用getPrefix方法来获报表操作的Provier类
    private String prefix = "qiaofeng";

    //是否禁用
    private boolean disable = false;

    @Autowired
    UreportFileMapper ureportFileMapper;

    //加载表报
    public InputStream loadReport(String fileName) {
        UreportFileEntity ureportFileEntity = ureportFileMapper.queryUreportFileEntityByName(fileName);
        byte[] content = new byte[0];
        if (ureportFileEntity != null) {
            content = ureportFileEntity.getContent();
        }
        return new ByteArrayInputStream(content);
    }

    //删除表报
    public void deleteReport(String fileName) {
        if (fileName != null){
            fileName = getNoCorrectName(fileName);
            ureportFileMapper.deleteReportFileByName(fileName);
        }
    }

    //查询所有表报
    public List<ReportFile> getReportFiles() {
        List<UreportFileEntity> ureportFileEntities = ureportFileMapper.queryReportFileList();
        List<ReportFile> reportFiles = new ArrayList<ReportFile>();
        for (UreportFileEntity ufe : ureportFileEntities){
            reportFiles.add(new ReportFile(ufe.getName(), ufe.getUpdateTime()));
        }
        return reportFiles;
    }

    //保存表报
    public void saveReport(String fileName, String content) {
        fileName = getNoCorrectName(fileName);
        UreportFileEntity ureportFileEntity = ureportFileMapper.queryUreportFileEntityByName(fileName);
        Date date = new Date();
        if (ureportFileEntity == null){
            ureportFileEntity = new UreportFileEntity();
            ureportFileEntity.setContent(content.getBytes());
            ureportFileEntity.setUpdateTime(date);
            ureportFileEntity.setCreateTime(date);
            ureportFileEntity.setName(fileName);
            ureportFileMapper.insertReportFile(ureportFileEntity);
        }else {
            ureportFileEntity.setContent(content.getBytes());
            ureportFileEntity.setUpdateTime(date);
            ureportFileMapper.updateReportFile(ureportFileEntity);
        }

    }


    /**
     * 获取没有前缀的文件名
     * @param name
     * @return
     */
    private String getNoCorrectName(String name){
        if(name.startsWith(prefix)){
            name = name.substring(prefix.length(), name.length());
        }
        return name;

    }


    public String getName() {
        return NAME;
    }

    public boolean disabled() {
        return disable;
    }

    public String getPrefix() {
        return prefix;
    }

}

 

 类似资料: