fileupload组件新增extendParams扩展参数,附加数据,方便后台做出相应处理;新增ImageUtils工具类生成缩略图;Ueditor新增StorageManager.uploadFileSuccess方法,方便写上传文件后回调。

This commit is contained in:
thinkgem
2019-01-06 20:02:21 +08:00
parent 8d46bf2e9d
commit 2af14b2244
6 changed files with 98 additions and 56 deletions

View File

@@ -0,0 +1,70 @@
/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
*/
package com.jeesite.common.image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.lang.StringUtils;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
/**
* 图片处理工具类
* @author ThinkGem
* @version 2018年12月31日
*/
public class ImageUtils {
private static Logger logger = LoggerFactory.getLogger(ImageUtils.class);
/**
* 缩略图生成,处理一些较大的图片,防止占用太多的网络资源
*/
public static void thumbnails(File imageFile, int maxWidth, int maxHeight, String outputFormat){
if (imageFile == null || !imageFile.exists() || (maxWidth <= 0 && maxHeight <= 0)){
return;
}
// 只处理可以压缩的图片如gif图片压缩后会出现黑色背景的情况
String extension = FileUtils.getFileExtension(imageFile.getName());
if (!StringUtils.inString(extension, "png", "jpg", "jpeg", "bmp", "ico")){
return;
}
try{
BufferedImage bufferedImage = ImageIO.read(imageFile);
Builder<BufferedImage> bilder = Thumbnails.of(bufferedImage);
if (bufferedImage != null){
if (maxWidth > 0){
if (bufferedImage.getWidth() <= maxWidth){
bilder.width(bufferedImage.getWidth());
}else{
bilder.width(maxWidth);
}
}
if (maxHeight > 0){
if (bufferedImage.getHeight() <= maxHeight){
bilder.height(bufferedImage.getHeight());
}else{
bilder.height(maxHeight);
}
}
if (StringUtils.isNotBlank(outputFormat)){
bilder.outputFormat(outputFormat);
}
bilder.toFile(imageFile);
}
}catch(IOException e){
logger.error("图片压缩失败:" + imageFile.getAbsoluteFile(), e);
}
}
}

View File

@@ -1,28 +1,21 @@
package com.jeesite.common.media;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.image.ImageUtils;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.io.PropertiesUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.lang.TimeUtils;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
/**
* 视频工具类
* @author ThinkGem
@@ -114,34 +107,11 @@ public class VideoUtils {
try {
File imgfile = new File(imgFile);
if (imgfile.exists()){
// 过滤掉gif图片因为gif图片转换后会出现黑色背景的情况gif基本也没有比较大的图片
if (!StringUtils.inString(FileUtils.getFileExtension(imgFile), "gif")){
Integer w = 800;
Integer h = 600;
if (StringUtils.isNotBlank(width)){
w = ObjectUtils.toInteger(width);
}
if (StringUtils.isNotBlank(height)){
h = ObjectUtils.toInteger(height);
}
Builder<File> file = Thumbnails.of(imgFile);
BufferedImage bufferedImage = ImageIO.read(imgfile);
if (bufferedImage.getWidth() <= w){
file.width(bufferedImage.getWidth());
}else{
file.width(w);
}
if (bufferedImage.getHeight() <= h){
file.height(bufferedImage.getHeight());
}else{
file.height(h);
}
file.toFile(imgFile);
}
ImageUtils.thumbnails(imgfile, 800, 600, null);
}else{
statusTemp = false;
}
} catch (IOException e) {
} catch (Exception e) {
statusTemp = false;
log.error("视频剪切图片失败", e);
}

View File

@@ -39,6 +39,9 @@ public final class Base64Uploader {
storageState.putInfo("url", ctx + PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", "");
// UEditor上传文件成功后调用事件
StorageManager.uploadFileSuccess(physicalPath, storageState);
}
return storageState;

View File

@@ -1,6 +1,5 @@
package com.jeesite.common.ueditor.upload;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -9,7 +8,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItemIterator;
@@ -20,8 +18,8 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.jeesite.common.image.ImageUtils;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.media.VideoUtils;
import com.jeesite.common.ueditor.PathFormat;
import com.jeesite.common.ueditor.define.ActionMap;
@@ -30,9 +28,6 @@ import com.jeesite.common.ueditor.define.BaseState;
import com.jeesite.common.ueditor.define.FileType;
import com.jeesite.common.ueditor.define.State;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
public class BinaryUploader {
public static final State save(HttpServletRequest request,
@@ -114,21 +109,8 @@ public class BinaryUploader {
// 如果开启了压缩图片
if ((Boolean)conf.get("imageCompressEnable")){
Integer width = (Integer)conf.get("imageCompressBorder");
// 过滤掉gif图片因为gif图片转换后会出现黑色背景的情况gif基本也没有比较大的图片
if (StringUtils.inString(FileUtils.getFileExtension(physicalPath),
"png","jpg","jpeg","bmp","ico")){
BufferedImage bufferedImage = ImageIO.read(new File(physicalPath));
Builder<BufferedImage> file = Thumbnails.of(bufferedImage);
if (bufferedImage != null){
if (bufferedImage.getWidth() <= width){
file.width(bufferedImage.getWidth());
}else{
file.width(width);
}
file.toFile(physicalPath);
}
}
Integer maxWidth = (Integer)conf.get("imageCompressBorder");
ImageUtils.thumbnails(new File(physicalPath), maxWidth, -1, null);
}
}
@@ -153,12 +135,19 @@ public class BinaryUploader {
storageState.putInfo("url", ctx + PathFormat.format(savePath) + "." + v.getOutputFileExtension());
storageState.putInfo("type", "." + v.getOutputFileExtension());
storageState.putInfo("original", originFileName +"."+ v.getInputFileExtension());
// Ueditor编辑器上传文件完成后调用事件
StorageManager.uploadFileSuccess(physicalPath, storageState);
return storageState;
}
}
storageState.putInfo("url", ctx + PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
// UEditor上传文件成功后调用事件
StorageManager.uploadFileSuccess(physicalPath, storageState);
}
return storageState;

View File

@@ -210,4 +210,14 @@ public class StorageManager {
return new BaseState(true);
}
/**
* UEditor上传文件成功后调用事件
* @param physicalPath 上传文件实际路径
* @param storageState.url 返回到客户端的文件访问地址
*/
public static void uploadFileSuccess(String physicalPath, State storageState){
}
}

View File

@@ -204,8 +204,8 @@
<#form:fileupload id="uploadImage2" returnPath="true"
filePathInputId="uploadImage2Path" fileNameInputId="uploadImage2Name"
uploadType="image" readonly="false" maxUploadNum="3" isMini="false"/>
<#form:input name="uploadImage2Path" class="form-control"/>
<#form:input name="uploadImage2Name" class="form-control"/>
<#form:input name="uploadImage2Path" value="/js/userfiles/fileupload/201812/1073024549485039616.png|/js/userfiles/fileupload/201812/1073043095867133952.png" class="form-control"/>
<#form:input name="uploadImage2Name" value="0 (1).png|0 (2).png" class="form-control"/>
</div>
</div>
</div>