自定义下载视图的实现

视图内容:

package com.immo.supervise.view;

import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.AbstractView;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.immo.framework.utils.HttpClientUtil;
import com.immo.framework.utils.IdUtils;
import com.immo.supervise.common.WxInfo;
import com.immo.supervise.entity.SuperviseQrcodeInfo;
import com.immo.supervise.mapper.SuperviseQrcodeInfoMapper;

/**
 * 带参数微信二维码下载
 * 
 * @author Linhai.Tan 2018-1-12
 *
 */
public class WxParamQRcodeView extends AbstractView {

	@Autowired
	private SuperviseQrcodeInfoMapper superviseQrcodeInfoMapper;

	/**
	 * 生成带参数二维码过程
	 */
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String qrUrl = null;
		Map<String, Object> params = new HashMap<>();
		Map<String, Object> actionInfo = new HashMap<>();
		Map<String, Object> sceneId = new HashMap<>();
		
		// 查询数据库是否有此条件的二维码
		String orgId = request.getParameter("orgId");
		String userId = request.getParameter("userId");
		String patientId = request.getParameter("patientId");
		EntityWrapper<SuperviseQrcodeInfo> wrapper=new EntityWrapper<>();
		if(StringUtils.isBlank(orgId)){
			wrapper.isNull("org_id");
		}else{
			wrapper.eq("org_id",orgId);
		}
		if(StringUtils.isBlank(userId)){
			wrapper.isNull("user_id");
		}else{
			wrapper.eq("user_id",userId);
		}
		if(StringUtils.isBlank(patientId)){
			wrapper.isNull("patient_id");
		}else{
			wrapper.eq("patient_id",patientId);
		}
		
		List<SuperviseQrcodeInfo> list =superviseQrcodeInfoMapper.selectList(wrapper);

		// 设置浏览器响应
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String("QRcode.png".getBytes(), "ISO-8859-1"));

		if (list != null && list.size() == 1) {
			qrUrl = WxInfo.getQRUrlByTicket(list.get(0).getTicket());
		} else {
			// 获取目前最大的序号
			Integer sequence = superviseQrcodeInfoMapper.selectMaxOrder() + 1;
			sceneId.put("scene_id", sequence);
			actionInfo.put("scene", sceneId);
			params.put("action_name", "QR_LIMIT_SCENE");
			params.put("action_info", actionInfo);
			String post = HttpClientUtil.JsonPost(WxInfo.getCreateQRcodeUrl(), params);
			LoggerFactory.getLogger(this.getClass()).error(post);
			Map map = JSONObject.parseObject(post, Map.class);
			qrUrl = WxInfo.getQRUrlByTicket((String) map.get("ticket"));

			// 保存此记录到数据库
			SuperviseQrcodeInfo info = new SuperviseQrcodeInfo();
			info.setSequence(sequence);
			info.setTicket((String) map.get("ticket"));
			info.setOrgId(orgId);
			info.setUserId(userId);
			info.setPatientId(patientId);
			info.setId(IdUtils.getInstanse().getUID());
			superviseQrcodeInfoMapper.insert(info);
		}

		URL url = new URL(qrUrl);
		InputStream in = url.openStream();
		ServletOutputStream out = response.getOutputStream();

		// 3. 文件流----> response输出流
		int len = -1;
		byte[] b = new byte[1024];
		while ((len = in.read(b)) != -1) {
			out.write(b, 0, len);
		}
		in.close();
	}

}

备注为视图:

package com.immo.supervise.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.immo.supervise.view.QRcodeView;
import com.immo.supervise.view.WxParamQRcodeView;

/**
 * 自定义视图配置
 * 
 * @author Linhai.Tan 2018-1-5
 *
 */
@Configuration
public class ViewConfig {

	/**
	 * 下载微信带参数二维码
	 * 
	 * @return
	 */
	@Bean(name = "wxParamQRcode")
	public WxParamQRcodeView wxParamQRcode() {
		return new WxParamQRcodeView();
	}
}

访问视图:

	/**
	 * 生成微信二维码接口视图,注意,这个是自定义视图
	 * 
	 * @return
	 */
	@GetMapping("/wxParamQRcode")
	public ModelAndView createQRCode() {
		return new ModelAndView("wxParamQRcode");
	}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值