//上传文件
public void uploadImg(MultipartFile[] multipartFiles){
for(int i=0;i<multipartFiles.length;i++) {
GenerateImage(multipartFiles[i]);
}
}
public boolean GenerateImage(MultipartFile multipartFile) {
// 判断图像数据为空
String name =multipartFile.getOriginalFilename();
String fileExt = name.substring(name.lastIndexOf(".") + 1).toLowerCase();
try {
// 生成图片
InputStream inputStream = multipartFile.getInputStream();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
byte[] bs = new byte[1024];
PropertiesLoader loader = new PropertiesLoader("filePath.properties");
String filePath = loader.getProperty("filePath");
File saveDirFile = new File(filePath+"/uploadImg");
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
String imgFilePath = saveDirFile.getPath() +"/"+ newFileName;
int len;
OutputStream out = new FileOutputStream(imgFilePath);
while ((len = inputStream.read(bs)) != -1) {
out.write(bs, 0, len);
}
out.flush();
out.close();
AppImage appImage = new AppImage();
//Global.USERFILES_BASE_URL此处为虚拟路径"/userfiles/"
String savePath = Global.USERFILES_BASE_URL+"uploadImg/"+newFileName;
appImage.setUrl(savePath);
appImage.setDelFlag("0");
appImage.preInsert();
uploadDao.insert(appImage);
return true;
} catch (Exception e) {
return false;
}
}
此处需要配置文件中定义filePath=D:\files路径