案例背景,我不想生成文件到服务器上面,同时这个文件是限时访问的,所以就有了如下需求
本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工具进行测试
上一篇:“大嘴博士”单挑巨子生物,可复美陷入信任危机 大嘴博士好不好 大嘴博士后续
下一篇:无视苹果 CEO 库克反对:得州州长正式签署应用商店年龄验证法案 库克就任苹果ceo十周年 苹果ceo库克政策观