@ResponseBody
@RequestMapping(value=
"upload_img"
public Picture uploadImage(@RequestParam MultipartFile[] upload_logo) throws IOException{
log.info(
"上传图片"
);
Picture pic =
new
Picture();
List<String> paths =
ArrayList<String>();
String dir = UploadUtil.getFolder();
for
(MultipartFile myfile : upload_logo){
if
(myfile.isEmpty()){
"文件未上传"
);
}
else
{
"文件长度: "
+ myfile.getSize());
"文件类型: "
+ myfile.getContentType());
"文件名称: "
+ myfile.getName());
"文件原名: "
+ myfile.getOriginalFilename());
"========================================"
);
String path = UploadUtil.writeFile(myfile.getOriginalFilename(),dir,myfile.getInputStream());
"文件路径:"
+path);
paths.add(path);
}
}
pic.setPaths(paths);
pic;
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
private static final Logger log = LoggerFactory.getLogger(UploadUtil.class);
private UploadUtil() {
}
private static SimpleDateFormat fullSdf =
SimpleDateFormat(
"yyyyMMddHHmmssSSS"
private static SimpleDateFormat folder =
SimpleDateFormat(
"yyyy"
+ File.separator +
"MM"
"dd"
);
public static String getFolder() {
folder.format(
Date());
}
public static String writeFile(String srcName,String dirName,InputStream input) throws IOException {
log.info(srcName);
String uploadDir = ContextUtil.getSysProp(
"upload_dir"
);
String virtualDir = ContextUtil.getSysProp(
"virtual_dir"
//设置你虚拟目录访问路径
(
null
!= srcName) {
srcName = srcName.substring(srcName.indexOf(
"."
));
}
{
srcName =
".jpg"
;
}
String filename =
""
;
filename = uploadDir + File.separator + dirName + File.separator + fullSdf.format(
Date()) + srcName;
String savePath = filename.replace(uploadDir,monospace!important; min-height:auto!important'>);
savePath = virtualDir + savePath.replace(
""
"/"
);
File file =
File(filename);
(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos =
FileOutputStream(file);
byte[] readBuff =
byte[1024 * 30];
int count = -1;
while
((count = input.read(readBuff,readBuff.length)) != -1) {
fos.write(readBuff,count);
}
fos.flush();
fos.close();
input.close();
savePath;
|