web123456

Vue downloads the file and renames it

/** * Download attachment * @param response * @param fileName */ @GetMapping(value = "/downloadAttachment") public ResponseEntity<?> downloadAttachment(final HttpServletResponse response, @RequestParam("fileName") String fileName) { InputStream inputStream = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { // Get file information ObjectStat objectStat = template.getObjectInfo(bucketName, fileName); // Set response header response.setHeader("content-type", objectStat.contentType()); response.setContentType(objectStat.contentType()); // Get file input stream inputStream = template.getObject(bucketName, fileName); outputStream = this.readInpurStream(inputStream); ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray()); return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s", URLEncoder.encode(fileName+".pdf", "UTF-8"))) .body(resource); } catch (Exception e) { log.error("Export attachment failed:",e); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Exception occurred when downloading the file"); } } /** * Convert input stream to ByteArrayOutputStream * @param input * @return * @throws Exception */ public ByteArrayOutputStream readInpurStream(InputStream input) throws Exception{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; try { while ((len = input.read(buffer)) > -1) { baos.write(buffer, 0, len); } baos.flush(); } catch (IOException e) { throw new Exception("Illegal flow."); } finally { try { input.close(); } catch (IOException e) { log.error("file stream shutdown failed."); } } return baos; }