• 图片可通过base64流的形式发送到前端,但需要前端处理,用http的content_type的图片类型用ResponseEntity返回可直接展示
  • controll中代码简略如下(此接口用postman请求也可展示图片)

@GetMapping(value = "/getFtpImageById/{id}")
public ResponseEntity<Object> getFtpImageById(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {logger.info("----------ftp图片,ftpId:{}", id);//通过id去数据库中查询出相关图片的信息,如具体地址,content_type等,这些信息在上传图片时可保存到数据库中xxxPo po = xxxxxService.getById(id);HttpHeaders headers = new HttpHeaders();//从数据库中查出当前图片对应的content_type,content_type在保存时可通过MultipartFile.getContentType()获得String content_type = po.getContentType; //"image/jpeg" 类似这种//设置请求头headers.add(HttpHeaders.CONTENT_TYPE, content_type);//获取图片byte数组  po.getFilePath是上传时保存的具体地址byte[] fileBytes = ftpUtils.ftpReadImage(po.getFilePath);return new ResponseEntity<>(fileBytes, headers, HttpStatus.OK);}

1.通过id去数据库中查询出相关图片的信息,如具体地址,content_type等,这些信息在上传图片时可保存到数据库中,从数据库中查出当前图片对应的content_type,content_type在保存时可通过MultipartFile.getContentType()获得;

2.然后把请求头的content_type设置为相应的类型;

3.从保存图片地址读取图片byte数组放到ResponseEntity返回即可;

  •  FTP工具类和yaml文件删减版如下:
ftp:port: 21hostname: xxx.xxx.xxx.xxxusername: xxxpassword: xxxbasePath: xxx/remotepath: /xxx/xxx/xxx

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.io.*;/*** 通过FTP上传文件*/
@Component
public class FileFtpUtils {@Value("${ftp.hostname}")private String hostName;public String getRemotePath() {return this.remotePath;}/*** ftp端口*/@Value("${ftp.port}")private Integer port;/*** ftp用户名*/@Value("${ftp.username}")private String userName;/*** ftp密码*/@Value("${ftp.password}")private String password;/*** ftp远程路径*/@Value("${ftp.remotepath}")private String remotePath;/*** 测试是否能连接** @param ftpClient* @param hostname  ip或域名地址* @param port      端口* @param username  用户名* @param password  密码* @return 返回真则能连接*/public static boolean connect(FTPClient ftpClient, String hostname, int port, String username, String password) {boolean flag = false;try {//ftp初始化的一些参数ftpClient.connect(hostname, port);ftpClient.enterLocalPassiveMode();ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);ftpClient.setControlEncoding("UTF-8");if (ftpClient.login(username, password)) {log.info("连接ftp成功");flag = true;} else {log.error("连接ftp失败,可能用户名或密码错误");try {disconnect(ftpClient);} catch (Exception e) {e.printStackTrace();}}} catch (IOException e) {log.error("连接失败,可能ip或端口错误");e.printStackTrace();}return flag;}/*** 断开连接** @param ftpClient* @throws Exception*/public static void disconnect(FTPClient ftpClient) {if (ftpClient.isConnected()) {try {ftpClient.disconnect();log.info("已关闭连接");} catch (IOException e) {log.error("没有关闭连接");e.printStackTrace();}}}/*** 读取ftp文件流* @param filePath* @return*/public byte[] ftpReadImage(String filePath){byte[] fileByte = null;InputStream inputStream = null;ByteArrayOutputStream outputStream = new ByteArrayOutputStream();FTPClient ftpClient = new FTPClient();//1 测试连接if (connect(ftpClient, hostName, port, userName, password)) {try {ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);inputStream = ftpClient.retrieveFileStream(new String(filePath.getBytes("UTF-8"), "iso-8859-1"));int readBytes;byte[] bytes = new byte[1024 * 1024];while ((readBytes = inputStream.read(bytes)) != -1) {outputStream.write(bytes, 0, readBytes);}disconnect(ftpClient);} catch (IOException e) {e.printStackTrace();}finally {if(outputStream != null){try {outputStream.flush();fileByte = outputStream.toByteArray();outputStream.close();} catch (IOException e) {e.printStackTrace();}}if (inputStream != null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}disconnect(ftpClient);}}return fileByte;}}

1.读取图片要返回byte[]数组;

  • 有些简略,不过大体思路很清晰,上传图片到ftp时候,要先保存一份详细信息到数据库中,如ftpId,fileName,filePath,fileSuffix,contentType等等,当保存成功要返回前端时候,可在后端自己拼接获取图片地址如http://xxxxx:8080/xx/getFtpImageById/{id} ,前端在<img :src=""> 标签中会自动发送获取图片请求(如需携带token等信息,可放到cookie中),这样可进行反显图片

springboot从ftp读取图片且根据不同的content_type在前端直接展示相关推荐

  1. spring-boot引用资源:图片、json文件、模板目录(jar包运行依旧有效)

    spring-boot引用资源:图片.json文件.模板目录 前端时间,在idea上运行项目OK,但在打成jar包后运行却出了岔子.网上一番搜索,终于得到了解决:使用流,使用类路径 干货 图片,jso ...

  2. 使用Java输入流(InputStream)读取FTP服务器图片,并上传到另一台FTP服务器

    使用JavaInputStream读取FTP图片到远程服务器 最近需要做一个新需求,要读取ftp服务器的图片,然后保存到另一台服务器上,ftp的访问路径是经过apache转换的,记录一下遇到的坑.我的 ...

  3. 基于springboot架构的读取excel 图片并自动上传

    基于springboot架构的读取excel 图片并自动上传 excel 图片上传 页面准备 comment.html 逻辑处理准备 控制类CommentController.java 接口类ICom ...

  4. springboot整合poi读取数据库数据和图片动态导出excel

    springboot整合poi读取数据库数据和图片动态导出excel 第一次操作 话不多说就直接上代码 实现代码 需要的依赖 <dependency><groupId>org. ...

  5. java base64上传图片|接口读取图片,springboot配置映射读取资源

    1.上传图片 public static String uploadImg(String baseImg,String basePath,String fileSavePath,HttpServlet ...

  6. mysql存读图片_mysql如何储存读取图片

    mysql储存读取图片的方法:首先将图片转换成缓冲流:然后获得图片的字节数组并执行相关操作:最后通过"public void MapSearchQuery(out byte[] imageB ...

  7. spring-boot整合FTP文件服务器

    @TOC一 一,扫盲篇:什么是FTP? FTP 服务器是支持 FTP协议的服务器.在电脑中安装FTP工具将电脑中的数据传输到服 务器当中,这时服务器就称为FTP服务器,而我们的电脑称为客户端.对于FT ...

  8. ttf,woff2字体转化为png图片,python读取图片

    20210326 乱码转换的时候 是同一套unicode编码 但是在不同的字体库中对应的字不同 20210324 https://jingyan.baidu.com/article/e73e26c0c ...

  9. OpenCV+python:读取图片

    1,源码: import cv2 as cv #导入OpenCV库 import numpy as np #导入numpy科学计算包 print("--------- Python + Op ...

最新文章

  1. 【星榜单】盘点那些坑爹的国产手机们
  2. 双核处理(动态规划)
  3. 使用异步 I/O 大大提高应用程序的性能
  4. Unix高级环境编程 学习小结(一)
  5. 百度机器翻译已经进化到什么程度?
  6. T4 Template Overview
  7. postgresql9.4.4中文手册笔记-9.10 支持枚举函数
  8. linux命令修改内容怎么回退,linux命令(修改).doc
  9. call stack and stack buffer overflow
  10. java做橡皮擦效果_HTML5 canvas橡皮擦擦拭效果
  11. Kotlin Native新增Objective-C互操作能力以及对WebAssembly的支持
  12. Java加密与解密的艺术~SM4实现
  13. linux入门 适合初学者_【推荐】适合初学者临摹的国画|国画基础入门教学视频教程!...
  14. 谜题40:不情愿的构造器
  15. android 拼音搜索
  16. 【数字图像处理】实验三 图像分割(MATLAB实现)
  17. 邮件发送JS脚本传播敲诈者木马的分析报告
  18. 搜索引擎学习之旅3 - 搜索引擎工作流程
  19. 腾讯云服务器安全组配置
  20. KMP算法的时间复杂度

热门文章

  1. 四种请求方式 get、post、put、delete 的用法及区别(全)
  2. LinuxMint安装后的简单配置
  3. POI-----POI操作Excel-7、打印区域
  4. GNSS星历数据读取
  5. PAT-Head of Hangs
  6. Visual Studio 2019 Compiler Hangs
  7. 3分钟快速阅读-《Effective Java》(三)
  8. 288.软件开发过程与软件测试
  9. 如何选择适合自己数据的统计检验方法
  10. 计算机机原理是什么意思,计算机工作原理及与工控机的区别