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

相关内容

热门资讯

安踏营收破800亿全球第三,光... 破局之路任重道远。手工劳动/兽妹手工编辑 /角叔出品/独角兽观察3月25日午间,安踏体育正式发布20...
数字营销“出海战”!易点天下冲... 本文来源:时代商业研究院 作者:时代商业研究院 图源:图虫创意来源|时代商业研究院作者|实习生陈嘉婕...
“杭州六小龙”第一股要来了!群... 本文来源:时代周报 作者:李佳晅“杭州六小龙”的第一家上市公司来了,不是宇树,而是群核科技。3月29...
思谋科技递表港交所,学界大佬创... 3月16日,思谋科技正式递表港交所,联席保荐人为摩根士丹利、中金公司和德意志银行。这家成立仅六年的年...
A股三大股指涨跌互现:铝板块带... A股三大股指3月30日集体低开。早盘探底回升,三大股指涨跌互现。午后两市维持窄幅震荡的走势,上涨个股...
南山铝业、常铝股份等涨停背后:... 受中东地缘冲突突发升级影响,全球铝供应端再生扰动。3月30日,A股铝板块走强,多只个股收涨。南山铝业...
摩尔线程斩获6.6亿元合同订单... 3月30日晚间,摩尔线程(688795)智能科技(北京)股份有限公司(简称“摩尔线程”)披露重大合同...
正力新能业绩会直击:增加供应商... 3月30日晚间,正力新能(03677.HK)发布2025年度业绩,2025年实现营业收入超过81亿元...
山外山:股东大健康和力远健鲲拟... 5月7日消息,山外山公告称,股东大健康和力远健鲲因自身资金需求,拟通过集中竞价交易或大宗交易的方式减...
teamLFG工作室加入Pla... 5月7日消息,索尼互娱工作室业务集团首席执行官Hermen Hulst宣布PlayStation S...