【easypoi 和 autopoi】 模板读取问题

ps: 不知道谁copy谁…

起因

由于需要模板导出,将模板放在resource下,本地windows可以读取, docker内读取不到.
发现:

autopoi:

public final class POICacheManager {

	private static final Logger LOGGER = LoggerFactory.getLogger(POICacheManager.class);

	private static LoadingCache<String, byte[]> loadingCache;

	static {
		loadingCache = CacheBuilder.newBuilder().expireAfterWrite(7, TimeUnit.DAYS).maximumSize(50).build(new CacheLoader<String, byte[]>() {
			@Override
			public byte[] load(String url) throws Exception {
				return new FileLoade().getFile(url);
			}
		});
	}

	public static InputStream getFile(String id) {
		try {
			// 复杂数据,防止操作原数据
			byte[] result = Arrays.copyOf(loadingCache.get(id), loadingCache.get(id).length);
			return new ByteArrayInputStream(result);
		} catch (ExecutionException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return null;
	}

}
class FileLoade {

	private static final Logger LOGGER = LoggerFactory.getLogger(FileLoade.class);

	public byte[] getFile(String url) {
		FileInputStream fileis = null;
		ByteArrayOutputStream baos = null;
		try {
			// 先用绝对路径查询,再查询相对路径
			try {
				fileis = new FileInputStream(url);
			} catch (FileNotFoundException e) {
				String path = PoiPublicUtil.getWebRootPath(url);
				fileis = new FileInputStream(path);
			}
			baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len;
			while ((len = fileis.read(buffer)) > -1) {
				baos.write(buffer, 0, len);
			}
			baos.flush();
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			LOGGER.error(e.getMessage(), e);
		} catch (IOException e) {
			LOGGER.error(e.getMessage(), e);
		} finally {
			try {
				if (fileis != null)
					fileis.close();
				if (fileis != null)
					baos.close();
			} catch (IOException e) {
				LOGGER.error(e.getMessage(), e);
			}
		}
		LOGGER.error(fileis + "这个路径文件没有找到,请查询");
		return null;
	}

}

解释

上述代码中通过url来拿到file,在通过file拿流, 但是springboot打包的file拿不到,哪怕是通过classloader.

easypoi

public final class POICacheManager {
    private static final Logger LOGGER = LoggerFactory.getLogger(POICacheManager.class);
    private static IFileLoader fileLoader = new FileLoaderImpl();
    private static ThreadLocal<IFileLoader> LOCAL_FILE_LOADER = new ThreadLocal();

    public POICacheManager() {
    }

    public static InputStream getFile(String id) {
        try {
            byte[] result;
            if (LOCAL_FILE_LOADER.get() != null) {
                result = ((IFileLoader)LOCAL_FILE_LOADER.get()).getFile(id);
            }

            result = fileLoader.getFile(id);
            result = Arrays.copyOf(result, result.length);
            return new ByteArrayInputStream(result);
        } catch (Exception var2) {
            LOGGER.error(var2.getMessage(), var2);
            return null;
        }
    }

    public static void setFileLoader(IFileLoader fileLoader) {
        POICacheManager.fileLoader = fileLoader;
    }

    public static void setFileLoaderOnce(IFileLoader fileLoader) {
        if (fileLoader != null) {
            LOCAL_FILE_LOADER.set(fileLoader);
        }

    }
}

【easypoi 和 autopoi】 模板读取问题_第1张图片

public class FileLoaderImpl implements IFileLoader {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileLoaderImpl.class);

    public FileLoaderImpl() {
    }

    public byte[] getFile(String url) {
        InputStream fileis = null;
        ByteArrayOutputStream baos = null;

        try {
            if (url.startsWith("http")) {
                URL urlObj = new URL(url);
                URLConnection urlConnection = urlObj.openConnection();
                urlConnection.setConnectTimeout(30000);
                urlConnection.setReadTimeout(60000);
                urlConnection.setDoInput(true);
                fileis = urlConnection.getInputStream();
            } else {
                try {
                    fileis = new FileInputStream(url);
                } catch (FileNotFoundException var11) {
                    fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url);
                }
            }

            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];

            int len;
            while((len = ((InputStream)fileis).read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }

            baos.flush();
            byte[] var6 = baos.toByteArray();
            return var6;
        } catch (Exception var12) {
            LOGGER.error(var12.getMessage(), var12);
        } finally {
            IOUtils.closeQuietly((Closeable)fileis);
            IOUtils.closeQuietly(baos);
        }

        LOGGER.error(fileis + "这个路径文件没有找到,请查询");
        return null;
    }
}

解释

easyopoi自己实现的获取autopoi一样, 但是easypoi提供了接口,可以自己实现. 这个就比较好.


使用jeecgboot开源框架的小心啦.

如果要使用模板导出的话, 模板需要放在jar包外面,服务器磁盘内.

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