ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot RestTemplate

2021-03-08 09:01:05  阅读:248  来源: 互联网

标签:outputStream springboot RestTemplate springframework org import rosellete com


 

RestTemplate 文件下载

package com.rosellete.iescp.cshop.controller.impl;

import cn.hutool.core.date.DateUtil;
import com.rosellete.iescp.base.exception.BusinessException;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rosellete.iescp.cshop.component.IStudentComponent;
import com.rosellete.iescp.cshop.controller.IStudentController;
import com.rosellete.iescp.cshop.entity.StudentEO;
import com.rosellete.iescp.cshop.repository.IStudentRepository;
import com.rosellete.iescp.cshop.service.IStudentService;
import com.rosellete.iescp.base.controller.impl.BaseControllerImpl;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

@RestController("studentController")
@RequestMapping("/controller/student/manager")
public class StudentControllerImpl
        extends BaseControllerImpl<StudentEO, IStudentRepository, IStudentComponent, IStudentService>
        implements IStudentController {

    @RequestMapping(value = {"/getElecInvoice"}, method = {RequestMethod.GET})
    public void getElecInvoice(HttpServletResponse httpServletResponse, @RequestParam String policyNo) throws Exception {
        RestTemplate restTemplate=new RestTemplate();
        httpServletResponse.setContentType("application/vnd.ms-excel;charset=utf-8");
        httpServletResponse.setHeader("Content-Disposition", String.format("attachment;filename=export_pdf_%s.pdf", DateUtil.format(new Date(), "yyyy-MM-dd HH.mm.ss")));
        try{
            ResponseEntity<byte[]> entity = restTemplate.getForEntity("http://ip:port/download?id=" + policyNo, byte[].class);

            HttpStatus statusCode = entity.getStatusCode();
            System.out.println("请求返回编码为:" + statusCode.toString());
            ServletOutputStream outputStream = httpServletResponse.getOutputStream();
            outputStream.write(entity.getBody());
            //outputStream.flush();
        }catch (HttpMessageNotWritableException e){
            e.printStackTrace();
        }
    }
    //部分参考 https://www.cnblogs.com/code111/p/10065319.html
    @RequestMapping(value = {"/getElecInvoice2"}, method = {RequestMethod.GET})
    public void getElecInvoice2(HttpServletResponse response, @RequestParam String policyNo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", String.format("attachment;filename=export_pdf_%s.pdf", DateUtil.format(new Date(), "yyyy-MM-dd HH.mm.ss")));
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
        ResponseEntity<byte[]> bytes = restTemplate.getForEntity("http://ip:port/download?id=" + policyNo, byte[].class);
        //ResponseEntity<byte[]> bytes = restTemplate.exchange("http://10.15.22.28:7001/itfmgr/servlet/downloadInvoice?id=" + id,  HttpMethod.GET,httpEntity, byte[].class);
        final ServletOutputStream outputStream = response.getOutputStream();
        try {
            outputStream.write(bytes.getBody(),0,bytes.getBody().length);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }



}

 

标签:outputStream,springboot,RestTemplate,springframework,org,import,rosellete,com
来源: https://www.cnblogs.com/whatlonelytear/p/14497863.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有