springboot RestTemplate

 

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

    }



}

 

上一篇:@LoadBalanced注解


下一篇:【Spring】远程调用HttpClient/RestTemplate