源码资源网 发表于 2025-11-1 09:57:10

springboot对redis存取二进制数据

springboot对redis存取二进制数据
可以用 RedisTemplate 配置成“值为 byte[] 的模板”,用 ByteArrayRedisSerializer 确保按原始二进制读写(不做字符编码/对象序列化)。配置package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.ByteArrayRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisBinaryConfig {

    @Bean
    public RedisTemplate<String, byte[]> binaryRedisTemplate(RedisConnectionFactory cf) {
      RedisTemplate<String, byte[]> t = new RedisTemplate<>();
      t.setConnectionFactory(cf);
      t.setKeySerializer(StringRedisSerializer.UTF_8);
      t.setValueSerializer(ByteArrayRedisSerializer.INSTANCE);
      t.setHashKeySerializer(StringRedisSerializer.UTF_8);
      t.setHashValueSerializer(ByteArrayRedisSerializer.INSTANCE);
      t.afterPropertiesSet();
      return t;
    }
}
使用

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

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

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

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

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

    public byte[] getHash(String hkey, String field) {
      Object v = binaryRedisTemplate.opsForHash().get(hkey, field);
      return v instanceof byte[] ? (byte[]) v : null;
    }
}

要点
[*]不要用 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 的二进制
package com.example.web;

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

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

@RestController
@RequestMapping("/bin")
public class BinaryController {

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

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

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

# 下载二进制到文件
curl -o out.png "http://localhost:8080/bin/pic1"
[*]Java(JDK 11+)
import java.net.http.*;
import java.net.URI;
import java.nio.file.*;

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

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

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


下面给出一个可在 Java 8 (1.8.0_152) 运行的 Spring Boot 2.7 示例:用 RedisTemplate<String, byte[]> 读写二进制,并通过 HTTP 接口让客户端上传/下载。pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>redis-binary-demo</artifactId>
<version>1.0.0</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
</parent>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
      <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
</build>
</project>application.ymlspring:
redis:
    host: 127.0.0.1
    port: 6379
server:
port: 8080配置 RedisTemplate(值为 byte[])
package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.ByteArrayRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisBinaryConfig {
    @Bean
    public RedisTemplate<String, byte[]> binaryRedisTemplate(RedisConnectionFactory cf) {
      RedisTemplate<String, byte[]> t = new RedisTemplate<>();
      t.setConnectionFactory(cf);
      t.setKeySerializer(StringRedisSerializer.UTF_8);
      t.setValueSerializer(ByteArrayRedisSerializer.INSTANCE);
      t.setHashKeySerializer(StringRedisSerializer.UTF_8);
      t.setHashValueSerializer(ByteArrayRedisSerializer.INSTANCE);
      t.afterPropertiesSet();
      return t;
    }
}Controller:上传/下载二进制
package com.example.web;

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

import java.time.Duration;

@RestController
@RequestMapping("/bin")
public class BinaryController {

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

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

    @GetMapping("/{key}")
    public ResponseEntity<byte[]> get(@PathVariable String key) {
      byte[] data = binaryRedisTemplate.opsForValue().get(key);
      if (data == null) return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
      HttpHeaders h = new HttpHeaders();
      h.setContentType(MediaType.APPLICATION_OCTET_STREAM);
      h.setContentLength(data.length);
      return new ResponseEntity<>(data, h, HttpStatus.OK);
    }
}启动类
package com.example;

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

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

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

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

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

      // 下载二进制
      ResponseEntity<byte[]> resp = rt.getForEntity("http://localhost:8080/bin/pic1", byte[].class);
      if (resp.getStatusCode().is2xxSuccessful()) {
            Files.write(Paths.get("D:/tmp/out.png"), resp.getBody());
      }
    }
}
[*]用 curl 调用也行:

[*]上传:curl -X PUT --data-binary @image.png "http://localhost:8080/bin/pic1?ttl=3600"
[*]下载:curl -o out.png "http://localhost:8080/bin/pic1"

页: [1]
查看完整版本: springboot对redis存取二进制数据

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