图片上传主要将需要上传的图片上传到对应的存储地址当中,再通过url访问图片就可以了;本文存储地址在本地,如果是在服务器上,配置服务器端的地址就可以了。
controller@ApiOperation(value = "图片上传", notes = "图片上传")
@PostMapping(value = "uploadCommodity")
public Result uploadImg(@RequestParam("file") List fileList, HttpServletRequest request) {
return Result.ok(appBasicServiceImpl.uploadImg(fileList, request));
}
service
package com.andis.app.service.impl;
import com.andis.app.dao.AppBasicDao;
import com.andis.app.dto.AppBasicInfo;
import com.andis.app.service.AppBasicService;
import com.andis.app.util.UploadUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@Service("appBasicServiceImpl")
public class AppBasicServiceImpl extends ServiceImpl implements AppBasicService {
//获取根路径
@Value("${file.path}")
private String dirPath;
@Override
public HashMap uploadImg(List fileList, HttpServletRequest request) {
if (UploadUtil.checkImg(fileList)){
HashMap hashMap=UploadUtil.upload(fileList,request,dirPath);
return hashMap;
}else {
HashMap hashMap=new HashMap();
hashMap.put("error","请上传bmp/gif/jpg/png格式的图片");
return hashMap;
}
}
}
上传工具类UploadUtil
package com.andis.app.util;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
public class UploadUtil {
public static Boolean checkImg(List fileList) {
if (fileList.isEmpty()) {
return false;
} else {
for (MultipartFile file : fileList) {
try {
//通过ImageIO检查是否是 bmp/gif/jpg/png图片
Image image = ImageIO.read(file.getInputStream());
return image != null;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return false;
}
}
public static HashMap upload(List fileList, HttpServletRequest request, String dirPath){
//定义URI地址
String fileDownloadUri="";
String fileNamePlus="";
HashMap urlMap=new HashMap<>();
for (MultipartFile file: fileList) {
// 获取文件名
String fileName = file.getOriginalFilename();
//创建文件
File dest = new File(dirPath+fileName);
//判断文件父目录是否存在
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdirs(); //这里因为创建的是多级目录,所以需要使用mkdirs()方法。使用mkdir()方法则文件夹创建不成功,会报找不到路径错误。
}
try {
//将文件内容写入创建的文件中
file.transferTo(dest);
//获得本机Ip(获取的是服务器的Ip)
InetAddress inetAddress=InetAddress.getLocalHost();
String ip=inetAddress.getHostAddress();
//URL地址的格式:http://ip:port/app/文件路径
fileDownloadUri=request.getScheme()+"://"+ ip+":"+request.getServerPort()+"/app/upload/"+fileName;
fileNamePlus=dirPath+fileName;
// if (fileDownloadUri==""){
// fileDownloadUri=request.getScheme()+"://"+ ip+":"+request.getServerPort()+"/app/upload/"+fileName;
// fileNamePlus=dirPath+fileName;
// }else {
// fileDownloadUri=fileDownloadUri+","+request.getScheme()+"://"+ ip+":"+request.getServerPort()+"/app/upload/"+fileName;
// fileNamePlus=dirPath+fileName+","+dirPath+fileName;
// }
} catch (IOException e) {
e.printStackTrace();
}
urlMap.put("source",fileNamePlus);
urlMap.put("target",fileDownloadUri);
}
//返回Url地址,以逗号分隔
return urlMap;
}
public static boolean deleteFiles(String pathName){
boolean flag = false;
//根据路径创建文件对象
File file = new File(pathName);
//路径是个文件且不为空时删除文件
if(file.isFile()&&file.exists()){
flag = file.delete();
}
//删除失败时,返回false
return flag;
}
}
配置图片映射地址
package com.andis.app.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${file.path}")
private String fileSavePath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/upload/**")
.addResourceLocations("file:"+fileSavePath);
}
}
application.yml
#文件上传根路径
server:
port: 8081
servlet:
context-path: /app
file:
path: D://upload/
测试
使用postman测试
删除图片,将source中的路径传到工具类当中的删除文件方法中就可以了。



