对于较大文件实现分片上传

思路:通过    获取上传的文件资源。调用Windows的分片对象File.prototype.slice。通过指定每次分片的大小,计算出分片个数。然后采用递归的方式一直上传每个分片至后台。直到上传分片数大于了总分片数时,跳出递归。具体代码如下:

前端:

js代码:

代码截图:



后台代码:

@RequestMapping("breakPointUploadFileDo")

public AjaxResponse breakPointUploadFileDo(@RequestParam MultipartFile file,long startLength, String fileName) {

AjaxResponse ajaxResponse =new AjaxResponse(true,"200");

System.out.println(file +" ** " + startLength);

if (file.getSize() <=0) {

return new AjaxResponse(false,"500");

}

int len =0;

final byte[] arr =new byte[1024];

final String writeFileName ="D:/test-" + fileName;

try (RandomAccessFile writeFile =new RandomAccessFile(writeFileName,"rw")) {

writeFile.seek(startLength);

final InputStream iStream = file.getInputStream();

while ((len = iStream.read(arr)) != -1) {

writeFile.write(arr,0, len);

}

}catch (final Exception e) {

e.printStackTrace();

}

return ajaxResponse;

}

你可能感兴趣的:(对于较大文件实现分片上传)