Created
June 21, 2018 03:59
-
-
Save donghaic/a925fbe3b9ca99d67d1a709d564ad8cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.van; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.PostMapping; | |
| import org.springframework.web.bind.annotation.RequestParam; | |
| import org.springframework.web.bind.annotation.ResponseBody; | |
| import org.springframework.web.multipart.MultipartFile; | |
| import java.io.*; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| @Controller | |
| public class Uploader { | |
| @GetMapping("/") | |
| public String page() { | |
| return "upload"; | |
| } | |
| /*@PostMapping("upload") | |
| @ResponseBody | |
| public Boolean upload(MultipartFile file) { | |
| File savefile = new File("E:",file.getOriginalFilename()); | |
| try { | |
| file.transferTo(savefile);//将文件保存 | |
| } catch (IOException e) { | |
| return false; | |
| } | |
| return true; | |
| }*/ | |
| /** | |
| * @author van | |
| * 检查文件存在与否 | |
| */ | |
| @PostMapping("checkFile") | |
| @ResponseBody | |
| public Boolean checkFile(@RequestParam(value = "md5File") String md5File) throws IOException { | |
| Boolean exist = false; | |
| String path = "D:/tmp/up/%s"; | |
| Path file = Paths.get(String.format(path, md5File)); | |
| if (file.toFile().exists()) { | |
| System.out.println(" exist " + md5File); | |
| exist = true; | |
| } | |
| return false; | |
| //实际项目中,这个md5File唯一值,应该保存到数据库或者缓存中,通过判断唯一值存不存在,来判断文件存不存在,这里我就不演示 | |
| } | |
| /** | |
| * @author van | |
| * 检查分片存不存在 | |
| */ | |
| @PostMapping("checkChunk") | |
| @ResponseBody | |
| public Boolean checkChunk(@RequestParam(value = "md5File") String md5File, | |
| @RequestParam(value = "chunk") Integer chunk) { | |
| Boolean exist = false; | |
| String path = "D:/tmp/up/" + md5File + "/";//分片存放目录 | |
| String chunkName = chunk + ".tmp";//分片名 | |
| File file = new File(path + chunkName); | |
| if (file.exists()) { | |
| exist = true; | |
| } | |
| return exist; | |
| } | |
| /** | |
| * @author van | |
| * 修改上传 | |
| */ | |
| @PostMapping("upload") | |
| @ResponseBody | |
| public Boolean upload(@RequestParam(value = "file") MultipartFile file, | |
| @RequestParam(value = "md5File") String md5File, | |
| @RequestParam(value = "chunk", required = false) Integer chunk) { //第几片,从0开始 | |
| String path = "D:/tmp/up/" + md5File + "/"; | |
| File dirfile = new File(path); | |
| if (!dirfile.exists()) {//目录不存在,创建目录 | |
| dirfile.mkdirs(); | |
| } | |
| String chunkName; | |
| if (chunk == null) {//表示是小文件,还没有一片 | |
| chunkName = "0.tmp"; | |
| } else { | |
| chunkName = chunk + ".tmp"; | |
| } | |
| String filePath = path + chunkName; | |
| File savefile = new File(filePath); | |
| try { | |
| if (!savefile.exists()) { | |
| savefile.createNewFile();//文件不存在,则创建 | |
| } | |
| file.transferTo(savefile);//将文件保存 | |
| } catch (IOException e) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| /** | |
| * @author van | |
| * 合成分片 | |
| */ | |
| @PostMapping("merge") | |
| @ResponseBody | |
| public Boolean merge(@RequestParam(value = "chunks", required = false) Integer chunks, | |
| @RequestParam(value = "md5File") String md5File, | |
| @RequestParam(value = "name") String name) throws Exception { | |
| String path = "D:/tmp/up"; | |
| FileOutputStream fileOutputStream = new FileOutputStream(path + "/" + name); //合成后的文件 | |
| try { | |
| byte[] buf = new byte[1024]; | |
| for (long i = 0; i < chunks; i++) { | |
| String chunkFile = i + ".tmp"; | |
| File file = new File(path + "/" + md5File + "/" + chunkFile); | |
| InputStream inputStream = new FileInputStream(file); | |
| int len = 0; | |
| while ((len = inputStream.read(buf)) != -1) { | |
| fileOutputStream.write(buf, 0, len); | |
| } | |
| inputStream.close(); | |
| } | |
| //删除md5目录,及临时文件,这里代码省略 | |
| } catch (Exception e) { | |
| return false; | |
| } finally { | |
| fileOutputStream.close(); | |
| } | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment