Java的新项目学成在线笔记-day13(十三)

6.3.5.3 分块检查
在Service 中定义分块检查方法:

[mw_shl_code=applescript,true]//得到块文件所在目录 private String getChunkFileFolderPath(String fileMd5){ String fileChunkFolderPath = getFileFolderPath(fileMd5) +"/" + "chunks" + "/";    
  return fileChunkFolderPath;    
  } //检查块文件 public CheckChunkResult checkchunk(String fileMd5, String chunk, String chunkSize) {    
//得到块文件所在路径   
  String chunkfileFolderPath = getChunkFileFolderPath(fileMd5); 
    //块文件的文件名称以1,2,3..序号命名,没有扩展名   
  File chunkFile = new File(chunkfileFolderPath+chunk);   
  if(chunkFile.exists()){   
      return new CheckChunkResult(MediaCode.CHUNK_FILE_EXIST_CHECK,true);  
   }else{    
     return new CheckChunkResult(MediaCode.CHUNK_FILE_EXIST_CHECK,false);  
   } }
[/mw_shl_code]
6.3.5.4 上传分块 
在Service 中定义分块上传分块方法:

[mw_shl_code=applescript,true]//块文件上传 public ResponseResult uploadchunk(MultipartFile file, String fileMd5, String chunk) {   
  if(file == null){       
  ExceptionCast.cast(MediaCode.UPLOAD_FILE_REGISTER_ISNULL);  
   }     
//创建块文件目录   
  boolean fileFold = createChunkFileFolder(fileMd5);  
   //块文件 
    File chunkfile = new File(getChunkFileFolderPath(fileMd5) + chunk);   
  //上传的块文件 
    InputStream inputStream= null;   
  FileOutputStream outputStream = null;   
  try {  
       inputStream = file.getInputStream();      
   outputStream = new FileOutputStream(chunkfile);  
       IOUtils.copy(inputStream,outputStream); 
    } catch (Exception e) {   
      e.printStackTrace();     
    LOGGER.error("upload chunk file fail:{}",e.getMessage());   
      ExceptionCast.cast(MediaCode.CHUNK_FILE_UPLOAD_FAIL);  
   }finally {    
     try {        
     inputStream.close();  
       } catch (IOException e) {          
   e.printStackTrace();     
    }      
   try {         
    outputStream.close();    
     } catch (IOException e) {        
     e.printStackTrace();     
   }   
    } 
    return new ResponseResult(CommonCode.SUCCESS); }    
//创建块文件目录    
private boolean createChunkFileFolder(String fileMd5){     
    //创建上传文件目录  
       String chunkFileFolderPath = getChunkFileFolderPath(fileMd5);  
       File chunkFileFolder = new File(chunkFileFolderPath);     
    if (!chunkFileFolder.exists()) {     
        //创建文件夹        
     boolean mkdirs = chunkFileFolder.mkdirs();      
       return mkdirs;      
   }       
  return true; 
    }[/mw_shl_code]

转载于:https://blog.51cto.com/13517854/2409031

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值