java断点续传上传_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);//手動(dòng)傳入Plupload對(duì)象HttpServletRequest屬性
//int userId = ((User)request.getSession().getAttribute("user")).getUserId();
//文件存儲(chǔ)絕對(duì)路徑,會(huì)是一個(gè)文件夾,項(xiàng)目相應(yīng)Servlet容器下的"pluploadDir"文件夾,還會(huì)以用戶唯一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();//可創(chuàng)建多級(jí)目錄,而mkdir()只能創(chuàng)建一級(jí)目錄
}
//開始上傳文件
UploadService.upload(plupload, dir);
Service層里這么寫的
@Service
public class UploadService
{
public static void upload(Plupload plupload,File pluploadDir){
String fileName = /*""+System.currentTimeMillis()+*/plupload.getName();
//在服務(wù)器內(nèi)生成唯一文件名
upload(plupload,pluploadDir,fileName);
}
private static void upload(Plupload plupload,File pluploadDir,String fileName){
int chunks = plupload.getChunks();
//用戶上傳文件被分隔的總塊數(shù)
int nowChunk = plupload.getChunk();
// 當(dāng)前塊,從0開始
// 這里Request請(qǐng)求類型的強(qiáng)制轉(zhuǎn)換可能出錯(cuò),配置文件中向SpringIOC容器引入multipartResolver對(duì)象即可。
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)plupload.getRequest();
// 調(diào)試發(fā)現(xiàn)map中只有一個(gè)鍵值對(duì)
MultiValueMap map = multipartHttpServletRequest.getMultiFileMap();
if(map!=null){
try{
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext()){
String key = iterator.next();
List multipartFileList = map.get(key);
for(MultipartFile multipartFile:multipartFileList){
//循環(huán)只進(jìn)行一次
plupload.setMultipartFile(multipartFile);
//手動(dòng)向Plupload對(duì)象傳入MultipartFile屬性值
File targetFile = new File(pluploadDir+"/"+fileName);
//新建目標(biāo)文件,只有被流寫入時(shí)才會(huì)真正存在
if(chunks>1){//用戶上傳資料總塊數(shù)大于1,要進(jìn)行合并
File tempFile = new File(pluploadDir.getPath()+"/"+multipartFile.getName());
//第一塊直接從頭寫入,不用從末端寫入
savePluploadFile(multipartFile.getInputStream(),tempFile,nowChunk==0?false:true);
if(chunks-nowChunk==1) {
//全部塊已經(jīng)上傳完畢,此時(shí)targetFile因?yàn)橛斜涣鲗懭攵嬖?#xff0c;要改文件名字
tempFile.renameTo(targetFile);
}
}else{
//只有一塊,就直接拷貝文件內(nèi)容
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();
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java断点续传上传_java断点续传上传文件,突然关闭然后再上传文件为什么不是从之前上传的进度上传...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java打印系统参数_Java学习(二十
- 下一篇: java tic tac toe_请问我