public Map uploadPanorama(MultipartFile file) {
try {
String imgPattern = ".+(.JPEG|.jpeg|.JPG|.jpg|.png|.PNG)";
Pattern pattern = Pattern.compile(imgPattern);
String fileName = file.getOriginalFilename();
if (!pattern.matcher(fileName).find()) {
Preconditions.checkArgument(false, "只能上传图片");
}
String substring = fileName.substring(fileName.lastIndexOf("."));
//唯一名
String uuid = UUID.randomUUID().toString();
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) {
path = new File("");
}
File upload = new File(path.getAbsolutePath(), "static/panorama/panos/");
if (!upload.exists()) {
upload.mkdirs();
}
if (!upload.exists()) {
upload.mkdirs();
}
File dest = new File(path.getAbsolutePath(), "static/panorama/panos/" + uuid + File.separator + "pano" + substring);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
String panoramaPath = "//panorama//panos//" + uuid + "//pano" + substring;
Map<String, String> result = HttpUtil.cutPanorama(cutPanoramaPath, panoramaPath);
// Map<String, String> result = new HashMap<>();
result.put("imagePath", panoramaPath);
return result;
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException("程序出错!请稍后再试");
}
}
Java实现图片上传
最新推荐文章于 2022-08-01 18:06:08 发布