当前位置: 首页 > backend >正文

FluxApi - 使用Spring进行调用Flux接口

好久不见各位,最近我也是已经实习有一段时间了,博客也有一段时间没有更新了,但是最近着手完成了一个图片生成的项目,后端使用的是Springboot,在图片处理使用了最近很火的黑森林研究所的Flux模型,如何使用Java来访问和调用这个接口呢,接下来我就来分享一下。

首先你需要在yml文件里进行比较隐私信息的配置:
 

image:edit:tokens:single-deduct: 100blend-deduct: 50api: https://api.bfl.ai/v1/flux-kontext-maxkey: your_keyblend-api: https://api.bfl.ai/v1/flux-kontext-pro

key是需要你去官网自行注册的,具体注册方法可以参考:
Flux爆火,全网最全面最详细的Flux使用教程!-CSDN博客

接下来就是最重要的接口部分:
 

    /*** 核心方法:调用Flux API处理图片* @param imageData 图片数据(可以是URL或base64字符串)* @param prompt 提示词* @param isBase64 是否为base64格式* @param aspectRatio 宽高比字符串,格式为 "width:height",为null时不传递*/private String processImageWithFluxApi(String imageData, String prompt, boolean isBase64, String aspectRatio) throws Exception {FluxRequestDTO request = new FluxRequestDTO(prompt, imageData);if (aspectRatio != null) {request.setAspectRatio(aspectRatio);}HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);headers.set("x-key", fluxApiKey);HttpEntity<FluxRequestDTO> entity = new HttpEntity<>(request, headers);ResponseEntity<FluxResponseDTO> response = restTemplate.postForEntity(fluxApiUrl, entity, FluxResponseDTO.class);if (response.getStatusCode() != HttpStatus.OK || response.getBody() == null) {throw new RuntimeException("Flux API调用失败");}FluxResponseDTO fluxResponse = response.getBody();String taskId = fluxResponse.getId();String pollingUrl = fluxResponse.getPollingUrl();if (taskId == null || pollingUrl == null) {throw new RuntimeException("无效的Flux API响应");}return pollFluxResult(pollingUrl);}private String pollFluxResult(String pollingUrl) throws Exception {int maxRetries = 60;int retryInterval = 5;for (int attempt = 0; attempt < maxRetries; attempt++) {ResponseEntity<FluxResponseDTO> response = restTemplate.getForEntity(pollingUrl, FluxResponseDTO.class);if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {FluxResponseDTO fluxResponse = response.getBody();String status = fluxResponse.getStatus();if ("Ready".equals(status)) {if (fluxResponse.getResult() != null &&fluxResponse.getResult().getSample() != null) {return fluxResponse.getResult().getSample();}throw new RuntimeException("未获取到处理后的图片URL");} else if ("failed".equals(status)) {throw new RuntimeException("Flux处理任务失败");}}Thread.sleep(retryInterval * 1000);}throw new RuntimeException("处理超时,请稍后重试");}

这段代码主要实现了与Flux API交互处理图片的功能,具体作用如下:

1. 核心方法`processImageWithFluxApi`负责构建请求并调用Flux API: - 根据传入的图片数据(URL或base64)、提示词等参数创建请求对象 - 设置请求头信息,包括内容类型和API密钥 - 发送POST请求到Flux API获取处理任务 - 从响应中提取任务ID和轮询URL,调用轮询方法获取最终结果

2. 辅助方法`pollFluxResult`负责轮询获取处理结果: - 设置最大重试次数(60次)和重试间隔(5秒) - 循环向轮询URL发送GET请求查询任务状态 - 当任务状态为"Ready"时,返回处理后的图片样本 - 当任务状态为"failed"时,抛出处理失败异常 - 超过最大重试次数仍未完成时,抛出超时异常 整体流程实现了一个异步API调用模式:先提交处理任务,然后通过轮询方式等待处理完成并获取结果,适用于处理时间不确定的图片处理场景。

这就是这次分享,希望可以对你们有帮助,以后会提高发的频率的。

http://www.xdnf.cn/news/17723.html

相关文章:

  • 《Foundations and Recent Trends in Multimodal Mobile Agents: A Survey》论文精读笔记
  • 物联网之小白调试网关设备
  • [TG开发]简单的回声机器人
  • hadoop 前端yarn 8088端口查看任务执行情况
  • 人工智能——CNN基础:卷积和池化
  • 利用生成式AI与大语言模型(LLM)革新自动化软件测试 —— 测试工程师必读深度解析
  • 数据分析可视化学习总结(美妆2)
  • 飞算AI:企业智能化转型的新引擎——零代码重塑生产力
  • CSS动态视口单位:彻底解决移动端适配顽疾,告别布局跳动
  • 高可用实战之Nginx + Apache篇
  • Java面试宝典:ZGC
  • PyTorch基础(Numpy与Tensor)
  • Vue3+AntDesign实现带搜索功能的下拉单选组件
  • 如何生成.patch?
  • 2025年AI大模型应用架构设计十大核心问题深度解析
  • Java pdf工具
  • Java 导出word 实现表格内插入图表(柱状图、折线图、饼状图)--可编辑数据
  • 飞算JavaAI的中间件风暴:Redis + Kafka 全链路实战
  • Android 在 2020-2025 都做哪些更新?
  • 浏览器面试题及详细答案 88道(23-33)
  • Pytorch FSDP权重分片保存与合并
  • CW32L011电机开发板控制教程
  • MVCC底层实现原理
  • Java Web开发:Session与Cookie详细入门指南
  • 深入理解 C++ 中的虚函数:原理、特点与使用场景
  • mac下载maven并配置,以及idea配置
  • 智慧城市数字孪生:城市管理的“平行宇宙”
  • nginx匹配规则
  • 计算机网络体系结构
  • framebuffer