101 lines
3.4 KiB
Java
101 lines
3.4 KiB
Java
package com.easypan.controller.basecontroller;
|
|
|
|
import com.easypan.entity.constants.Constants;
|
|
import com.easypan.entity.dto.SessionShareDto;
|
|
import com.easypan.entity.dto.SessionWebUserDto;
|
|
import com.easypan.entity.enums.ResponseCodeEnum;
|
|
import com.easypan.entity.vo.PaginationResultVO;
|
|
import com.easypan.entity.vo.ResponseVO;
|
|
import com.easypan.utils.CopyTools;
|
|
import com.easypan.utils.StringTools;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
import java.io.*;
|
|
|
|
public class BaseController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
|
|
|
|
protected static final String STATUC_SUCCESS = "success";
|
|
|
|
protected static final String STATUC_ERROR = "error";
|
|
|
|
protected <T> ResponseVO getSuccessResponseVO(T t) {
|
|
ResponseVO<T> responseVO = new ResponseVO<>();
|
|
responseVO.setStatus(STATUC_SUCCESS);
|
|
responseVO.setCode(ResponseCodeEnum.CODE_200.getCode());
|
|
responseVO.setInfo(ResponseCodeEnum.CODE_200.getMsg());
|
|
responseVO.setData(t);
|
|
return responseVO;
|
|
}
|
|
|
|
// 从session中获得SessionWebUserDto对象
|
|
protected SessionWebUserDto getUserInfoFromSession(HttpSession session) {
|
|
SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
|
|
return sessionWebUserDto;
|
|
}
|
|
|
|
protected SessionShareDto getSessionShareFromSession(HttpSession session, String shareId) {
|
|
SessionShareDto sessionShareDto = (SessionShareDto)
|
|
session.getAttribute(Constants.SESSION_SHARE_KEY + shareId);
|
|
return sessionShareDto;
|
|
}
|
|
|
|
|
|
// 输出文件
|
|
protected void readFile(HttpServletResponse response, String filePath) {
|
|
if (!StringTools.pathIsOk(filePath)) {
|
|
return;
|
|
}
|
|
OutputStream out = null;
|
|
FileInputStream in = null;
|
|
try {
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
return;
|
|
}
|
|
in = new FileInputStream(file);
|
|
byte[] byteData = new byte[1024];
|
|
out = response.getOutputStream();
|
|
int len = 0;
|
|
while ((len = in.read(byteData)) != -1) {
|
|
out.write(byteData, 0, len);
|
|
}
|
|
out.flush();
|
|
} catch (Exception e) {
|
|
logger.error("读取文件异常", e);
|
|
} finally {
|
|
if (out != null) {
|
|
try {
|
|
out.close();
|
|
} catch (IOException e) {
|
|
logger.error("IO异常", e);
|
|
}
|
|
}
|
|
if (in != null) {
|
|
try {
|
|
in.close();
|
|
} catch (IOException e) {
|
|
logger.error("IO异常", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
protected <S, T> PaginationResultVO<T> convert2PaginationVO(PaginationResultVO<S> result, Class<T> clazz) {
|
|
PaginationResultVO<T> resultVO = new PaginationResultVO<>();
|
|
resultVO.setList(CopyTools.copyList(result.getList(), clazz));
|
|
resultVO.setPageNo(result.getPageNo());
|
|
resultVO.setPageSize(result.getPageSize());
|
|
resultVO.setPageTotal(result.getPageTotal());
|
|
resultVO.setTotalCount(result.getTotalCount());
|
|
return resultVO;
|
|
}
|
|
}
|