springboot项目,后端集成上传upload文件(zip并解压到指定路径中)

springboot项目,后端集成上传zip文件,并解压到指定的文件中

上传

	@PostMapping(value = "/upload")
	public JsonResponse upload(@RequestParam(value="id",required=true) String id, @RequestParam(required = false) MultipartFile file) {
		try {
			System.out.println("upload start = " + System.currentTimeMillis());
			String videoUrl = uploadFile(file,id);
			System.out.println("upload end = " + System.currentTimeMillis());
			return okJson(videoUrl);
		} catch (Exception e) {
			e.printStackTrace();
			return errorJson("文件上传失败!");
		}
	}
	
	@Value("${jeecg.path.uploadFile}") //获取配置文件上传路径
	private String uploadFileURL; 
	
	public String uploadFile(MultipartFile file,String id) throws Exception {
		Integer batch = 1; //文件版本号
		ThreeDimensionRoom room = roomService.selectBatchById(id);
		if (!org.springframework.util.StringUtils.isEmpty(room)) {
			batch+=room.getBatch();
		}
		roomService.updateBatchById(id, batch);
		String shortPath =  file.getOriginalFilename();
		File dest = new File(uploadFileURL+"//upFile//"+id+"//"+batch, shortPath);
		if (!dest.getParentFile().exists()) {
			boolean rel = dest.getParentFile().mkdirs();
			if (!rel) {
				throw new JeecgBootException("文件夹创建失败");
			}
		}
		InputStream is = file.getInputStream();
		OutputStream os = new FileOutputStream(dest);
		try {
			byte[] buffer = new byte[8 * 1024];
			int bytesRead;
			while ((bytesRead = is.read(buffer)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
		} catch (Exception e) {
			return errorJson("上传失败!");
		} finally {
			is.close();
		 	os.close();
		}
		return shortPath;
	}

解压

public JsonResponse unZipFiles(@RequestParam(value="id",required=true) String id) {
		try {
			Integer batch = 1;
			ThreeDimensionRoom room = roomService.selectBatchById(id);
			if (!org.springframework.util.StringUtils.isEmpty(room)) {
				batch=room.getBatch();
			}
			File zipFile = new File(uploadFileURL+"//upFile//"+id+"//"+batch +"//machineRoom.zip");
			String path = uploadFileURL+"//unzipFile//"+id+"//"+batch +"//"; //解压到指定的文件夹目录
			unZipFiles(zipFile, path);
			return okJson();
		} catch (Exception e) {
			e.printStackTrace();
			return errorJson("找不到对应的文件,解压失败!");
		}
	}

	//解压文件到指定目录
    @SuppressWarnings("rawtypes")
    public static void unZipFiles(File zipFile,String descDir) throws IOException {
        File pathFile = new File(descDir);
        if(!pathFile.exists()) {
            pathFile.mkdirs();
        }
        //解决zip文件中有中文目录或者中文文件
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
        for(Enumeration entries = zip.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");;
            //判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if(!file.exists()) {
                file.mkdirs();
            }
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(new File(outPath).isDirectory()) {
                continue;
            }
            //输出文件路径信息
//            System.out.println(outPath);
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0) {
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
        }
        zip.close();
        System.out.println("******************解压完毕********************");
    }
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1.代码原理 该程序逐个读取帧图片,并对帧图片逐个进行多行人检测、多目标追踪。该方法是在线方法,将逐个读取帧图片改为逐帧读取视频即可实现在线处理视频。 1.1 多行人检测。 使用gluoncv的预训练模型faster_rcnn_fpn_bn_resnet50_v1b_coco实现多行人检测,这一步骤见detect.py。 1.2 多目标追踪。 使用sort算法实现多目标追踪,详见https://github.com/abewley/sort。 2.代码部署 2.1 配置环境。 安装python==3.6,安装requirements.txt要求的库(代码运行实际用到的库可能少于该文件,因此建议根据代码安装所需要的库)。 2.2 准备数据。 有两种方法准备数据: 2.2.1 将A-data文件夹放入当前目录,A-data文件为Track1 Track2等子文件夹,每个子文件存有.jpg帧图片。 2.2.2 修改run.py的第97行,将input_folder改为A-data文件夹所在路径。 2.3 运行程序run.py。 2.4 程序输出。 程序运行时会打印处理进度及估计的剩余时间。 程序运行完成后,会在当前目录下生成output文件夹,文件存有Track1 Track2等数据集对应的检测结果,.avi文件用于观察检测追踪效果,.txt文件是用于提交的文本文件。 3.调参 3.1 多目标检测模型的选择。 修改detect.py第10行(YOLO.__init__)即可,可选模型及其名称、效果详见gluoncv官网 3.2 sort算法参数的修改。 run.py第34行,参数含义见sort.py。 3.3 将sort改为deepsort。 详见https://github.com/nwojke/deep_sort。 TODO:经尝试,经deep_sort处理后的检测框位置有变形、偏移现象,待解决。 3.4 输入输出路径见run.__main__

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值