在项目中有时会遇到大文件上传,经常会出现链接超时的问题,所以就需要使用文件分片上传的方式来上传大文件。实现原理就是,在前端将文件分成指定大小的“文件块”,分成多次上传,每次上传前先向后台查询已经上传成功的文件的大小,然后从指定的位置切割一块文件,进行上传,后台接收到文件块后追加到指定的文件中。
这里需要后台提供两个接口,一个上传文件的接口,一个查询已上传文件大小的接口。
前端代码:
rush:js;">
package com.fs.controller;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.csource.ClientGlobal;
import org.csource.common.MyException;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.multipartfile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import com.alibaba.fastjson.JSON;
import com.fs.entity.UploadedFileInfo;
import com.fs.util.UploadUtil;
/**
* @author admin
*
* 2016年12月20日下午8:59:38
*/
@Controller
public class FastDFsuploadController {
@Resource
private UploadUtil uploadUtil;
@RequestMapping(value = "v1/uploadSize",method = RequestMethod.POST)
@ResponseBody
public Long uploadSize(HttpServletRequest request) {
String name = request.getParameter("name");
Long size = Long.parseLong(request.getParameter("size"));
try {
JedisShardInfo jedisShardInfo = new JedisShardInfo("192.168.0.12");
Jedis jedis = new Jedis(jedisShardInfo);
String str = jedis.get(name);
UploadedFileInfo uploadedFileInfo = null;
// 如果首次上传,已上传大小为 0
if (StringUtils.isEmpty(str)) {
uploadedFileInfo = new UploadedFileInfo();
jedis.set(name,JSON.toJSONString(uploadedFileInfo));
jedis.close();
return 0L;
}
uploadedFileInfo = JSON.parSEObject(str,UploadedFileInfo.class);
jedis.close();
//已经全部上传
if(size <= uploadedFileInfo.getSize()){
System.out.println("上传完成");
}
//UploadUtil.getFileInfo(uploadedFileInfo.getFileId());
return uploadedFileInfo.getSize();
} catch (Exception e) {
e.printstacktrace();
return 0L;
}
}
@RequestMapping(value = "v1/uploadFile",method = RequestMethod.POST)
@ResponseBody
public int upload(HttpServletRequest request) {
// CommonsMultipartResolver resolver = new
// CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator t = multiRequest.getFileNames();
multipartfile fileDetail = multiRequest.getFile(t.next());
String name = request.getParameter("name");
JedisShardInfo jedisShardInfo = new JedisShardInfo("192.168.0.12");
Jedis jedis = new Jedis(jedisShardInfo);
UploadedFileInfo uploadedFileInfo = JSON.parSEObject(jedis.get(name),UploadedFileInfo.class);
uploadedFileInfo.setFileName(name);
String path = "d:/testUpload";
File file = new File(path);
if (!file.exists() && !file.isDirectory()) {
boolean b = file.mkdirs();
}
RandomAccessFile randomFile = null;
try {
randomFile = new RandomAccessFile(path+"/"+name,"rw");
randomFile.seek(randomFile.length());
randomFile.write(fileDetail.getBytes());
uploadedFileInfo.setLocalPath(path+"/"+name);
uploadedFileInfo.setSize(randomFile.length());
} catch (IOException e) {
e.printstacktrace();
}finally{
try {
randomFile.close();
} catch (IOException e) {
e.printstacktrace();
}
}
// 将上传文件信息保存到redis
jedis.set(name,JSON.toJSONString(uploadedFileInfo));
jedis.close();
return 0;
}
}
以上所述是小编给大家介绍的JS异步文件分片断点上传的实现思路。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。