redis奇巧淫记之保存文件
创始人
2025-05-28 08:14:44
0

案例背景,我不想生成文件到服务器上面,同时这个文件是限时访问的,所以就有了如下需求 

本Demo的核心是使用Base64编码和解码,

配置application.yml,使其允许上传100M的附件

spring:servlet:multipart:max-file-size: 100MBmax-request-size: 100MB

 控制层代码

package com.example.demo11.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;@RestController
@RequestMapping("/file")
public class DownExcel {@Autowiredprivate RedisTemplate redisTemplate;@PostMapping("/uploadFile")public Long uploadFile(MultipartFile file) throws IOException {String value=Base64.getEncoder().encodeToString(file.getBytes());long key = new Date().getTime();ValueOperations valueOperations = redisTemplate.opsForValue();valueOperations.set("File:" + key, value);redisTemplate.expireAt("File:" + key, new Date(new Date().getTime() + 4000000));return key;}@GetMapping("/File/{key}")public void file(@PathVariable("key") String key, HttpServletRequest request, HttpServletResponse response) {ValueOperations valueOperations = redisTemplate.opsForValue();Object o = valueOperations.get("File:" + key);if (o != null) {response.setContentType("application/x-download");ServletOutputStream outputStream = null;try {outputStream = response.getOutputStream();byte[] decode = Base64.getDecoder().decode(o.toString());outputStream.write(decode);} catch (IOException e) {throw new RuntimeException(e);} finally {try {if (outputStream != null) {outputStream.close();}} catch (IOException e) {throw new RuntimeException(e);}}}}}

静态化配置类()

package com.example.demo11.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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){// 准备RedisTemplate对象RedisTemplate redisTemplate = new RedisTemplate<>();// 设置连接工厂redisTemplate.setConnectionFactory(connectionFactory);// 创建JSON序列化工具GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();// 设置key的序列化redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setHashKeySerializer(RedisSerializer.string());// 设置value的序列化redisTemplate.setValueSerializer(jsonRedisSerializer);redisTemplate.setHashValueSerializer(jsonRedisSerializer);// 返回return redisTemplate;}}

上传文件

Linux使用如下命令

curl -X POST -F 'file=@/home/rock/Desktop/无标题.png' http://10.147.17.25:8080/file/upload

 Windows使用如下命令和参数

curl -X POST -H "Content-Type: multipart/form-data" -F "file=@D:/scrcpy/adb.exe" http://10.147.17.25:8080/file/upload

或者使用Apifox工具进行测试 

相关内容

热门资讯

吕天贵上任后首张业绩答卷,中信... 出品|达摩财经8月27日,中信银行(601998.SH)召开2026年半年度业绩发布会,该行新行长吕...
美股V型反弹,中概股多数上涨,... 北京时间8月28日,美股三大指数V型反弹,盘中一度全线下跌。截至23:00,道指涨0.38%,纳指涨...
A股第二高价股,拟10派10元... 8月28日晚间,源杰科技发布2026年半年报,上半年,公司实现营业收入9.25亿元,同比增长351....
深圳市教育局原局长郑秀玉出任福... 奥一新闻获悉,深圳市教育局原局长郑秀玉,已出任福田区政协党组书记、主席。 公开报道显示,8月27日,...
国内商品期货夜盘收盘,焦煤涨超... 8月28日,国内商品期货夜盘收盘,燃油跌0.54%,棉花跌0.06%,玻璃涨2.61%,纯碱涨2.1...
剧本杀恋陪,会变成牛郎产业吗?... 订阅 快刀财经 ▲ 做您的私人商学院在剧本杀情感本面前,奥斯卡还得练。作者:半佛仙人来源:半佛仙...
5万翻5次变160万:普通人逆... 如果手里只有5万元,怎样才能把它变成160万元?从数学上看,这并不复杂。5万元翻一倍是10万元,再翻...
财通证券:上半年净利润19亿元... 8月28日,财通证券披露2026年半年度报告。公司上半年实现营业收入43.47亿元,同比增长46.9...
华林证券:上半年净利润2.58... 8月28日,华林证券披露2026年半年度报告。公司上半年实现营业收入8.49亿元,同比增长1.67%...
A股“猴茅”,最新业绩公布 a... 8月28日,有“猴茅”之称的CRO概念股昭衍新药披露半年报,2026年上半年,公司实现营业收入7.0...