文件上传,是图片时再压缩
This commit is contained in:
parent
aefc1418ec
commit
03bdc7c3cc
|
@ -132,6 +132,17 @@ public class FileUploadUtils
|
||||||
//file.transferTo(desc);
|
//file.transferTo(desc);
|
||||||
IoUtil.close(out);
|
IoUtil.close(out);
|
||||||
String pathFileName = getPathFileName(baseDir, fileName);
|
String pathFileName = getPathFileName(baseDir, fileName);
|
||||||
|
|
||||||
|
String extension = getExtension(file);
|
||||||
|
String[] imgExtension = {"bmp", "gif", "jpg", "jpeg", "png"};
|
||||||
|
boolean isImg = false;
|
||||||
|
for (String str : imgExtension)
|
||||||
|
{
|
||||||
|
if (str.equalsIgnoreCase(extension))
|
||||||
|
{
|
||||||
|
isImg = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
// try {
|
// try {
|
||||||
// File input = new File(baseDir+"/"+fileName);
|
// File input = new File(baseDir+"/"+fileName);
|
||||||
// BufferedImage image = ImageIO.read(input);
|
// BufferedImage image = ImageIO.read(input);
|
||||||
|
@ -151,21 +162,24 @@ public class FileUploadUtils
|
||||||
// } catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
// System.out.println("压缩失败:" + e.getMessage());
|
// System.out.println("压缩失败:" + e.getMessage());
|
||||||
// }
|
// }
|
||||||
try {
|
//如果是图片则压缩
|
||||||
Date date = new Date();
|
if(isImg){
|
||||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
|
try {
|
||||||
Random random = new Random();
|
Date date = new Date();
|
||||||
int randomNumber = random.nextInt(900000) + 100000; // 生成一个介于100000到999999之间的随机数
|
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
|
||||||
String outfilename = fileName.substring(0,fileName.lastIndexOf("/"))+"/"+sdf.format(date)+randomNumber+fileName.substring(fileName.lastIndexOf("."),fileName.length());
|
Random random = new Random();
|
||||||
|
int randomNumber = random.nextInt(900000) + 100000; // 生成一个介于100000到999999之间的随机数
|
||||||
|
String outfilename = fileName.substring(0,fileName.lastIndexOf("/"))+"/"+sdf.format(date)+randomNumber+fileName.substring(fileName.lastIndexOf("."),fileName.length());
|
||||||
|
|
||||||
File inputFile = new File(baseDir + File.separator + fileName);
|
File inputFile = new File(baseDir + File.separator + fileName);
|
||||||
File outputFile = new File(baseDir + File.separator + outfilename); // 输出图片文件
|
File outputFile = new File(baseDir + File.separator + outfilename); // 输出图片文件
|
||||||
String retImgName = "/file/uploadPath/"+outfilename;
|
String retImgName = "/file/uploadPath/"+outfilename;
|
||||||
float quality = 0.45f; // 压缩质量,范围0.0到1.0
|
float quality = 0.45f; // 压缩质量,范围0.0到1.0
|
||||||
compressImage(inputFile, outputFile, quality);
|
compressImage(inputFile, outputFile, quality);
|
||||||
pathFileName = retImgName;
|
pathFileName = retImgName;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return pathFileName;
|
return pathFileName;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue