java图片上传
package com.sky.controller.admin;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
@Autowired
private AliOssUtil aliOssUtil;
@PostMapping("/upload")
@ApiOperation("文件上传")
public Result<String> upload(MultipartFile file){
log.info("文件上传:{}",file);
try {
String originalFilename=file.getOriginalFilename();
String extension= originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName= UUID.randomUUID().toString()+extension;
String filePath= aliOssUtil.upload(file.getBytes(),objectName);
return Result.success(filePath);
} catch (IOException e) {
log.error("文件上传失败:{}",e);
}
return Result.error(MessageConstant.UPLOAD_FAILED);
}
@PostMapping("/uploadben")
@ApiOperation("文件上传到本地")
public Result<String> uploadben(MultipartFile file){
log.info("文件上传到本地:{}",file);
try {
String originalFilename=file.getOriginalFilename();
String extension= originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName= UUID.randomUUID().toString()+extension;
log.info("新的文件名:{}",objectName);
String projectDir = System.getProperty("user.dir");
System.out.println(projectDir);
LocalDate date = LocalDate.now();
String folderPath="/public/uploads/"+ date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))+"/";
String folderName =projectDir+folderPath;
File folder = new File(folderName);
if (!folder.exists()) {
boolean result = folder.mkdirs();
}
file.transferTo(new File(folderName+objectName));
return Result.success(folderPath+objectName);
} catch (IOException e) {
log.error("文件上传失败:{}",e);
}
return Result.error(MessageConstant.UPLOAD_FAILED);
}
@GetMapping("/download")
@ApiOperation("文件下载")
public void downloadfile(HttpServletRequest req, HttpServletResponse resp) throws Exception {
File file = new File("D:/ApowerREC/20211124_084339.mp4");
String fileName = file.getName();
String userAgent = req.getHeader("User-Agent").toLowerCase();
if (userAgent.indexOf("firefox") != -1) {
System.out.println("====================火狐浏览器====================");
resp.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
}
else {
System.out.println("====================谷歌浏览器====================");
resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
}
resp.setCharacterEncoding("UTF-8");
resp.addHeader("Content-Length", "" + file.length());
resp.setContentType("video/mpeg4");
FileInputStream fis = null;
OutputStream os = null;
try {
os = resp.getOutputStream();
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer,0,len);
}
} catch (Exception e) {
if (null != fis) {
fis.close();
}
if (null != os) {
os.flush();
os.close();
}
}
}
@GetMapping("/downloadglb")
@ApiOperation("模型文件下载")
public void downloadfileglb(HttpServletRequest req, HttpServletResponse resp) throws Exception {
File file = new File("D:/ApowerREC/收费站Draco.glb");
String fileName = file.getName();
String userAgent = req.getHeader("User-Agent").toLowerCase();
if (userAgent.indexOf("firefox") != -1) {
System.out.println("====================火狐浏览器====================");
resp.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
}
else {
System.out.println("====================谷歌浏览器====================");
resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
}
resp.setCharacterEncoding("UTF-8");
resp.addHeader("Content-Length", "" + file.length());
resp.setContentType("application/glTF-binary");
FileInputStream fis = null;
OutputStream os = null;
try {
os = resp.getOutputStream();
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer,0,len);
}
} catch (Exception e) {
if (null != fis) {
fis.close();
}
if (null != os) {
os.flush();
os.close();
}
}
}
}
vue3前端请求下载地址
<template>
<div class="p-2">
<video :src="videosrc" style="width: 500px;height: 200px;" controls preload="auto" controlslist="nodownload"></video>
</div>
</template>
<script setup>
import {ref,onMounted,onBeforeUnmount,reactive,computed } from 'vue'
import service from '~/api/apiaxios';
const videosrc=ref(null);
onMounted(async()=>{
const res2= await service.get(`/admin/common/download`,{responseType: "blob"}).then(data=>{
console.log(data)
let url = window.URL.createObjectURL(new Blob([data]))
console.log(url)
videosrc.value=url
})
})
onBeforeUnmount(()=> {
})
</script>
<style>
</style>
