BaseController.java 3.3 KB
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);
	}
}