栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 后台文件合并。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 后台文件合并。

public Object uploadComponent(String appid, Map data) throws Exception {
        String sha = data.get("sha").toString();
	    // 获取存储的分块文件
		String folder = (this.uploadPath + this.software + sha + "_tmp/");
		// 存储整块文件到指定文件夹
		String saveFolder = (this.uploadPath + this.software + sha + "/");
		String fileName = data.get("fileName").toString();
	    File files = new File(folder);
		// 遍历本地文件夹
		File[] list = files.listFiles();
        // 排序很重要
		List fileList = Arrays.asList(list);
		Collections.sort(fileList, new Comparator() {
			@Override
			public int compare(File o1, File o2) {
				if(Integer.parseInt(o1.getName().split("_")[0])>Integer.parseInt(o2.getName().split("_")[0])){
					// 升序
					return 1;
				}
				// 降序
				return -1;
			}
		});
// --------------------------------------方式一↓ ------------------------------ 
        // 保证创建一个新文件
        File file = new File(saveFolder + fileName);
        if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
            file.getParentFile().mkdirs();
        }
		try (	
			BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(saveFolder , data.get("fileName").toString())));){
			int bufSize = 1024;byte[] buffer = new byte[bufSize];
			
			for (int i = 0; i < list.length; i ++) {
				try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(fileList.get(i)));) {
					int readcount;
					while ((readcount = inputStream.read(buffer)) > 0) {
						outputStream.write(buffer, 0, readcount);
					}
				}
			}
		} catch(Exception e) {
			LOG.error("合并分块文件失败, 请重新上传!");
        	throw e;
		}
// --------------------------------------方式一↑ ------------------------------ 
// --------------------------------------方式二↓ ------------------------------ 

//		// 合成分片文件
//		// 将碎片文件和流对象关联,并存储到集合中
//		ArrayList fileInputStreams=new ArrayList();
//		for(int i = 0; i < fileList.size(); i++){
//			try {
//				// 此处不能使用try-with-resource, 此处使用会导致fileInputStreams关闭,进而导至SequenceInputStream 序列流提前关闭。读不到文件
//				fileInputStreams.add(new FileInputStream(fileList.get(i)));
//			} catch (Exception e) {
//				LOG.error("读取合并分块文件失败, 请重新上传!");
//	        	throw new BusinessException(this.messageSource.getExceptionMessage(Messages.E400320007));
//			}
//		}
//		// 将多个流合并成一个序列流
//		// 获取Enumeration对象
//		Enumeration en= Collections.enumeration(fileInputStreams);
//		// 保证创建一个新文件
//        File file = new File(saveFolder + fileName);
//        if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
//            file.getParentFile().mkdirs();
//        }
//		try (
//			// 序列流 【此处的流会提前关闭】
//			SequenceInputStream sis = new SequenceInputStream(en);
//			// 字节输出流
//			FileOutputStream fos = new FileOutputStream(file);) {
//			byte[] buf=new byte[1024];
//			int len = 0;
//			while ((len=sis.read(buf))!=-1) {
//				fos.write(buf, 0, len);
//			}
//		} catch(Exception e) {
//			e.printStackTrace();
//			LOG.error("合并分块文件失败, 请重新上传!");
//        	throw e;
//		}

// --------------------------------------方式二↑ ------------------------------ 
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/874335.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号