BaseController.java
3.3 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.teplot.common;
import java.io.File;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
/**
* Depiction: 控制器基类
* <p>
* Modify:
* <p>
* Author: Kevin Lynn
* <p>
* Create Date:2016年9月8日 上午9:47:40
*
*/
public class BaseController extends Controller {
public final static int CODE_SUCCESS = 200;// 操作成功
public final static int CODE_FAILURE = 400;// 操作失败
public final static int CODE_SIGN_ERROR = 401;// 签名错误
public final static int CODE_TOKEN_EXPIRE = 406;// 登录过期
public final static int VERIFY_CODE_ERROR = 418;// 验证码错误
public final static String MSG_SUCCESS = "操作成功";
public final static String MSG_FAIL = "操作失败";
public final static String MSG_SIGN_ERROR = "签名错误";
public final static String MSG_TOKEN_EXPIRE = "登录过期";
public final static String MSG_VERIFY_CODE_ERROR = "验证码错误";
/**
* 处理单个的文件上传
*
* @param formName
* 表单名称
* @param size
* 文件大小限制,单位:字节
* @param error
* 错误信息
* @param destDir
* 目标文件夹
* @param fileName
* 目标文件名
* @return
*/
protected Response upload(String formName, int size, String error, String destDir, String fileName) {
File destDirectory = new File(destDir);
destDirectory.mkdirs();
Response ret = new Response(BaseController.CODE_SUCCESS);
UploadFile upload = null;
try {
upload = getFile(formName, "", size);
} catch (Exception e) {
System.out.println("upload-->" + e.toString());
ret.setCode(CODE_FAILURE);
ret.setMsg(error);
addUEditorResponse(ret, false, "");
renderJson(ret.toJson());
return ret;
}
if (upload == null) {
System.out.println("upload is null");
ret.setCode(CODE_FAILURE);
ret.setMsg("请选择文件");
addKindEditorResponse(ret, false, "");
addUEditorResponse(ret, false, "");
renderJson(ret.toJson());
return ret;
}
File tempF = upload.getFile();
// if (tempF != null && tempF.exists() && ImageUtil.isImage(tempF.getName())) {
// ImageUtil.scaleImage(tempF.getPath(), tempF.getPath(), 1080);
// }
String tempName = tempF.getName();
int beginIndex = tempName.lastIndexOf(".");
String format = tempF.getName().substring(beginIndex, tempName.length());
String destPath = destDir + "/" + fileName + format;
tempF.renameTo(new File(destPath));
ret.setData(destPath.replace(Utils.getBaseUploadDir(), ""));
addUEditorResponse(ret, true, ret.getStr("data"));
addKindEditorResponse(ret, true, ret.getStr("data"));
return ret;
}
// 为了兼容UMEditor编辑器的文件上传
private void addUEditorResponse(Response ret, boolean isSuccess, String fileUrl) {
ret.set("state", isSuccess ? "SUCCESS" : "FAILURE");
ret.set("name", fileUrl);
ret.set("originalName", fileUrl);
ret.set("size", "1024");
ret.set("type", "image/png");
ret.set("url", fileUrl);
}
// 为了兼容KindEditor编辑器的文件上传
private void addKindEditorResponse(Response ret, boolean isSuccess, String fileUrl) {
ret.set("error", isSuccess ? 0 : -1);
ret.set("message", isSuccess ? "上传成功" : "上传失败");
ret.set("url", Utils.host() + "/upload" + fileUrl);
}
}