1. 程式人生 > >spring boot 輸出流檔案下載

spring boot 輸出流檔案下載

public void downloadProductInterface(@Valid DownloadProductInterfaceDTO interfaceDTO,
@SuppressWarnings("unused") BindingResult bindingResult, HttpServletResponse response) throws IOException {


    log.debug("name is {}, library is {}", interfaceDTO.getName(), interfaceDTO.getLibrary());
@SuppressWarnings
("unchecked") Map<String, Object> map = BeanToMapUtils.toMap(interfaceDTO); ServiceReturn<DataProduct> serviceReturn = dataProductService.find(map); ByteArrayOutputStream tmp = new ByteArrayOutputStream(); tmp.write(serviceReturn.getBody().getData().getBytes()); response.setHeader("Content-Disposition"
, "attachment; filename=" + serviceReturn.getBody().getLibrary() + "_" + serviceReturn.getBody().getName() + ".json"); Integer contentLength = tmp.size(); response.setHeader("content-length", contentLength + ""); response.getOutputStream().write(tmp.toByteArray()); }