大文件分片上传java后台合并文件并进行ocr识别

	@Override
	public RespEntity uploadFileByMappedByteBuffer(MultipartFile file, int chunkNumber, int currentChunkSize,
			int totalChunks, int chunkSize, CarCenter carCenter, String type, String carNum, Integer uploads, 
			String uploadFilePathSub, String serverUrl, String uploadsType) throws IOException {
   
		// TODO Auto-generated method stub
		  RespEntity respEntity = new RespEntity();
	      String url = null;
	      String fileName = file.getOriginalFilename();
	      String tempDirPath = "D:/home/upload/carImg";
//	      String tempDirPath = uploadFilePathSub + 	"carImg";
	      //总页数大于1, 证明是已分片
	      if(totalChunks > 1) {
   
	          File newFile = new File(tempDirPath + fileName);
	          if (newFile.exists())
	        	  newFile.delete(); 
	          String tempFileName = fileName + "_tmp";
	          File tmpDir = new File(tempDirPath);
	          File tmpFile = new File(tempDirPath, tempFileName);
	          if (!tmpDir.exists())
	            tmpDir.mkdirs(); 
	          RandomAccessFile accessTmpFile = new RandomAccessFile(tmpFile, "rw");
	          long offset = chunkSize * (chunkNumber - 1);
	          accessTmpFile.seek(offset);
	          accessTmpFile.write(file.getBytes());
	          accessTmpFile.close();
	          boolean isOk = checkAndSetUploadProgress(file, tempDirPath, chunkNumber, currentChunkSize, totalChunks);
	          System.out.println("上传=" + isOk);
	          if (isOk) {
   
	        	  boolean flag = renameFile(tmpFile, fileName);
	        	  System.out.println("upload complete !!" + flag + " name=" + fileName);
	          } 
	          if(chunkNumber == totalChunks) {
   
	        	  respEntity = readCarInfo(new File(tempDirPath, fileName), carCenter, type, carNum, uploads, tempDirPath, serverUrl, uploadsType);
	        	  //删除临时文件
	              tmpDir.delete();
	              tmpFile.delete();
	          }
	      }else {
   
	    	  //获取UUID
	          String uuid = UUID.randomUUID().toString().replaceAll("-", "");
	    	  String newFileName = "";
	    	  if(fileName.indexOf(".") != -1) {
   
	        	  newFileName = fileName.substring(0, fileName.lastIndexOf("."));
	              String extendsFileName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
	              newFileName = newFileName + uuid + extendsFileName;
	          }else {
   
		        	newFileName += "微信图片_" + fileName + uuid + ".jpg";
		      }
	    	  
	    	  //新增文件file
	          File newFile = new File(tempDirPath, newFileName); 
	          //生成图片文件
	          FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
	          respEntity = readCarInfo(newFile, carCenter, type, carNum, uploads, tempDirPath, serverUrl, uploadsType);
	      }
	      
	      return respEntity;
	}
	
	private boolean checkAndSetUploadProgress(MultipartFile file, String tempDirPath, int chunkNumber, int currentChunkSize, int totalChunks) {
   
	    try {
   
	        String fileName = file.getOriginalFilename();
	        File confFile = new File(tempDirPath, fileName + ".conf");
	        RandomAccessFile accessConfFile = new RandomAccessFile(confFile, "rw");
	        System.out.println("set part " + chunkNumber + " complete");
	        accessConfFile.setLength(totalChunks);
	        System.out.println("总分片数="+ totalChunks);
	        accessConfFile.seek((chunkNumber - 1));
	        System.out.println("ChunkNumber()=" + chunkNumber);
	        accessConfFile.write(127);	
	        byte[] completeList = FileUtils.readFileToByteArray(confFile);
	        System.out.println("数组长="+ completeList.length)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值