<!-- 上传的文件的限制 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
public static List<String> imageUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());
// 上文图片路径
List<String> listFile = new ArrayList<>();
String imageUrl = null;
if (cmr.isMultipart(request)) {
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (request);
List<MultipartFile> listImage = mRequest.getFiles("picture");
if (listImage != null && listImage.size() > 0) {
for (MultipartFile mFile : listImage) {
if (!mFile.isEmpty()) {
String suffix = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf("."));
suffix = suffix.toLowerCase();
if (suffix.equals(".jpg") || suffix.equals(".jpeg") || suffix.equals(".png")) {
String fileName = UUID.randomUUID().toString();
if (mFile.getSize() < 10000000 && mFile.getSize() > 0) {
PropertiesUtils pro = new PropertiesUtils();
// String path = pro.load("file.properties") +
// "/upload/image";
// 注意 注意 线上获取上传路径地址
String path = request.getServletContext().getRealPath("upload/image/");
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
// 缩略图上传路径 start
String desPath = path + "/thum_" + fileName;
// 压缩图片
Thumbnails.of(mFile.getInputStream()).scale(1f).outputQuality(0.38f).toFile(desPath);
imageUrl = request.getRequestURL().toString();
int index = imageUrl.indexOf("xdfstar") + 7;
String url = imageUrl.substring(0, index);
imageUrl = url + "/upload/image/" + "thum_" + fileName + ".JPEG";
request.setAttribute("imageUrl", imageUrl);
// 存放所有上传图片的路径
listFile.add(imageUrl);
} else {
listFile = null;
}
} else {
}
}
}
} else {
listFile = null;
}
}
return listFile;
}