依星源码资源网,依星资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

【好消息,好消息,好消息】VIP会员可以发表文章赚积分啦 !
查看: 5|回复: 0

springboot对redis存取二进制数据

[复制链接] 主动推送

2万

主题

2万

帖子

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
23052
发表于 1 小时前 | 显示全部楼层 |阅读模式
springboot对redis存取二进制数据
可以用 RedisTemplate 配置成“值为 byte[] 的模板”,用 ByteArrayRedisSerializer 确保按原始二进制读写(不做字符编码/对象序列化)。
配置
  1. package com.example.config;

  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.ByteArrayRedisSerializer;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;

  8. @Configuration
  9. public class RedisBinaryConfig {

  10.     @Bean
  11.     public RedisTemplate<String, byte[]> binaryRedisTemplate(RedisConnectionFactory cf) {
  12.         RedisTemplate<String, byte[]> t = new RedisTemplate<>();
  13.         t.setConnectionFactory(cf);
  14.         t.setKeySerializer(StringRedisSerializer.UTF_8);
  15.         t.setValueSerializer(ByteArrayRedisSerializer.INSTANCE);
  16.         t.setHashKeySerializer(StringRedisSerializer.UTF_8);
  17.         t.setHashValueSerializer(ByteArrayRedisSerializer.INSTANCE);
  18.         t.afterPropertiesSet();
  19.         return t;
  20.     }
  21. }
复制代码

使用

  1. import org.springframework.data.redis.core.RedisTemplate;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import java.time.Duration;

  4. public class BinService {
  5.     @Autowired
  6.     private RedisTemplate<String, byte[]> binaryRedisTemplate;

  7.     public void save(String key, byte[] data, Duration ttl) {
  8.         binaryRedisTemplate.opsForValue().set(key, data, ttl);
  9.     }

  10.     public byte[] get(String key) {
  11.         return binaryRedisTemplate.opsForValue().get(key);
  12.     }

  13.     public void saveHash(String hkey, String field, byte[] data) {
  14.         binaryRedisTemplate.opsForHash().put(hkey, field, data);
  15.     }

  16.     public byte[] getHash(String hkey, String field) {
  17.         Object v = binaryRedisTemplate.opsForHash().get(hkey, field);
  18.         return v instanceof byte[] ? (byte[]) v : null;
  19.     }
  20. }
复制代码


要点
  • 不要用 StringRedisTemplate 保存二进制,否则会按 UTF-8 编码导致损坏。
  • Redis 单个 value 最大 512MB;超大文件建议分片或改用对象存储。
  • 若直接用底层连接也可:connection.set(key.getBytes(StandardCharsets.UTF_8), bytes)。

下面给出一个最小可用的服务端与客户端示例,演示通过 HTTP 存/取 Redis 中的二进制数据。
服务端(Spring Boot)
  • PUT /bin/{key}:请求体即二进制;可选 ttl 秒
  • GET /bin/{key}:返回 application/octet-stream 的二进制
  1. package com.example.web;

  2. import org.springframework.http.*;
  3. import org.springframework.web.bind.annotation.*;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.redis.core.RedisTemplate;

  6. import java.time.Duration;
  7. import java.util.Optional;

  8. @RestController
  9. @RequestMapping("/bin")
  10. public class BinaryController {

  11.     @Autowired
  12.     private RedisTemplate<String, byte[]> binaryRedisTemplate;

  13.     @PutMapping("/{key}")
  14.     public ResponseEntity<Void> put(@PathVariable String key,
  15.                                     @RequestBody byte[] body,
  16.                                     @RequestParam(name = "ttl", required = false) Optional<Long> ttlSec) {
  17.         if (ttlSec.isPresent()) {
  18.             binaryRedisTemplate.opsForValue().set(key, body, Duration.ofSeconds(ttlSec.get()));
  19.         } else {
  20.             binaryRedisTemplate.opsForValue().set(key, body);
  21.         }
  22.         return ResponseEntity.accepted().build();
  23.     }

  24.     @GetMapping("/{key}")
  25.     public ResponseEntity<byte[]> get(@PathVariable String key) {
  26.         byte[] data = binaryRedisTemplate.opsForValue().get(key);
  27.         if (data == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
  28.         HttpHeaders h = new HttpHeaders();
  29.         h.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  30.         h.setContentLength(data.length);
  31.         // 可选下载文件名:
  32.         // h.setContentDisposition(ContentDisposition.attachment().filename(key + ".bin").build());
  33.         return new ResponseEntity<>(data, h, HttpStatus.OK);
  34.     }
  35. }
复制代码
客户端调用示例
  • curl
  1. # 上传二进制,设置 1 小时过期
  2. curl -X PUT --data-binary @image.png "http://localhost:8080/bin/pic1?ttl=3600"

  3. # 下载二进制到文件
  4. curl -o out.png "http://localhost:8080/bin/pic1"
复制代码
  • Java(JDK 11+)
  1. import java.net.http.*;
  2. import java.net.URI;
  3. import java.nio.file.*;

  4. public class ClientDemo {
  5.     public static void main(String[] args) throws Exception {
  6.         HttpClient http = HttpClient.newHttpClient();

  7.         // PUT 上传
  8.         byte[] bytes = Files.readAllBytes(Path.of("image.png"));
  9.         HttpRequest put = HttpRequest.newBuilder(URI.create("http://localhost:8080/bin/pic1?ttl=3600"))
  10.                 .PUT(HttpRequest.BodyPublishers.ofByteArray(bytes))
  11.                 .header("Content-Type", "application/octet-stream")
  12.                 .build();
  13.         http.send(put, HttpResponse.BodyHandlers.discarding());

  14.         // GET 下载
  15.         HttpRequest get = HttpRequest.newBuilder(URI.create("http://localhost:8080/bin/pic1")).GET().build();
  16.         HttpResponse<byte[]> resp = http.send(get, HttpResponse.BodyHandlers.ofByteArray());
  17.         if (resp.statusCode() == 200) Files.write(Path.of("download.png"), resp.body());
  18.     }
  19. }
复制代码
说明
  • 需配合你前面配置的 RedisTemplate<String, byte[]>(ByteArrayRedisSerializer)使用。
  • value 最大 512MB;大对象建议分片或对象存储。


下面给出一个可在 Java 8 (1.8.0_152) 运行的 Spring Boot 2.7 示例:用 RedisTemplate<String, byte[]> 读写二进制,并通过 HTTP 接口让客户端上传/下载。
pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <groupId>com.example</groupId>
  5.   <artifactId>redis-binary-demo</artifactId>
  6.   <version>1.0.0</version>
  7.   <parent>
  8.     <groupId>org.springframework.boot</groupId>
  9.     <artifactId>spring-boot-starter-parent</artifactId>
  10.     <version>2.7.18</version>
  11.   </parent>
  12.   <properties>
  13.     <java.version>1.8</java.version>
  14.   </properties>
  15.   <dependencies>
  16.     <dependency>
  17.       <groupId>org.springframework.boot</groupId>
  18.       <artifactId>spring-boot-starter-web</artifactId>
  19.     </dependency>
  20.     <dependency>
  21.       <groupId>org.springframework.boot</groupId>
  22.       <artifactId>spring-boot-starter-data-redis</artifactId>
  23.     </dependency>
  24.     <dependency>
  25.       <groupId>org.apache.commons</groupId>
  26.       <artifactId>commons-pool2</artifactId>
  27.     </dependency>
  28.   </dependencies>
  29.   <build>
  30.     <plugins>
  31.       <plugin>
  32.         <groupId>org.springframework.boot</groupId>
  33.         <artifactId>spring-boot-maven-plugin</artifactId>
  34.       </plugin>
  35.     </plugins>
  36.   </build>
  37. </project>
复制代码
application.yml
  1. spring:
  2.   redis:
  3.     host: 127.0.0.1
  4.     port: 6379
  5. server:
  6.   port: 8080
复制代码
配置 RedisTemplate(值为 byte[])
  1. package com.example.config;

  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.ByteArrayRedisSerializer;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;

  8. @Configuration
  9. public class RedisBinaryConfig {
  10.     @Bean
  11.     public RedisTemplate<String, byte[]> binaryRedisTemplate(RedisConnectionFactory cf) {
  12.         RedisTemplate<String, byte[]> t = new RedisTemplate<>();
  13.         t.setConnectionFactory(cf);
  14.         t.setKeySerializer(StringRedisSerializer.UTF_8);
  15.         t.setValueSerializer(ByteArrayRedisSerializer.INSTANCE);
  16.         t.setHashKeySerializer(StringRedisSerializer.UTF_8);
  17.         t.setHashValueSerializer(ByteArrayRedisSerializer.INSTANCE);
  18.         t.afterPropertiesSet();
  19.         return t;
  20.     }
  21. }
复制代码
Controller:上传/下载二进制
  1. package com.example.web;

  2. import org.springframework.http.*;
  3. import org.springframework.web.bind.annotation.*;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.redis.core.RedisTemplate;

  6. import java.time.Duration;

  7. @RestController
  8. @RequestMapping("/bin")
  9. public class BinaryController {

  10.     @Autowired
  11.     private RedisTemplate<String, byte[]> binaryRedisTemplate;

  12.     @PutMapping("/{key}")
  13.     public ResponseEntity<Void> put(@PathVariable String key,
  14.                                     @RequestBody byte[] body,
  15.                                     @RequestParam(name = "ttl", required = false) Long ttlSec) {
  16.         if (ttlSec != null && ttlSec > 0) {
  17.             binaryRedisTemplate.opsForValue().set(key, body, Duration.ofSeconds(ttlSec));
  18.         } else {
  19.             binaryRedisTemplate.opsForValue().set(key, body);
  20.         }
  21.         return ResponseEntity.accepted().build();
  22.     }

  23.     @GetMapping("/{key}")
  24.     public ResponseEntity<byte[]> get(@PathVariable String key) {
  25.         byte[] data = binaryRedisTemplate.opsForValue().get(key);
  26.         if (data == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
  27.         HttpHeaders h = new HttpHeaders();
  28.         h.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  29.         h.setContentLength(data.length);
  30.         return new ResponseEntity<>(data, h, HttpStatus.OK);
  31.     }
  32. }
复制代码
启动类
  1. package com.example;

  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. @SpringBootApplication
  5. public class RedisBinaryDemoApplication {
  6.     public static void main(String[] args) {
  7.         SpringApplication.run(RedisBinaryDemoApplication.class, args);
  8.     }
  9. }
复制代码
Java 8 客户端调用示例(使用 RestTemplate)
  1. // 普通 Java 项目或测试里使用
  2. import org.springframework.http.*;
  3. import org.springframework.web.client.RestTemplate;

  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;

  6. public class ClientDemo {
  7.     public static void main(String[] args) throws Exception {
  8.         RestTemplate rt = new RestTemplate();

  9.         // 上传二进制
  10.         byte[] bytes = Files.readAllBytes(Paths.get("D:/tmp/image.png"));
  11.         HttpHeaders h = new HttpHeaders();
  12.         h.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  13.         HttpEntity<byte[]> req = new HttpEntity<>(bytes, h);
  14.         rt.exchange("http://localhost:8080/bin/pic1?ttl=3600", HttpMethod.PUT, req, Void.class);

  15.         // 下载二进制
  16.         ResponseEntity<byte[]> resp = rt.getForEntity("http://localhost:8080/bin/pic1", byte[].class);
  17.         if (resp.getStatusCode().is2xxSuccessful()) {
  18.             Files.write(Paths.get("D:/tmp/out.png"), resp.getBody());
  19.         }
  20.     }
  21. }
复制代码
  • 用 curl 调用也行:
    • 上传:curl -X PUT --data-binary @image.png "http://localhost:8080/bin/pic1?ttl=3600"
    • 下载:curl -o out.png "http://localhost:8080/bin/pic1"

相关帖子

扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员6折;永久VIP4折
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
312337667

微信扫一扫,私享最新原创实用干货

QQ|免责声明|小黑屋|依星资源网 ( 鲁ICP备2021043233号-3 )|网站地图

GMT+8, 2025-11-1 11:52

Powered by Net188.com X3.4

邮箱:312337667@qq.com 客服QQ:312337667(工作时间:9:00~21:00)

快速回复 返回顶部 返回列表