案例背景,我不想生成文件到服务器上面,同时这个文件是限时访问的,所以就有了如下需求
本Demo的核心是使用Base64编码和解码,
同时redis直接保存字节码的也同理,需要配置redis模板的序列化方式
配置application.yml,使其允许上传100M的附件
spring: servlet: multipart: max-file-size: 100MB max-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 {
@Autowired
private 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 {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
// 准备RedisTemplate对象
RedisTemplate<String,Object> 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工具进行测试