如下图,本机(服务器)存储的图片想要在浏览器上通过Url地址访问:
实现很简单,只需要利用拦截器将本机地址映射成url路径就行:
@Configuration
public class FilePathConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/upload/**") //虚拟url路径
.addResourceLocations("file:E:/image/"); //真实本地路径
}
}
启动程序,输入本机Ip+端口+Url路径(替代了图片本地路径)+图片文件名:
尝试上传后再访问:
public static final String host = "http://127.0.0.1:8080/upload/";
@ApiOperation(value = "上传图片")
@ApiImplicitParam(name = "file",value = "图片上传",required = true,dataTypeClass = MultipartFile.class,
allowMultiple = true,paramType = "query")
@PostMapping(value = "/upload")
@ResponseBody
public String insertOrderImg(@RequestParam("file") MultipartFile file) {
// 文件上传路径
String location = "E:\\image\\";
String originalFilename = file.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
//设置允许上传文件类型
String suffixList = ".jpg,.png,.ico,.bmp,.jpeg";
String imgUrl=null;
// 判断是否包含
if (suffixList.contains(extName.trim().toLowerCase())) {
// 保存文件的路径
String path = location + originalFilename;
// spring的transferTo保存文件方法
try {
file.transferTo(new File(path));
imgUrl = host + originalFilename;
} catch (IOException e) {
log.info("保存失败");
}
}
return imgUrl;
}
使用Hutool或Thumbnails
引入依赖
<dependency>
<groupId>com.siashangroupId>
<artifactId>toolkit-imageartifactId>
<version>1.1.1version>
dependency>
public static final String host = "http://127.0.0.1:8080/upload/";
@ApiOperation(value = "上传图片")
@ApiImplicitParam(name = "file",value = "图片上传",required = true,dataTypeClass = MultipartFile.class,
allowMultiple = true,paramType = "query")
@PostMapping(value = "/upload")
@ResponseBody
public String insertOrderImg(@RequestParam("file") MultipartFile file) throws IOException {
// 文件上传路径
String location = "E:\\image\\";
String originalFilename = file.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
//设置允许上传文件类型
String suffixList = ".jpg,.png,.ico,.bmp,.jpeg";
String imgUrl=null;
// 判断是否包含
if (suffixList.contains(extName.trim().toLowerCase())) {
BufferedImage image = ImgUtil.read(file.getInputStream());
int height = image.getHeight();
int width = image.getWidth();
// 保存文件的路径
String path = location + originalFilename;
// thumbnailator压缩存放路径
String thumbPath= location+"thumbnailator"+originalFilename;
//Img压缩存放路径
String imgPath = location +"img"+ originalFilename;
String newPath = location +"new"+ originalFilename;
String shaValue = DigestUtil.sha256Hex(file.getBytes());
// spring的transferTo保存文件方法
try {
log.info("原图高{},宽{},大小{}KB",height,width,file.getSize()/1024);
file.transferTo(new File(path));
//Hutool图像编辑器压缩
Img img = Img.from(file.getInputStream());
Image img1 = img.scale(0.5f).setQuality(0.2f).getImg();
byte[] bytes = ImgUtil.toBytes(img1, ImgUtil.IMAGE_TYPE_JPEG);
log.info("Hutool压缩后高{},宽{},大小{}KB",img1.getHeight(null),img1.getWidth(null),bytes.length/1024);
FileUtil.writeBytes(bytes,imgPath);
//Thumbnails压缩 要读取压缩后图片大小和尺寸必须写后再读,没有Img编辑器压缩方便
OutputStream outputStream1 = new FileOutputStream(thumbPath);
Thumbnails.of(file.getInputStream())
.scale(0.5f)
.outputQuality(0.2f)
.toOutputStream(outputStream1);
outputStream1.close();
byte[] bytes1 = FileUtil.readBytes(thumbPath);
BufferedImage read1 = ImgUtil.read(thumbPath);
log.info("直接压缩高{},宽{},大小{}KB",read1.getHeight(),read1.getWidth(),bytes1.length/1024);
// //Hutool另一种压缩方法,要读取压缩后图片大小和尺寸必须写后再读 ,没有Img编辑器压缩方便
// OutputStream outputStream2 = new FileOutputStream(newPath);
// ImageOutputStream imageOutputStream = ImgUtil.getImageOutputStream(outputStream2);
// ImgUtil.scale(image, imageOutputStream, 0.5f);
// ImgUtil.write(image,ImgUtil.IMAGE_TYPE_JPG,imageOutputStream,0.2F);
// outputStream2.close();
// byte[] bytes2 = FileUtil.readBytes(newPath);
// BufferedImage read2 = ImgUtil.read(newPath);
// log.info("Hutool直接压缩高{},宽{},大小{}KB",read2.getHeight(),read2.getWidth(),bytes2.length/1024);
imgUrl = host + originalFilename;
} catch (IOException e) {
log.info("保存失败");
}
}
return imgUrl;
}
public static final String host = "http://127.0.0.1:8080/upload/";
public static final Map<String,String> imgMap= new ConcurrentHashMap<>();
@ApiOperation(value = "上传图片")
@ApiImplicitParam(name = "file",value = "图片上传",required = true,dataTypeClass = MultipartFile.class,
allowMultiple = true,paramType = "query")
@PostMapping(value = "/upload")
@ResponseBody
public String insertOrderImg(@RequestParam("file") MultipartFile file) throws IOException {
// 文件上传路径
String location = "E:\\image\\";
String originalFilename = file.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
//设置允许上传文件类型
String suffixList = ".jpg,.png,.ico,.bmp,.jpeg";
String imgUrl=null;
// 判断是否包含
if (suffixList.contains(extName.trim().toLowerCase())) {
BufferedImage image = ImgUtil.read(file.getInputStream());
int height = image.getHeight();
int width = image.getWidth();
// 保存文件的路径
String path = location + originalFilename;
// thumbnailator压缩存放路径
String thumbPath= location+"thumbnailator"+originalFilename;
//Img压缩存放路径
String imgPath = location +"img"+ originalFilename;
String newPath = location +"new"+ originalFilename;
//同一文件SHA256进行哈希运算后值唯一
String shaValue = DigestUtil.sha256Hex(file.getBytes());
if (imgMap.containsKey(shaValue)) {
log.info("重复的SHA{},已上传的文件名{}",shaValue,imgMap.get(shaValue));
return "已经上传,不可重复";
}
imgMap.put(shaValue,originalFilename);
// spring的transferTo保存文件方法
try {
log.info("原图高{},宽{},大小{}KB",height,width,file.getSize()/1024);
file.transferTo(new File(path));
//Hutool图像编辑器压缩
Img img = Img.from(file.getInputStream());
Image img1 = img.scale(0.5f).setQuality(0.2f).getImg();
byte[] bytes = ImgUtil.toBytes(img1, ImgUtil.IMAGE_TYPE_JPEG);
log.info("Hutool压缩后高{},宽{},大小{}KB",img1.getHeight(null),img1.getWidth(null),bytes.length/1024);
FileUtil.writeBytes(bytes,imgPath);
//Thumbnails压缩 要读取压缩后图片大小和尺寸必须写后再读,没有Img编辑器压缩方便
OutputStream outputStream1 = new FileOutputStream(thumbPath);
Thumbnails.of(file.getInputStream())
.scale(0.5f)
.outputQuality(0.2f)
.toOutputStream(outputStream1);
outputStream1.close();
byte[] bytes1 = FileUtil.readBytes(thumbPath);
BufferedImage read1 = ImgUtil.read(thumbPath);
log.info("Thumbnails压缩高{},宽{},大小{}KB",read1.getHeight(),read1.getWidth(),bytes1.length/1024);
// //Hutool另一种压缩方法,要读取压缩后图片大小和尺寸必须写后再读 ,没有Img编辑器压缩方便
// OutputStream outputStream2 = new FileOutputStream(newPath);
// ImageOutputStream imageOutputStream = ImgUtil.getImageOutputStream(outputStream2);
// ImgUtil.scale(image, imageOutputStream, 0.5f);
// ImgUtil.write(image,ImgUtil.IMAGE_TYPE_JPG,imageOutputStream,0.2F);
// outputStream2.close();
// byte[] bytes2 = FileUtil.readBytes(newPath);
// BufferedImage read2 = ImgUtil.read(newPath);
// log.info("Hutool直接压缩高{},宽{},大小{}KB",read2.getHeight(),read2.getWidth(),bytes2.length/1024);
imgUrl = host + originalFilename;
} catch (IOException e) {
log.info("保存失败");
}
}
return imgUrl;
}