Open cmglaze opened 1 year ago
EXAMPLE RESPONSE: To convert a Spring to a file and download it in Spring Boot, you can follow these steps:
@Autowired private ResourceLoader resourceLoader;
Resource resource = resourceLoader.getResource("classpath:path/to/file"); InputStream inputStream = resource.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); }
response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=file.ext");
OutputStream outputStream = response.getOutputStream(); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.flush(); outputStream.close();
EXAMPLE RESPONSE: To convert a Spring to a file and download it in Spring Boot, you can follow these steps:
@Autowired private ResourceLoader resourceLoader;
Resource resource = resourceLoader.getResource("classpath:path/to/file"); InputStream inputStream = resource.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); }
response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=file.ext");
OutputStream outputStream = response.getOutputStream(); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.flush(); outputStream.close();