UploadAction.java
2.92 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.cjs.cms.action.site;
import java.io.File;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.cjs.cms.biz.site.UploadBiz;
import com.cjs.cms.util.file.FileUtil;
import com.cjs.cms.util.lang.StringUtil;
/**
* 上传
*
* @author tongxiaochuan
*
*/
@Controller
@RequestMapping("/upload")
public class UploadAction {
@Autowired
private UploadBiz uploadBiz;
Logger log = LogManager.getLogger();
/**
* CKEditor图片上传
*
* @param upload
* @param req
* @param res
*/
@RequestMapping("/imagePreview")
public void imagePreview(@RequestParam MultipartFile upload, HttpServletRequest req,
HttpServletResponse res) {
String fileName = upload.getOriginalFilename();
// 图片不允许为空
if (StringUtil.isBlank(fileName)) {
return;
}
fileName = StringUtil.getUUID() + FileUtil.getSuffix(fileName);
try {
Calendar date = Calendar.getInstance();
String imageFile = "";
if (date.get(Calendar.MONTH) + 1 < 10) {
imageFile = uploadBiz.uploadPath + date.get(Calendar.YEAR) + 0
+ (date.get(Calendar.MONTH) + 1);
} else {
imageFile = uploadBiz.uploadPath + date.get(Calendar.YEAR)
+ (date.get(Calendar.MONTH) + 1);
}
File file = new File(imageFile);
if (!file.exists() && file.isDirectory()) {
file.mkdir();
}
String url = imageFile + "/" + fileName;
String host = "";
if (date.get(Calendar.MONTH) + 1 < 10) {
host = uploadBiz.imageUrl + "img/" + date.get(Calendar.YEAR) + 0
+ (date.get(Calendar.MONTH) + 1) + "/" + fileName;
} else {
host = uploadBiz.imageUrl + "img/" + date.get(Calendar.YEAR)
+ (date.get(Calendar.MONTH) + 1) + "/" + fileName;
}
String callback = req.getParameter("CKEditorFuncNum");
res.getWriter().println("<script type=\"text/javascript\">");
res.getWriter().println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'"
+ host + "',''" + ")");
res.getWriter().println("</script>");
FileUtil.transferFile(upload, url);
} catch (Exception e) {
log.error("", e);
}
}
}