From bd495830063ea6b1e984529726cbbc81c44c3915 Mon Sep 17 00:00:00 2001 From: panpanxu Date: Sat, 20 May 2017 10:30:22 +0800 Subject: [PATCH] Update FileUploadController.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 分开处理状态请求和文件上传,否则会状态请求报错 MultipartException: The current request is not a multipart request --- .../wu/controller/FileUploadController.java | 134 +++++++++--------- 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/serverJAVAEE/src/main/java/me/kazaff/wu/controller/FileUploadController.java b/serverJAVAEE/src/main/java/me/kazaff/wu/controller/FileUploadController.java index e07ab5b..879b931 100644 --- a/serverJAVAEE/src/main/java/me/kazaff/wu/controller/FileUploadController.java +++ b/serverJAVAEE/src/main/java/me/kazaff/wu/controller/FileUploadController.java @@ -37,67 +37,73 @@ public String printWelcome(ModelMap model) { } //大文件上传 - @RequestMapping(value = "fileUpload", method = RequestMethod.POST) - @ResponseBody - public String fileUpload(String status, FileInfo info, @RequestParam(value = "file", required = false) MultipartFile file){ - - if(status == null){ //文件上传 - if(file != null && !file.isEmpty()){ //验证请求不会包含数据上传,所以避免NullPoint这里要检查一下file变量是否为null - try { - File target = wu.getReadySpace(info, this.uploadFolder); //为上传的文件准备好对应的位置 - if(target == null){ - return "{\"status\": 0, \"message\": \"" + wu.getErrorMsg() + "\"}"; - } - - file.transferTo(target); //保存上传文件 - - //将MD5签名和合并后的文件path存入持久层,注意这里这个需求导致需要修改webuploader.js源码3170行 - //因为原始webuploader.js不支持为formData设置函数类型参数,这将导致不能在控件初始化后修改该参数 - if(info.getChunks() <= 0){ - if(!wu.saveMd52FileMap(info.getMd5(), target.getName())){ - log.error("文件[" + info.getMd5() + "=>" + target.getName() + "]保存关系到持久成失败,但并不影响文件上传,只会导致日后该文件可能被重复上传而已"); - } - } - - return "{\"status\": 1, \"path\": \"" + target.getName() + "\"}"; - - }catch(IOException ex){ - log.error("数据上传失败", ex); - return "{\"status\": 0, \"message\": \"数据上传失败\"}"; - } - } - }else{ - if(status.equals("md5Check")){ //秒传验证 - - String path = wu.md5Check(info.getMd5()); - - if(path == null){ - return "{\"ifExist\": 0}"; - }else{ - return "{\"ifExist\": 1, \"path\": \"" + path + "\"}"; - } - - }else if(status.equals("chunkCheck")){ //分块验证 - - //检查目标分片是否存在且完整 - if(wu.chunkCheck(this.uploadFolder + "/" + info.getName() + "/" + info.getChunkIndex(), Long.valueOf(info.getSize()))){ - return "{\"ifExist\": 1}"; - }else{ - return "{\"ifExist\": 0}"; - } - - }else if(status.equals("chunksMerge")){ //分块合并 - - String path = wu.chunksMerge(info.getName(), info.getExt(), info.getChunks(), info.getMd5(), this.uploadFolder); - if(path == null){ - return "{\"status\": 0, \"message\": \"" + wu.getErrorMsg() + "\"}"; - } - - return "{\"status\": 1, \"path\": \"" + path + "\", \"message\": \"中文测试\"}"; - } - } - - log.error("请求参数不完整"); - return "{\"status\": 0, \"message\": \"请求参数不完整\"}"; - } -} \ No newline at end of file + @RequestMapping(value = "fileUpload", method = RequestMethod.POST, headers = ("content-type=multipart/*")) + @ResponseBody + public String fileUpload(FileInfo info, @RequestParam(value = "file", required = false) MultipartFile file) { + + if (file != null && !file.isEmpty()) { //验证请求不会包含数据上传,所以避免NullPoint这里要检查一下file变量是否为null + try { + File target = wu.getReadySpace(info, this.uploadFolder); //为上传的文件准备好对应的位置 + if (target == null) { + return "{\"status\": 0, \"message\": \"" + wu.getErrorMsg() + "\"}"; + } + + file.transferTo(target); //保存上传文件 + + //将MD5签名和合并后的文件path存入持久层,注意这里这个需求导致需要修改webuploader.js源码3170行 + //因为原始webuploader.js不支持为formData设置函数类型参数,这将导致不能在控件初始化后修改该参数 + if (info.getChunks() <= 0) { + if (!wu.saveMd52FileMap(info.getMd5(), target.getName())) { + log.error("文件[" + info.getMd5() + "=>" + target.getName() + "]保存关系到持久成失败,但并不影响文件上传,只会导致日后该文件可能被重复上传而已"); + } + } + + return "{\"status\": 1, \"path\": \"" + target.getName() + "\"}"; + + } catch (IOException ex) { + log.error("数据上传失败", ex); + return "{\"status\": 0, \"message\": \"数据上传失败\"}"; + } + } + + log.error("请求参数不完整"); + return "{\"status\": 0, \"message\": \"请求参数不完整\"}"; + } + + @RequestMapping(value = "fileUpload", method = RequestMethod.POST, headers = ("content-type=application/*")) + @ResponseBody + public String fileUpload(String status, FileInfo info) { + + if (status.equals("md5Check")) { //秒传验证 + + String path = wu.md5Check(info.getMd5()); + + if (path == null) { + return "{\"ifExist\": 0}"; + } else { + return "{\"ifExist\": 1, \"path\": \"" + path + "\"}"; + } + + } else if (status.equals("chunkCheck")) { //分块验证 + + //检查目标分片是否存在且完整 + if (wu.chunkCheck(this.uploadFolder + "/" + info.getName() + "/" + info.getChunkIndex(), Long.valueOf(info.getSize()))) { + return "{\"ifExist\": 1}"; + } else { + return "{\"ifExist\": 0}"; + } + + } else if (status.equals("chunksMerge")) { //分块合并 + + String path = wu.chunksMerge(info.getName(), info.getExt(), info.getChunks(), info.getMd5(), this.uploadFolder); + if (path == null) { + return "{\"status\": 0, \"message\": \"" + wu.getErrorMsg() + "\"}"; + } + + return "{\"status\": 1, \"path\": \"" + path + "\", \"message\": \"分块合并完成\"}"; + } + + log.error("请求参数不完整"); + return "{\"status\": 0, \"message\": \"请求参数不完整\"}"; + } +}