AbstractController

public abstract class AbstractController
{
    /** 日志 */
    private static final Logger LOG = LoggerFactory.getLogger(AbstractController.class);

    /** 加密策略(md5) */
    private static final Encry encry = ExtensionLoader.getExtensionLoader(Encry.class).getExtension("md5");

    /** request对象 */
    private HttpServletRequest request;

    /** response对象 */
    private HttpServletResponse response;

    /**
     * 导出类型
     */
    public enum ExportType
    {
        /** 选中的数据 */
        checkedData,
        /** 当前页数据 */
        currPageData,
        /** 所有数据 */
        allData
    }

    /**
     * 获取request
     * @return
     */
    public HttpServletRequest getRequest()
    {
        return request;
    }
    /**
     * 获取response
     * @return
     */
    public HttpServletResponse getResponse()
    {
        return response;
    }
    /**
     * 创建session
     * @return
     */
    public HttpSession getSession()
    {
        return getRequest().getSession(true);
    }
    /**
     * 下载处理, 大文件下载尽量用流
     * @param in
     * @param fileName
     */
    public void download(InputStream in, String fileName)
    {
        OutputStream out = null;
        try
        {
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=" + CommonUtil.encodeDownFileName(fileName, request.getHeader("User-Agent")));
            // 设置 默认编码
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            out = response.getOutputStream();
            byte[] buf = new byte[1024 * 10];
            int len = 0;
            while ((len = in.read(buf)) > 0)
            {
                out.write(buf, 0, len);
            }
            out.flush();
        } catch (Exception e)
        {
            LOG.error(e.getMessage(), e);
        } finally
        {
            StreamUtil.closeStream(in, out);
        }
    }
    /**
     * 下载, 大文件下载慎用
     * @param fileData
     * @param fileName
     */
    public void download(byte[] fileData, String fileName)
    {
        download(new ByteArrayInputStream(fileData), fileName);
    }
    /**
     * 获取当前用户
     * @return
     */
    protected User getCurrentUser()
    {
        return getLoginedService().getCurrentUser();
    }
    /**
     * 登录业务数据操作
     * @return
     */
    protected LoginedService getLoginedService()
    {
        return LoginedFactory.getInstance().getLoginedService();
    }
    /**
     * 设置数据到session
     * @param key
     * @param value
     */
    protected void setSessionAttribute(String key, Object value)
    {
        getLoginedService().setAttribute(key, value);
    }
    /**
     * 获取值
     * @param key
     * @return
     */
    protected Object getSessionAttribute(String key)
    {
        return getLoginedService().getAttribute(key);
    }
    /**
     * 获取操作详细信息
     * @param opType
     * @param detail
     * @return
     */
    protected OpDetail getOpDetail(String opType, String detail)
    {
        return new OpDetail(opType, getCurrentUser() == null ? "" : getCurrentUser().getUsername(), detail);
    }
    /**
     * 获取映射文件流
     * @param mappingPath
     * @return
     * @throws FileNotFoundException
     */
    protected InputStream getMappingFileStream(String mappingPath) throws FileNotFoundException
    {
        // 映射路径
        String basePath = getRequest().getServletContext().getRealPath("/");
        File mappingFile = new File(basePath, mappingPath);
        if (!mappingFile.exists())
        {
            LOG.error("映射路径[" + mappingPath + "]对应的文件信息不存在.");
            return null;
        }
        return new FileInputStream(mappingFile);
    }
    /**
     * 获取文件字节码
     * @param mappingPath
     * @return
     * @throws Exception
     */
    protected byte[] getMappingFileBytes(String mappingPath) throws Exception
    {
        InputStream in = getMappingFileStream(mappingPath);
        return StreamUtil.getStreamBytes(in);
    }
    /**
     * 获取基准路径
     * @return
     */
    protected String getBasePath()
    {
        return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
    }
    /**
     * 判断字符串与当前密码是否相等
     * @param pass
     * @return
     */
    protected boolean isEqPwd(String pass)
    {
        // return true;
        if (CommonUtil.isEmpty(pass))
        {
            return false;
        }
        return getCurrentUser() != null && getCurrentUser().getPasswd().equals(encry.encode(pass));
    }
    /**
     * 注入Request, Response对象
     * @param request
     * @param response
     */
    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    {
        this.request = request;
        this.response = response;
    }

    protected String appendStrList(List<? extends AbstractEntity> entites, String split)
    {
        if (entites == null || entites.size() == 0)
        {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        split  = (split == null ? "," : split);

        for (AbstractEntity entity : entites)
        {
            builder.append(entity.getId()).append(split);
        }
        // 删除最后一个分隔符
        builder.delete(builder.length() - split.length(), builder.length());
        return builder.toString();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值