SpringBoot将图片/文件传至前端
SpringBoot将图片/文件传至前端@GetMapping("/download")public String download (HttpServletResponse response) {File file = new File("");byte[] bytes = new byte[1024];try {OutputStream os = response.getOutputStrea
·
SpringBoot将图片/文件传至前端
1. 返回OutputStream
@GetMapping("/download")
public String download (HttpServletResponse response) {
File file = new File("");
byte[] bytes = new byte[1024];
try (OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(file)){
while ((fis.read(bytes)) != -1) {
os.write(bytes);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
2. 返回base64格式
@GetMapping("/base64")
public String getBase64() {
byte[] data = null;
String dataStr = null;
final Response picture = feignServer.getBase64();
Response.Body body = picture.body();
try (InputStream is = body.asInputStream()){
data = is.readAllBytes();
} catch (IOException e) {
e.printStackTrace();
}
dataStr = Base64.encodeBase64String(data);
return dataStr;
}
更多推荐
所有评论(0)