从FTP下载图片返回文件流在页面显示图片

.HTML

<div id="showPicture" style="width:100%;height:800px;line-height:100px;overflow:auto;overflow-x:hidden; text-align: center;">
 </div>

.JS

$("#showPicture").append("<img src='/SREHR/web/common/getFtpImage?filepath="+filepath+"' width='900px' height='675px' title=''/>");

.Controller
@RequestMapping("getFtpImage")
@ResponseBody
public  void getFtpImage (String workId,HttpServletResponse response,@RequestParam String filepath) throws InterruptedException {  
        //创建FTP连接
        FTPClient ftp = null;
        OutputStream outStream  = null;
        InputStream in = null;
        try {
            ftp= initFTP(ftp);
            //下载文件
            in = ftp.retrieveFileStream(filepath);
            String picType = filepath.split("\\.")[1];
            BufferedImage bufImg = null;
            bufImg = ImageIO.read(in);
            outStream = response.getOutputStream();
            ImageIO.write(bufImg,  picType, outStream);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in!=null) {
                try {
                    in.close();
                    destroy(ftp);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }        
    }  

    //FTP初始化
    public  FTPClient initFTP(FTPClient ftp) throws IOException {
        
        InputStream in =  UploadToFtpController.class.getResourceAsStream("/application.properties");
        Properties properties=new Properties();
        properties.load(in);
        String addr = properties.getProperty("ftp_url");
        int port=Integer.valueOf(properties.getProperty("ftp_port")).intValue();
        String username=properties.getProperty("ftp_username");
        String password=properties.getProperty("ftp_password");
        ftp = new FTPClient();
        ftp.connect(addr,port);
        ftp.login(username,password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftp.setControlEncoding("GBK");
        ftp.setBufferSize(1024*1024*10); //设置缓冲区上传速度为10M,默认为1K
        ftp.setFileType(FTP.BINARY_FILE_TYPE);//设置上传方式位字节      
        ftp.enterLocalPassiveMode();//Switch to passive mode
        return ftp;
    }
    //关闭FTP
    public void destroy(FTPClient ftp) throws IOException {
        if(ftp != null){
            ftp.disconnect();
            ftp = null;
        }
    }

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值