资源类存储
1、springboot+mongodb
org.springframework.boot
spring-boot-starter-data-mongodb
org.projectlombok
lombok
true
# 服务端口
server.port=8090
# mongodb地址+数据库名
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/file
# 最大文件数
spring.servlet.multipart.max-file-size=10MB
# 最大请求数
spring.servlet.multipart.max-request-size=100MB
@Data
@document
//链式编程
@Accessors(chain = true)
public class UploadFile {
@Id
private String id;
private String name;
private LocalDateTime createdTime;
private Binary content;
private String contentType;
private long size;
public UploadFile(){
this.id = UUID.randomUUID().toString().substring(0,32);
}
}
@Data
@Accessors(chain = true)
public class JSONResult implements Serializable {
private Integer code;
private String msg;
private T data;
public static JSONResult build(int code, String msg, T data) {
return new JSONResult()
.setCode(code)
.setMsg(msg)
.setData(data);
}
}
@RestController
@RequestMapping("/db")
public class MongoDBController {
//封装集合名
public static String getCollection(String name){
return "fileCollection_"+name;
}
@Autowired
private MongoTemplate mongoTemplate;
@Value("${server.port}")
private String port;
@PostMapping("/upload")
public JSONResult uploadImage(@RequestParam("file")MultipartFile file){
JSONResult jsonResult = null;
try {
//设置图片封装类基本信息
UploadFile uploadFile = new UploadFile()
.setContentType(file.getContentType().split("/")[1])
.setName(file.getOriginalFilename())
.setCreatedTime(LocalDateTime.now())
.setContent(new Binary(file.getBytes()))
.setSize(file.getSize());
//存入mongodb
UploadFile savedFile = mongoTemplate.save(uploadFile, getCollection("photo"));
String url = "localhost:"+ port +"/db/image/show/"+savedFile.getId()+"/photo";
//前端展示信息
if (file.getOriginalFilename().contains("mp4")){
url = "localhost:"+ port +"/db/movie/show/"+savedFile.getId()+"/photo";
}
jsonResult = JSONResult.build(200, "图片上传成功", url);
} catch (Exception e) {
e.printStackTrace();
jsonResult = JSONResult.build(500,"图片上传失败",null);
}
return jsonResult;
}
@GetMapping("/movie/show/{id}/{name}")
public void movieLoad(@PathVariable("id") String id,
@PathVariable("name") String name,
HttpServletResponse response) throws Exception{
UploadFile file = mongoTemplate.findById(id, UploadFile.class, getCollection(name));
response.setContentType("video/mp4");
OutputStream outputStream = response.getOutputStream();
outputStream.write(file.getContent().getData());
outputStream.flush();
}
//设置返回值的字符编码
@GetMapping(value = "/image/show/{id}/{name}", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
public byte[] loadImage(@PathVariable("id") String id,
@PathVariable("name") String name){
List fileList = mongoTemplate.find(new Query(Criteria.where("_id").is(id)),UploadFile.class,getCollection(name));
byte[] data = null;
data = fileList.get(0).getContent().getData();
return data;
}
@GetMapping(value = "/image/out/{id}/{name}")
public void outImage(@PathVariable("id") String id,
@PathVariable("name") String name,
HttpServletResponse response) throws IOException{
List fileList = mongoTemplate.find(new Query(Criteria.where("_id").is(id)),UploadFile.class,getCollection(name));
byte[] data = fileList.get(0).getContent().getData();
response.setContentType("image/jpeg");
ServletOutputStream outputStream = response.getOutputStream();
BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));
ImageIO.write(image,"png",outputStream);
}
@GetMapping("/image/local/{id}/{name}")
public String loadLocal(@PathVariable("id") String id,
@PathVariable("name") String name) throws IOException {
//获取指定集合中指定对象
UploadFile file = mongoTemplate.findById(id,UploadFile.class,getCollection(name));
File file1 = new File("D:\temp\movies\"+file.getName());
if (file1.exists()){
file1.delete();
}
file1.createNewFile();
OutputStream outputStream = new FileOutputStream(file1);
outputStream.write(file.getContent().getData());
return "ok";
}
}
db
视频展示
2、springboot+OSS(阿里OSS对象存储)
com.aliyun.oss
aliyun-sdk-oss
3.10.2
@Data
@document
@Accessors(chain = true)
public class LoadFile {
@Id
private String id;
private String filePath;
private String name;
private LocalDateTime createdTime;
private InputStream content;
private String contentType;
private long size;
}
@Component
public class Aliyun {
@Autowired
private RestTemplate restTemplate;
/// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-beijing.aliyuncs.com。
private static String endpoint = "https://oss-cn-beijing.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
//从阿里控制台点击头像可以看到AccessKey
private static String accessKeyId = "******";
private static String accessKeySecret = "******";
private static String bucketName = "******";
public String upload(LoadFile file, String url) {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String key = file.getFilePath() + "/" + file.getName();
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file.getContent());
//上传到oss后就在本地下载
//只是测试---------
String result = restTemplate.getForObject(url, String.class, file.getName(), file.getFilePath());
//-----测试--------
// 上传字符串。
ossClient.putObject(putObjectRequest);
// 关闭OSSClient。
ossClient.shutdown();
return result;
}
//能读但是没办法显示。。
public byte[] load(String key) throws IOException {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
OSSObject ossObject = ossClient.getObject(bucketName, key);
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
StringBuffer buffer = new StringBuffer();
while (true) {
String line = reader.readLine();
buffer.append(line);
if (line == null) {
break;
}
}
byte[] data = buffer.toString().getBytes(Charset.defaultCharset());
reader.close();
// 关闭OSSClient。
ossClient.shutdown();
return data;
}
public void loadLocal(String key) throws IOException {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
File file = new File("D:\temp\" + key);
if (file.exists()) {
file.delete();
}
file.createNewFile();
ossClient.getObject(new GetObjectRequest(bucketName, key), file);
// 关闭OSSClient。
ossClient.shutdown();
}
}
@RestController
@RequestMapping("/oss")
public class OSSController {
@Autowired
Aliyun aliyun;
@Autowired
RestTemplate restTemplate;
@PostMapping("/image/upload")
public JSONResult upload(@RequestParam("file")MultipartFile file) throws Exception{
LoadFile loadFile = new LoadFile()
.setFilePath("movie")
.setName(file.getOriginalFilename())
.setContent(file.getInputStream());
String url = "http://localhost:8090/oss/load/"+file.getOriginalFilename()+"/"+loadFile.getFilePath();
String result = aliyun.upload(loadFile, url);
JSONResult jsonResult = JSONResult.build(200, result,url);
return jsonResult;
}
@GetMapping(value = "/load/{name}/{path}")
public String load(@PathVariable("name")String name,
@PathVariable("path")String path) throws Exception{
aliyun.loadLocal(path + "/" + name);
return "生成文件已经下载直本地D:/temp"+path+"/"+name;
}
}
可能restTemplat会出错 RestTemplateConfig
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
oss
@Controller
public class RouteController {
@GetMapping("/image")
public String uploadImage(){return "imageUpload";}
@GetMapping("movie")
public String uploadMovie(){return "movies";}
@GetMapping("/oss/image")
public String uploadOSSImage(){return "ossUpload";}
@GetMapping("/oss/movie")
public String uploadOSSMovie(){return "OOSMovies";}
}