ftp读取图片并转Base64

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public String download(String ftpUrl,String sfzh){
       FTPClient ftpClient = new FTPClient();
       InputStream inputStream = null;
       String re=null;
       try {
           ftpClient.connect(ftp_ip,ftp_port);
           ftpClient.login(ftp_username, ftp_password);
           ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
           //是否成功登录服务器
           int reply = ftpClient.getReplyCode();
           if (!FTPReply.isPositiveCompletion(reply)) {
               ftpClient.disconnect();
           }
           //跳转到指定目录
           ftpClient.changeWorkingDirectory(ftpUrl);
           //5.遍历下载的目录
           FTPFile[] fs = ftpClient.listFiles();
           for (FTPFile ff : fs){
               //解决中文乱码问题,两次解码
               byte[] bytes=ff.getName().getBytes("iso-8859-1");
               String fileName=new String(bytes,"utf-8");
               if(sfzh.equals(fileName)){
                   inputStream = ftpClient.retrieveFileStream(fileName);
               }
           }
 
           if (inputStream != null) {
               byte[] data=null;
               data=new byte[inputStream.available()];
               BASE64Encoder encoder=new BASE64Encoder();
               re=encoder.encode(data);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }finally{
           if(ftpClient.isConnected()){
               try{
                   ftpClient.disconnect();
               }catch(IOException e){
                   e.printStackTrace();
               }
           }
           if(null != inputStream){
               try {
                   inputStream.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
 
       return re;
   }

  

posted @   沉默的老牛  阅读(2642)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示