常用代码段

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;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值