fileupload组件新增extendParams扩展参数,附加数据,方便后台做出相应处理;新增ImageUtils工具类生成缩略图;Ueditor新增StorageManager.uploadFileSuccess方法,方便写上传文件后回调。
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,28 +1,21 @@
|
|||||||
package com.jeesite.common.media;
|
package com.jeesite.common.media;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.jeesite.common.collect.ListUtils;
|
import com.jeesite.common.collect.ListUtils;
|
||||||
|
import com.jeesite.common.image.ImageUtils;
|
||||||
import com.jeesite.common.io.FileUtils;
|
import com.jeesite.common.io.FileUtils;
|
||||||
import com.jeesite.common.io.PropertiesUtils;
|
import com.jeesite.common.io.PropertiesUtils;
|
||||||
import com.jeesite.common.lang.ObjectUtils;
|
|
||||||
import com.jeesite.common.lang.StringUtils;
|
import com.jeesite.common.lang.StringUtils;
|
||||||
import com.jeesite.common.lang.TimeUtils;
|
import com.jeesite.common.lang.TimeUtils;
|
||||||
|
|
||||||
import net.coobird.thumbnailator.Thumbnails;
|
|
||||||
import net.coobird.thumbnailator.Thumbnails.Builder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 视频工具类
|
* 视频工具类
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
@@ -114,34 +107,11 @@ public class VideoUtils {
|
|||||||
try {
|
try {
|
||||||
File imgfile = new File(imgFile);
|
File imgfile = new File(imgFile);
|
||||||
if (imgfile.exists()){
|
if (imgfile.exists()){
|
||||||
// 过滤掉gif图片,因为gif图片转换后会出现黑色背景的情况(gif基本也没有比较大的图片)。
|
ImageUtils.thumbnails(imgfile, 800, 600, null);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}else{
|
}else{
|
||||||
statusTemp = false;
|
statusTemp = false;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
statusTemp = false;
|
statusTemp = false;
|
||||||
log.error("视频剪切图片失败", e);
|
log.error("视频剪切图片失败", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ public final class Base64Uploader {
|
|||||||
storageState.putInfo("url", ctx + PathFormat.format(savePath));
|
storageState.putInfo("url", ctx + PathFormat.format(savePath));
|
||||||
storageState.putInfo("type", suffix);
|
storageState.putInfo("type", suffix);
|
||||||
storageState.putInfo("original", "");
|
storageState.putInfo("original", "");
|
||||||
|
|
||||||
|
// UEditor上传文件成功后调用事件
|
||||||
|
StorageManager.uploadFileSuccess(physicalPath, storageState);
|
||||||
}
|
}
|
||||||
|
|
||||||
return storageState;
|
return storageState;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.jeesite.common.ueditor.upload;
|
package com.jeesite.common.ueditor.upload;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -9,7 +8,6 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.apache.commons.fileupload.FileItemIterator;
|
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.MultipartFile;
|
||||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||||
|
|
||||||
|
import com.jeesite.common.image.ImageUtils;
|
||||||
import com.jeesite.common.io.FileUtils;
|
import com.jeesite.common.io.FileUtils;
|
||||||
import com.jeesite.common.lang.StringUtils;
|
|
||||||
import com.jeesite.common.media.VideoUtils;
|
import com.jeesite.common.media.VideoUtils;
|
||||||
import com.jeesite.common.ueditor.PathFormat;
|
import com.jeesite.common.ueditor.PathFormat;
|
||||||
import com.jeesite.common.ueditor.define.ActionMap;
|
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.FileType;
|
||||||
import com.jeesite.common.ueditor.define.State;
|
import com.jeesite.common.ueditor.define.State;
|
||||||
|
|
||||||
import net.coobird.thumbnailator.Thumbnails;
|
|
||||||
import net.coobird.thumbnailator.Thumbnails.Builder;
|
|
||||||
|
|
||||||
public class BinaryUploader {
|
public class BinaryUploader {
|
||||||
|
|
||||||
public static final State save(HttpServletRequest request,
|
public static final State save(HttpServletRequest request,
|
||||||
@@ -114,21 +109,8 @@ public class BinaryUploader {
|
|||||||
|
|
||||||
// 如果开启了压缩图片
|
// 如果开启了压缩图片
|
||||||
if ((Boolean)conf.get("imageCompressEnable")){
|
if ((Boolean)conf.get("imageCompressEnable")){
|
||||||
Integer width = (Integer)conf.get("imageCompressBorder");
|
Integer maxWidth = (Integer)conf.get("imageCompressBorder");
|
||||||
// 过滤掉gif图片,因为gif图片转换后会出现黑色背景的情况(gif基本也没有比较大的图片)。
|
ImageUtils.thumbnails(new File(physicalPath), maxWidth, -1, null);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -153,12 +135,19 @@ public class BinaryUploader {
|
|||||||
storageState.putInfo("url", ctx + PathFormat.format(savePath) + "." + v.getOutputFileExtension());
|
storageState.putInfo("url", ctx + PathFormat.format(savePath) + "." + v.getOutputFileExtension());
|
||||||
storageState.putInfo("type", "." + v.getOutputFileExtension());
|
storageState.putInfo("type", "." + v.getOutputFileExtension());
|
||||||
storageState.putInfo("original", originFileName +"."+ v.getInputFileExtension());
|
storageState.putInfo("original", originFileName +"."+ v.getInputFileExtension());
|
||||||
|
|
||||||
|
// Ueditor编辑器上传文件完成后调用事件
|
||||||
|
StorageManager.uploadFileSuccess(physicalPath, storageState);
|
||||||
|
|
||||||
return storageState;
|
return storageState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
storageState.putInfo("url", ctx + PathFormat.format(savePath));
|
storageState.putInfo("url", ctx + PathFormat.format(savePath));
|
||||||
storageState.putInfo("type", suffix);
|
storageState.putInfo("type", suffix);
|
||||||
storageState.putInfo("original", originFileName + suffix);
|
storageState.putInfo("original", originFileName + suffix);
|
||||||
|
|
||||||
|
// UEditor上传文件成功后调用事件
|
||||||
|
StorageManager.uploadFileSuccess(physicalPath, storageState);
|
||||||
}
|
}
|
||||||
|
|
||||||
return storageState;
|
return storageState;
|
||||||
|
|||||||
@@ -210,4 +210,14 @@ public class StorageManager {
|
|||||||
|
|
||||||
return new BaseState(true);
|
return new BaseState(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UEditor上传文件成功后调用事件
|
||||||
|
* @param physicalPath 上传文件实际路径
|
||||||
|
* @param storageState.url 返回到客户端的文件访问地址
|
||||||
|
*/
|
||||||
|
public static void uploadFileSuccess(String physicalPath, State storageState){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,8 +204,8 @@
|
|||||||
<#form:fileupload id="uploadImage2" returnPath="true"
|
<#form:fileupload id="uploadImage2" returnPath="true"
|
||||||
filePathInputId="uploadImage2Path" fileNameInputId="uploadImage2Name"
|
filePathInputId="uploadImage2Path" fileNameInputId="uploadImage2Name"
|
||||||
uploadType="image" readonly="false" maxUploadNum="3" isMini="false"/>
|
uploadType="image" readonly="false" maxUploadNum="3" isMini="false"/>
|
||||||
<#form:input name="uploadImage2Path" 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" class="form-control"/>
|
<#form:input name="uploadImage2Name" value="0 (1).png|0 (2).png" class="form-control"/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user