Springboot 图片上传、多图上传、图片压缩(后台代码)
话不多说,直接上代码!
简单的前端HTML测试代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script type="text/javascript">
$(function() {
$('input[id="uploadcyefuji"]').change(function(e){
var formData = new FormData();
for (i= 0 ; i <$('#uploadcyefuji')[0].files.length; i++) {
formData.append('file', $('#uploadcyefuji')[0].files[i]);
}
formData.append('pathName', 'test');
$.ajax({
// url 记得写成自己的接口路径
url: 'http://192.168.0.100:8443/uploadimgAPP',
type: 'POST',
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
alert("上传成功")
}).fail(function(res) {
alert("上传失败");
});
});
});
</script>
</head>
<body>
<input type="hidden" id="insertcyefuji" name="fuji" />
<input type="file" id="uploadcyefuji" name="file" multiple/>
</body>
</html>
后台Controller
@RequestMapping(value = "/uploadimgAPP", method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> upload(@RequestParam("file") MultipartFile[] file,@RequestParam String pathName,
HttpServletRequest request) {
Map<String,Object> map=new HashMap<String, Object>();
if(file!=null&&file.length>0){
String fileName=null;
try {
for (int i = 0; i < file.length; i++) {
if (!file[i].isEmpty()) {
map.put("fileName", uploadImage(FilePath+pathName,pathName, file[i], true));
System.out.println(fileName);
Map<String,String> mapqMap =new HashMap<String, String>();
mapqMap= (Map<String, String>) map.get("fileName");
fileName = mapqMap.get("YT");
System.out.println(fileName);
}
}
//上传成功
if(fileName!=null){
logger.debug("上传成功!");
map.put("status", fileName);
}else {
map.put("status", "上传失败!文件格式错误!");
//throw new RuntimeException("上传失败!文件格式错误!");
}
} catch (Exception e) {
e.printStackTrace();
map.put("status", "上传异常!");
}
}else {
map.put("status", "没有检测到有效文件!");
}
return map;
}
/**
* 上传图片
* 原名称
* @param request 请求
* @param path_deposit 存放位置(路径)
* @param file 文件
* @param isRandomName 是否随机名称
* @return 完整文件路径
*/
public Map<String, String> uploadImage(String path,String pathName,MultipartFile file,boolean isRandomName) {
//上传
try {
String[] typeImg={"gif","png","jpg","jpeg"};
if(file!=null){
String origName=file.getOriginalFilename();// 文件原名称
System.out.println("上传的文件原名称:"+origName);
// 判断文件类型
String type =origName.indexOf(".")!=-1?origName.substring(origName.lastIndexOf(".")+1, origName.length()):null;
if (type!=null) {
boolean booIsType=false;
for (int i = 0; i < typeImg.length; i++) {
if (typeImg[i].equals(type.toLowerCase())) {
booIsType=true;
}
}
//类型正确
if (booIsType) {
//组合名称
String fileSrc="";
//是否随机名称
if(isRandomName){
origName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"_"+UUID.randomUUID().toString()+origName.substring(origName.lastIndexOf("."));
}
//判断是否存在目录
String path1 = "C:/pingtai/"+pathName+"t";
File Fpath=new File(path);
if(!Fpath.exists()){
Fpath.mkdirs();//创建目录
}
File Fpath1=new File(path1);
if(!Fpath1.exists()){
Fpath1.mkdirs();//创建目录
}
//上传
File targetFile=new File(path,origName);
file.transferTo(targetFile);
//完整路径
fileSrc="pingtai/"+pathName+"/"+origName;
//压缩图片
long size = file.getSize();
double scale = 1.0d ;
if(size >= 200*1024){
if(size > 0){
scale = (200*1024f) / size ;
}
}
try {
if(size < 200*1024){
Thumbnails.of(fileSrc).scale(1f).toFile(fileSrc);
}else{
Thumbnails.of("C:/"+fileSrc).scale(1f).outputQuality(0.25f).toFile("C:/"+fileSrc);
}
} catch (Exception e1) {
}
//缩略图
String fileSrc1="pingtai/"+pathName+"t/"+origName;
try {
Thumbnails.of("C:/"+fileSrc).scale(0.4f).outputQuality(0.4f).toFile("C:/"+fileSrc1);
}catch(Exception e1){
}
System.out.println("图片上传成功:"+fileSrc);
Map<String, String> map = new HashMap<String,String>();
map.put("SL", fileSrc1);//
map.put("YT", fileSrc);
return map;
}
}
}
return null;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
本文详细介绍了一种使用SpringBoot实现图片上传、多图上传及图片压缩的方法,包括前端HTML测试代码与完整的后端Controller代码。
1471

被折叠的 条评论
为什么被折叠?



