forked from wemakebug/FileUpload.Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUploadController.java
More file actions
67 lines (53 loc) · 2.53 KB
/
ImageUploadController.java
File metadata and controls
67 lines (53 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.zhangzhihao.FileUpload.Java.Controller;
import com.zhangzhihao.FileUpload.Java.Model.File;
import com.zhangzhihao.FileUpload.Java.Service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.UUID;
import static com.zhangzhihao.FileUpload.Java.Utils.CreateMd5.createMd5;
import static com.zhangzhihao.FileUpload.Java.Utils.DeepCopy.deepClone;
import static com.zhangzhihao.FileUpload.Java.Utils.IsImag.isImage;
import static com.zhangzhihao.FileUpload.Java.Utils.SaveFile.getRealPath;
import static com.zhangzhihao.FileUpload.Java.Utils.SaveFile.saveFile;
@Controller
@RequestMapping("/ImageUpload")
public class ImageUploadController {
@Autowired
private FileService fileService;
@RequestMapping(value = "/Index", method = RequestMethod.GET)
public String Upload() {
return "ImageUpload/Upload";
}
@ResponseBody
@RequestMapping(value = "/ImageUp", method = RequestMethod.POST)
public String fileUpload(@RequestParam("id") String id,
@RequestParam("name") String name,
@RequestParam("type") String type,
@RequestParam("lastModifiedDate") String lastModifiedDate,
@RequestParam("size") int size,
@RequestParam("file") MultipartFile file) {
String fileName = "";
MultipartFile saveFile = null;
try {
saveFile = (MultipartFile) deepClone(file);
java.io.File tempFile = new java.io.File(UUID.randomUUID().toString());
file.transferTo(tempFile);
if (!isImage(tempFile))
return "{\"error\":true}";
String realpath = getRealPath();
String ext = name.substring(name.lastIndexOf("."));
fileName = UUID.randomUUID().toString() + ext;
saveFile(realpath, fileName, saveFile);
fileService.save(new File(fileName, createMd5(file).toString(), new Date()));
} catch (Exception ex) {
return "{\"error\":true}";
}
return "{jsonrpc = \"2.0\",id = id,filePath = \"/Upload/\" + fileFullName}";
}
}