首页 新闻 赞助 找找看

java断点续传上传文件,突然关闭然后再上传文件为什么不是从之前上传的进度上传

0
[待解决问题]

java断点续传我运行起来之后上传文件,突然关掉浏览器再上传文件为什么不是从之前上传的进度上传

问题补充:

Controller层里这么写的

@Controller
public class UploadAction {

@Autowired
private File_Service FileSerivce;

//返回上传路径
public File File_dir(Plupload plupload,HttpServletRequest request){
String FileDir = "pluploadDir";//文件保存的文件夹
plupload.setRequest(request);//手动传入Plupload对象HttpServletRequest属性

//int userId = ((User)request.getSession().getAttribute("user")).getUserId();

//文件存储绝对路径,会是一个文件夹,项目相应Servlet容器下的"pluploadDir"文件夹,还会以用户唯一id作划分
File dir = new File(request.getSession().getServletContext().getRealPath("/") + FileDir + "/"/*+userId*/);
return dir;
}


/**Plupload文件上传处理方法*/
@RequestMapping(value="upload")
public void upload(Plupload plupload,HttpServletRequest request,HttpServletResponse response,File_file File_file) throws Exception {

int chunks = plupload.getChunks();
int nowChunk = plupload.getChunk();
File dir = File_dir(plupload, request);
if (!dir.exists()) {
dir.mkdirs();//可创建多级目录,而mkdir()只能创建一级目录
}
//开始上传文件
UploadService.upload(plupload, dir);

 

 

Service层里这么写的

@Service

public class UploadService
{
public static void upload(Plupload plupload,File pluploadDir){
String fileName = /*""+System.currentTimeMillis()+*/plupload.getName();
//在服务器内生成唯一文件名
upload(plupload,pluploadDir,fileName);
}
private static void upload(Plupload plupload,File pluploadDir,String fileName){
int chunks = plupload.getChunks();
//用户上传文件被分隔的总块数
int nowChunk = plupload.getChunk();
// 当前块,从0开始
// 这里Request请求类型的强制转换可能出错,配置文件中向SpringIOC容器引入multipartResolver对象即可。
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)plupload.getRequest();
// 调试发现map中只有一个键值对
MultiValueMap<String,MultipartFile> map = multipartHttpServletRequest.getMultiFileMap();
if(map!=null){
try{
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next();
List<MultipartFile> multipartFileList = map.get(key);
for(MultipartFile multipartFile:multipartFileList){
//循环只进行一次
plupload.setMultipartFile(multipartFile);
//手动向Plupload对象传入MultipartFile属性值
File targetFile = new File(pluploadDir+"/"+fileName);
//新建目标文件,只有被流写入时才会真正存在
if(chunks>1){//用户上传资料总块数大于1,要进行合并
File tempFile = new File(pluploadDir.getPath()+"/"+multipartFile.getName());
//第一块直接从头写入,不用从末端写入
savePluploadFile(multipartFile.getInputStream(),tempFile,nowChunk==0?false:true);
if(chunks-nowChunk==1) {
//全部块已经上传完毕,此时targetFile因为有被流写入而存在,要改文件名字
tempFile.renameTo(targetFile);
}
}else{
//只有一块,就直接拷贝文件内容
multipartFile.transferTo(targetFile);
}
}
}
} catch (IOException e){
e.printStackTrace();
}
}
}
private static void savePluploadFile(InputStream inputStream,File tempFile,boolean flag){
OutputStream outputStream = null;
try {
if(flag==false){
//从头写入
outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
} else{
//从末端写入
outputStream = new BufferedOutputStream(new FileOutputStream(tempFile,true));
}
byte[] bytes = new byte[1024];
int len = 0;
while ((len = (inputStream.read(bytes)))>0){
outputStream.write(bytes,0,len);
}
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
try{
outputStream.close();
inputStream.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
}

A_Qiang的主页 A_Qiang | 初学一级 | 园豆:44
提问于:2018-05-09 19:53
< >
分享
所有回答(2)
0

断点的文件点,存储在哪里?

sxusky | 园豆:202 (菜鸟二级) | 2018-05-09 20:18

文件存储在了项目运行时自动生产的target文件里

支持(0) 反对(0) A_Qiang | 园豆:44 (初学一级) | 2018-05-10 08:52
0

缓存被清除了

津城古天乐 | 园豆:202 (菜鸟二级) | 2018-05-10 00:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册