spring boot打包成jar以后,怎么读取resources里的文件夹?

spring boot确实很方便,也会产生一些新的问题
比如,怎么读取jar里的某个文件夹里的所有文件
读取以后,可以生成url,返回给前台页面,用户根据url下载文件

其实,项目里写完这个工具类,才发现org.apache.commons commons-vfs2早就有各种问价的读取了。大家也可以利用vfs2来实现。

要读取的文件目录
要读取的文件目录

public class FileHelper {
	
	public static void main(String[] args) {
		String path = "";
		int current = 1;
		int size = 10;
		String fileName = ".xsl";
		//String fileName = "报告"
		Page<JarEntry> page = new Page<JarEntry>(current, size);
		try {
			FileHelper.getJarFilesByFolderPath(path, page, fileName);
			List<JarEntry> fileList = page.getRecords();
			for (JarEntry jarEntry : fileList) {
				
				//文件路径
				String entryUrl = jarEntry.getName();
				
				//文件名
				String entryName = entryUrl.substring(entryUrl.indexOf(path) + 5);//加5,是yjbg + /
				System.out.println("文件名 " + entryName);
				InputStream is = FileHelper.getJarEntryInputStream(path, entryUrl);
				System.out.println("这里的流,可以直接用HttpServletResponse返回给客户端下载 " + is);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取jar里某个文件的文件流
	 * @param path
	 * @param entryName
	 * @return
	 * @throws IOException
	 */
	public static InputStream getJarEntryInputStream(String path, String entryName) throws IOException {
		JarFile jarFile = getJar(path);
		ZipEntry ze = jarFile.getEntry(entryName);
		return jarFile.getInputStream(ze);
	}
	
	/**
	 * 获取jar里的某个文件
	 * @param path
	 * @param entryUrl
	 * @return
	 * @throws IOException
	 */
	public static ZipEntry getJarEntry(String path, String entryUrl) throws IOException {
		JarFile jarFile = getJar(path);
		return jarFile.getEntry(entryUrl);
	}
	
	/**
	 * 读取文件夹的文件
	 * @param path 路径(这里是的值是"yjbg")
	 * @param page 是分页封装类(含有当前页current、每页数量size、总条数total、当前页记录records)
	 * @param fileName 要匹配的文件,所包含的关键字(为空时,返回path下的所有文件)
	 * @return
	 * @throws IOException
	 */
	public static Page<JarEntry> getJarFilesByFolderPath(String path, Page<JarEntry> page, String fileName) 
				throws IOException {
		List<JarEntry> list = new ArrayList<JarEntry>();
		JarFile jarFile = getJar(path);
		Enumeration<JarEntry> jarEntrys = jarFile.entries(); 
		while (jarEntrys.hasMoreElements()) { 
			JarEntry entry = jarEntrys.nextElement(); 
			if (entry.isDirectory()) {
				continue;
			}
			String name = entry.getName();
			boolean isNameNull = fileName == null || fileName.trim().length() == 0;
			if (name.contains(path) && (isNameNull || (!isNameNull && name.contains(fileName)))) {
				list.add(entry);
			}
		}
		orderJarEntry(list, true);
		subListByPage(list, page);
		return page;
	}
	
	/**
	 * 获取jar文件
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static JarFile getJar(String path) throws IOException {
		URL url = FileHelper.class.getClassLoader().getResource(path); 
		String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2); 
		URL jarURL = new URL(jarPath); 
		JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection(); 
		JarFile jarFile = jarCon.getJarFile();
		return jarFile;
	}
	
	/**
	 * 根据最后的修改时间排序
	 * @param files
	 * @param isDesc
	 * @return
	 */
	public static List<JarEntry> orderJarEntry(List<JarEntry> files, boolean isDesc) {
		files.sort((JarEntry h1, JarEntry h2) -> {
			Long h1M = h1.getLastModifiedTime().toMillis();
			Long h2M = h2.getLastModifiedTime().toMillis();
			if (isDesc) {
				return h2M.compareTo(h1M);
			} else {
				return h1M.compareTo(h2M);
			}
		});
		return files;
	}
	
	/**
	 * 分页
	 * @param list
	 * @param page
	 * @return
	 */
	public static <T> Page<T> subListByPage(List<T> list, Page<T> page) {
		List<T> records = new ArrayList<T>();
		int total = 0;
		if (list.size() > 0) {
			total = list.size();
			int current = page.getCurrent();
			int size = page.getSize();
			int fromIndex = (current - 1) * size;
			int toIndex = current * size;
			toIndex = (toIndex <= total) ? toIndex: total;
			records = list.subList(fromIndex, toIndex);
		}
		page.setTotal(total);
		page.setRecords(records);
		return page;
	}
	
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值