1、文件下载
@RequestMapping("/download")
public String downLoad(HttpServletResponse response) {
String filename = "2.xlsx";
String filePath = "D:/download";
File file = new File(filePath + "/" + filename);
if (file.exists()) { //判断文件父目录是否存在
response.setContentType("application/pdf;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename, "UTF-8"));
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;
OutputStream os = null; //输出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download---" + filename);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
2、拦截器和配置(自定义注解)
1、自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Signed {
}
2、注册拦截器
@Configuration
public class ParamVerifyInterceptorConfig implements WebMvcConfigurer {
@Autowired
private CheckParamInterceptor checkSourceInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(checkSourceInterceptor).addPathPatterns("/sign/getSignCertificateInfo");
}
}
3、拦截器
@Slf4j
@Component
public class CheckParamInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (!(handler instanceof HandlerMethod)) {
log.warn("unSupport handler...");
throw new IllegalArgumentException("Interceptor only supports HandlerMethod handler");
}
// 拿到请求参数里面的参数
String unionCreditNo = request.getParameter("uniscid");
String certificateNo = request.getParameter("cerno");
// 使用Base64工具类对加密后的参数进行解密
if (Objects.nonNull(unionCreditNo)) {
unionCreditNo = new String(Base64EnCoderDecoderUtils.decryptBASE64(unionCreditNo.getBytes()), StandardCharsets.UTF_8);
}
if (Objects.nonNull(certificateNo)) {
certificateNo = new String(Base64EnCoderDecoderUtils.decryptBASE64(certificateNo.getBytes()), StandardCharsets.UTF_8);
}
// 拿到该方法上的注解对象
Signed signed = getCheckSignedAnnotationObj((HandlerMethod) handler);
if (Objects.nonNull(signed)) {
if (StringUtils.isEmpty(unionCreditNo) && StringUtils.isEmpty(certificateNo)) {
log.warn("request param is illegal, uniscid:{},cerno:{}", unionCreditNo, certificateNo);
throw new BusinessException(ErrorCode.getParamError());
}
}
return true;
}
/**
* 拿到该方法上的Signed注解对象
*/
private Signed getCheckSignedAnnotationObj(HandlerMethod handlerMethod) {
if (handlerMethod.getBeanType().isAnnotationPresent(Signed.class)) {
return handlerMethod.getBeanType().getAnnotation(Signed.class);
} else if (handlerMethod.getMethod().isAnnotationPresent(Signed.class)) {
return handlerMethod.getMethod().getAnnotation(Signed.class);
}
return null;
}
}