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工具进行测试 

相关内容

热门资讯

两只大牛股,紧急发声:不属实!... 5天3板超声电子:“超声电子高频板通过英伟达认证”情况不属实,目前无产品供货给英伟达9月13日晚间,...
科创板创新药企BD授权交易总额... 2026年半年度财报季已收官,在创新药企业以业绩扭亏和BD兑现吸引市场目光的同时,以CXO为代表的上...
蔚来李斌1-8月交付增近58%... 9月9日消息,上海蔚来汽车有限公司副总裁品牌与传播资深专家马麟在微博透露,9月8日下午,蔚来创始人、...
京东物流部署过万前置仓应对手机... IT之家 9 月 13 日消息,据京东物流黑板报,9 月,多个品牌旗舰手机集中上新。针对消费者希望尽...
特朗普让泽连斯基勿袭击俄柴油燃... 9月13日,当地时间9月13日,美国总统特朗普要求乌克兰总统泽连斯基停止袭击俄罗斯柴油燃料设施,称相...
下周,年内最便宜新股启动打新 ... 2026.09.13本文字数:1467,阅读时长大约2分钟作者 |第一财经 安卓封图 |AI生成下周...
北京:激活地理信息要素赋能城市... 9月13日,从北京市规划和自然资源委员会获悉,《北京市测绘地理信息条例》已由北京市第十六届人民代表大...
接下来该如何逃顶?建议看看这篇... 最近这段时间,很多朋友的心里其实都是悬着的。行情走到这个位置,大家见面聊得最多的就是,接下来到底该怎...
武汉:到2035年武汉区域科技... 9月13日,武汉市委十四届十三次全会召开,审议通过了《关于打造具有全球竞争力的开放创新生态 高水平建...
卡塔尔股市收盘上涨0.25% 9月13日,卡塔尔证券交易所主要股指今日收盘上涨0.25%,报9848.95点,上涨24.46点。全...