|
@@ -0,0 +1,161 @@
|
|
|
|
+package cn.com.qmth.mps.controller;
|
|
|
|
+
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+
|
|
|
|
+import com.qmth.boot.tools.io.IOUtils;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.mps.bean.User;
|
|
|
|
+import cn.com.qmth.mps.util.ObjectUtil;
|
|
|
|
+import cn.com.qmth.mps.util.ServletUtil;
|
|
|
|
+
|
|
|
|
+public class BaseController {
|
|
|
|
+
|
|
|
|
+ protected HttpServletRequest currentRequest() {
|
|
|
|
+ return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取接入用户
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ protected User getAccessUser() {
|
|
|
|
+ User user = (User) ServletUtil.getRequest().getAttribute("accessEntity");
|
|
|
|
+ return user;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 参数trim
|
|
|
|
+ *
|
|
|
|
+ * @param bean
|
|
|
|
+ * @param nullIfBlank
|
|
|
|
+ * @author
|
|
|
|
+ */
|
|
|
|
+ protected void trim(Object bean, boolean nullIfBlank) {
|
|
|
|
+ if (null == bean) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Class<? extends Object> clazz = bean.getClass();
|
|
|
|
+ if (clazz.equals(Map.class)) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (clazz.equals(List.class)) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (clazz.equals(Set.class)) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (clazz.isEnum()) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (ObjectUtil.isBaseDataType(clazz)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
|
+ try {
|
|
|
|
+ for (int i = 0; i < fields.length; i++) {
|
|
|
|
+ Field f = fields[i];
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
+ Object value = f.get(bean);
|
|
|
|
+ if (null == value) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (f.getType().equals(String.class)) {
|
|
|
|
+ if (null != value) {
|
|
|
|
+ String s = (String) value;
|
|
|
|
+ s = s.trim();
|
|
|
|
+ if (nullIfBlank && StringUtils.isBlank(s)) {
|
|
|
|
+ s = null;
|
|
|
|
+ }
|
|
|
|
+ f.set(bean, s);
|
|
|
|
+ }
|
|
|
|
+ } else if (f.getType().equals(Map.class)) {
|
|
|
|
+ } else if (f.getType().equals(List.class)) {
|
|
|
|
+ } else if (f.getType().equals(Set.class)) {
|
|
|
|
+ } else if (f.getType().isEnum()) {
|
|
|
|
+ } else if (ObjectUtil.isBaseDataType(f.getType())) {
|
|
|
|
+ } else {
|
|
|
|
+ trim(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 参数trim.<br>
|
|
|
|
+ * set null if blank.
|
|
|
|
+ *
|
|
|
|
+ * @param bean
|
|
|
|
+ * @author
|
|
|
|
+ */
|
|
|
|
+ protected void trim(Object bean) {
|
|
|
|
+ trim(bean, false);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ protected HttpServletResponse getResponse() {
|
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
|
+ .getRequestAttributes();
|
|
|
|
+ HttpServletResponse response = requestAttributes.getResponse();
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected void exportFile(String fileName, InputStream in) {
|
|
|
|
+ OutputStream out = null;
|
|
|
|
+ try {
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ HttpServletResponse response = getResponse();
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
+ out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
+ IOUtils.copy(in, out);
|
|
|
|
+ out.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected void exportFile(String fileName, File file) {
|
|
|
|
+ OutputStream out = null;
|
|
|
|
+ InputStream in = null;
|
|
|
|
+ try {
|
|
|
|
+ in = new FileInputStream(file);
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ HttpServletResponse response = getResponse();
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
|
|
|
+ response.addHeader("Content-Length", "" + file.length());
|
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
+ out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
+ IOUtils.copy(in, out);
|
|
|
|
+ out.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|