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

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

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

//得到块文件所在目录 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)

   } }


6.3.5.4 上传分块 
在Service 中定义分块上传分块方法:
 

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

//块文件上传 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;

    }

猜你喜欢

转载自blog.csdn.net/czbkzmj/article/details/91794765
今日推荐