TIFF属于一种高清图片格式,分辨率非常高,在CKFinder上传中会出现问题,查阅CF源码发现使用ImageIO上传图片数据,一般的JPG等格式是完全没问题的,但是在上传TIFF格式且提交较大时(达到1M以上就会报错,但是其他格式的完全不会)会报错内存溢出,但不是显示具体错误信息。

我们先采用两种方式解决问题:

第一种方式,就是修改CF的源代码,在上传TIFF格式图片的时候,保留TIFF格式的图片,同时将其转换为JPG格式的图片,这个是没有任何问题的。

第二种方式,尚未实现,需要解决CF缩略图模式下TIFF不支持预览问题。

CKFinder版本:2.3

解决方法一:TIFF数据格式图片转换JPG格式,同时保留原格式TIFF

WEB-INF/ckfinder.xml,添加tif格式,允许上传tif格式图片数据。

%BASE_URL%images/

%BASE_DIR%images

100M

bmp,gif,jpeg,jpg,png,tif

下载CKFinder 2.3版本的java源代码(注意对应Jeesite里面的版本),地址如下:

https://ckeditor.com/ckeditor-4/ckfinder/changelog/

直接将整个类文件复制到工程中,包含完整的包路径。

这里只要关注ImageIO这个方法,因为只要涉及到此方法都会报错,所以在ImageIO运行前就做格式判断,并强制取消对TIFF格式的数据验证,在createTmpThumb方法中,这里对图片进行上传操作,所以单独对TIFF格式数据进行格式转换成JPG进行存储即可。

PS:目前只做简单的处理,只能说勉强能用,后续再对第二种方法进行研究。

/*

* CKFinder

* ========

* http://ckfinder.com

* Copyright (C) 2007-2012, CKSource - Frederico Knabben. All rights reserved.

*

* The software, this file and its contents are subject to the CKFinder

* License. Please read the license.txt file before using, installing, copying,

* modifying or distribute this file or part of its contents. The contents of

* this file is part of the Source Code of CKFinder.

*/

package com.ckfinder.connector.utils;

import java.awt.Dimension;

import java.awt.image.BufferedImage;

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Arrays;

import java.util.List;

import javax.imageio.ImageIO;

import javax.media.jai.JAI;

import javax.media.jai.PlanarImage;

import net.coobird.thumbnailator.Thumbnails;

import org.apache.commons.fileupload.FileItem;

import com.ckfinder.connector.configuration.IConfiguration;

import com.sun.media.jai.codec.FileCacheSeekableStream;

import com.sun.media.jai.codec.ImageCodec;

import com.sun.media.jai.codec.ImageEncoder;

import com.sun.media.jai.codec.JPEGEncodeParam;

import com.sun.media.jai.codec.TIFFEncodeParam;

/**

* Utils to operate on images.

*/

public class ImageUtils {

/**

* allowed image extensions.

*/

private static final String[] ALLOWED_EXT = { "gif", "jpeg", "jpg", "png",

"psd", "bmp", "tiff", "tif", "swc", "jpc", "jp2", "jpx", "jb2",

"xbm", "wbmp" };

private static final int MAX_BUFF_SIZE = 1024;

private static boolean prefix_tiff = false;

/**

* Resizes the image and writes it to the disk.

*

* @param sourceImage

* orginal image file.

* @param width

* requested width

* @param height

* requested height

* @param quality

* requested destenation file quality

* @param destFile

* file to write to

* @throws IOException

* when error occurs.

*/

private static void resizeImage(final BufferedImage sourceImage, final int width,

final int height, final float quality,

final File destFile) throws IOException {

try {

Thumbnails.of(sourceImage).size(width, height).keepAspectRatio(false)

.outputQuality(quality).toFile(destFile);

// for some special files outputQuality couses error:

//IllegalStateException inner Thumbnailator jar. When exception is thrown

// image is resized without quality

// When http://code.google.com/p/thumbnailator/issues/detail?id=9

// will be fixed this try catch can be deleted. Only:

//Thumbnails.of(sourceImage).size(width, height).keepAspectRatio(false)

//.outputQuality(quality).toFile(destFile);

// should remain.

} catch (IllegalStateException e) {

Thumbnails.of(sourceImage).size(width, height).keepAspectRatio(false)

.toFile(destFile);

}

}

/**

* create thumb file.

*

* @param orginFile

* orgin image file.

* @param file

* file to save thumb

* @param conf

* connector configuration

* @throws IOException

* when error occurs.

*/

public static void createThumb(final File orginFile, final File file,

final IConfiguration conf) throws IOException {

BufferedImage image = ImageIO.read(orginFile);

if (image != null) {

Dimension dimension = createThumbDimension(image,

conf.getMaxThumbWidth(), conf.getMaxThumbHeight());

FileUtils.createPath(file, conf, true);

if (image.getHeight() == dimension.height

&& image.getWidth() == dimension.width) {

writeUntouchedImage(orginFile, file);

} else {

resizeImage(image, dimension.width, dimension.height,

conf.getThumbsQuality(), file);

}

} else {

if (conf.isDebugMode()) {

throw new IOException("Wrong image file");

}

}

}

/**

* Uploads image and if the image size is larger than maximum allowed it

* resizes the image.

*

* @param stream

* input stream.

* @param file

* file name

* @param fileName

* name of file

* @param conf

* connector configuration

* @throws IOException

* when error occurs.

* 20180105 update method - upload 2 files, one of JPEG, one of TIFF

*/

public static void createTmpThumb(final InputStream stream,

final File file, final String fileName, final IConfiguration conf)

throws IOException {

if(prefix_tiff){

//System.out.println(file.getAbsolutePath());

//System.out.println(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")));

FileCacheSeekableStream stream1 = new FileCacheSeekableStream(stream);

PlanarImage in = JAI.create("stream", stream1);

String newFilePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".jpg";

OutputStream osJPEG = new FileOutputStream(newFilePath);

JPEGEncodeParam JPEGparam = new JPEGEncodeParam();

ImageEncoder encJPEG = ImageCodec.createImageEncoder("JPEG", osJPEG, JPEGparam);

OutputStream osTIFF = new FileOutputStream(file);

TIFFEncodeParam TIFFparam = new TIFFEncodeParam();

TIFFparam.setCompression(TIFFEncodeParam.COMPRESSION_NONE);

ImageEncoder encTIFF = ImageCodec.createImageEncoder("TIFF", osTIFF, TIFFparam);

try {

encJPEG.encode(in);

osJPEG.flush();

osJPEG.close();

encTIFF.encode(in);

osTIFF.flush();

osTIFF.close();

stream.close();

} catch (IOException e) {

throw new IOException("Wrong file");

}

} else {

BufferedInputStream bufferedIS = new BufferedInputStream(stream);

bufferedIS.mark(Integer.MAX_VALUE);

BufferedImage image = ImageIO.read(bufferedIS);

if (image == null) {

throw new IOException("Wrong file");

}

Dimension dimension = createThumbDimension(image, conf.getImgWidth(),

conf.getImgHeight());

if (image.getHeight() == dimension.height

&& image.getWidth() == dimension.width) {

bufferedIS.reset();

writeUntouchedImage(bufferedIS, file);

} else {

resizeImage(image, dimension.width, dimension.height,

conf.getImgQuality(), file);

}

stream.close();

}

}

/**

* Creates image file with fixed width and height.

*

* @param sourceFile

* input file

* @param destFile

* file to save

* @param width

* image width

* @param height

* image height

* @param quality

* image quality

* @throws IOException

* when error occurs.

*/

public static void createResizedImage(final File sourceFile,

final File destFile, final int width, final int height,

final float quality) throws IOException {

BufferedImage image = ImageIO.read(sourceFile);

Dimension dimension = new Dimension(width, height);

if (image.getHeight() == dimension.height

&& image.getWidth() == dimension.width) {

writeUntouchedImage(sourceFile, destFile);

} else {

resizeImage(image, dimension.width, dimension.height, quality,

destFile);

}

}

/**

* creates dimension of thumb.

*

* @param image

* orginal image.

* @param maxWidth

* max thumb width

* @param maxHeight

* max thumb height

* @return dimension of thumb image.

*/

private static Dimension createThumbDimension(final BufferedImage image,

final int maxWidth, final int maxHeight) {

Dimension dimension = new Dimension();

if (image.getWidth() >= image.getHeight()) {

if (image.getWidth() >= maxWidth) {

dimension.width = maxWidth;

dimension.height = Math.round(((float) maxWidth / image

.getWidth()) * image.getHeight());

} else {

dimension.height = image.getHeight();

dimension.width = image.getWidth();

}

} else {

if (image.getHeight() >= maxHeight) {

dimension.height = maxHeight;

dimension.width = Math.round((((float) maxHeight / image

.getHeight()) * image.getWidth()));

} else {

dimension.height = image.getHeight();

dimension.width = image.getWidth();

}

}

return dimension;

}

/**

* checks if file is image.

*

* @param file

* file to check

* @return true if file is image.

*/

public static boolean isImage(final File file) {

List list = Arrays.asList(ALLOWED_EXT);

String fileExt = null;

if (file != null) {

fileExt = FileUtils.getFileExtension(file.getName().toLowerCase());

return (fileExt != null) ? list.contains(fileExt) : false;

} else {

return false;

}

}

/**

* check if image size isn't bigger then bigest allowed.

*

* @param stream

* temp file input stream.

* @param conf

* connector configuration.

* @return true if image size isn't bigger then bigest allowe.

* @throws IOException

* when error occurs during reading image.

*/

public static boolean checkImageSize(final InputStream stream,

final IConfiguration conf) throws IOException {

if(!prefix_tiff){

BufferedImage bi = ImageIO.read(stream);

stream.close();

if (bi == null) {

return false;

}

if (bi.getHeight() > conf.getImgHeight()

|| bi.getWidth() > conf.getImgWidth()) {

return false;

}

}

return true;

}

/**

* checks if image file is image.

*

* @param item

* file upload item

* @return true if file is image.

*/

public static boolean checkImageFile(final FileItem item) {

BufferedImage bi;

InputStream is = null;

if(item.getContentType().equals("image/tiff")){

prefix_tiff = true;

return true;

} else {

prefix_tiff = false;

try {

is = item.getInputStream();

bi = ImageIO.read(is);

} catch (IOException e) {

return false;

} finally {

if (is != null){

try { is.close(); } catch (Exception e) {}

}

}

return (bi != null);

}

}

/**

* writes unchanged file to disk.

*

* @param sourceFile

* - file to read from

*

* @param destFile

* - file to write to

*

* @throws IOException

* when error occurs.

*/

private static void writeUntouchedImage(final File sourceFile, final File destFile)

throws IOException {

FileInputStream fileIS = new FileInputStream(sourceFile);

writeUntouchedImage(fileIS, destFile);

}

/**

* writes unchanged file to disk.

*

* @param stream

* - stream to read the file from

*

* @param destFile

* - file to write to

*

* @throws IOException

* when error occurs.

*/

private static void writeUntouchedImage(final InputStream stream, final File destFile)

throws IOException {

ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();

byte[] buffer = new byte[MAX_BUFF_SIZE];

int readNum = -1;

while ((readNum = stream.read(buffer)) != -1) {

byteArrayOS.write(buffer, 0, readNum);

}

byte[] bytes = byteArrayOS.toByteArray();

byteArrayOS.close();

FileOutputStream fileOS = new FileOutputStream(destFile);

fileOS.write(bytes);

fileOS.flush();

fileOS.close();

}

}

java ckfinder 图片重命名,CKFinder上传TIFF格式图片相关推荐

  1. django如何给上传的图片重命名(给上传文件重命名)

    1.先在你项目中添加一个文件夹如:system 在文件夹下添加__init__.py 和storage.py文件,并在storage.py中添加如下代码: # -*- coding: UTF-8 -* ...

  2. php 重命名 漏洞,上传漏洞[汇总]

    最后必然失去的希望就是毒药啊. 个人对上传漏洞的理解在第一章节就说过了,这里稍微复述一下.上传漏洞按个人理解分两种,第一种是代码层的.即代码过滤不严格,或者使用客户端本地校验.第二种是服务层的,比如i ...

  3. svg上传服务器无法显示,让WordPress支持上传SVG格式图片并显示在媒体库中的方法...

    让WordPress支持上传SVG格式图片并显示在媒体库中的方法 发布时间:2020-12-11 14:18:12 来源:亿速云 阅读:167 作者:小新 这篇文章将为大家详细讲解有关让WordPre ...

  4. JAVA上传tiff格式文件

    文章目录 Java上传tiff格式文件 Java上传tiff格式文件 加入pom依赖 <dependency><groupId>com.twelvemonkeys.imagei ...

  5. Java GUI编程:swing实现上传tiff文件至hdfs功能

    上传tiff文件至hdfs pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...

  6. asP上传服务器文件闪退,aspupload文件重命名及上传进度条的解决方法附代码

    发现还没有aspupload这个组件的,这两样功能的解决方案,现把我的改进方案写在这里!谢谢 关于aspupload上传组件,文件重命名,进度条的问题解决方案! 共用到4个文件,分别是1.asp,2. ...

  7. File Struct 上传JPG格式图片变成tmp 服务器上传文件名字随机起

    今天在写ssh框架中上传图片的代码,解决了一部分,然后卡在了图片上传之后的文件名不一致,就像随机取的.从我上传的jpg格式变成了tmp. 格式是不对了,但是文件内容并没有发生变化,说明文件是上传成功了 ...

  8. SE78上传BMP格式图片出错,出错信息提示上传的不是BMP格式图片

    如下图,导入文件为一BMP图片(红框部分), 然后点击确认键(黑框部分). 结果却出错,提示不是BMP文件,如下图红框部分. 很纳闷明明是BMP怎么就提示说不是BMP呢?感觉应该是上传的图片有问题,所 ...

  9. php 上传二进制图片,关于PHP CURL上传二进制流图片

    推荐:"PHP视频教程" 前言项目中的模块数据由PHP爬虫更新.检测到新图片需要上传到跨区域CDN回传服务器(静态资源服务器),服务器管理器只提供上传API 解决方案1.本地保存图 ...

  10. uniapp 上传指定格式图片

    只需要在上传成功的时候做一个判断 uni.uploadFile({url: apiBaseUrl + "/api/file/xxx", // 后端api接口filePath: te ...

最新文章

  1. Hexo 个人博客 SEO 优化(3):改造你的博客,提升搜索引擎排名
  2. 压缩感知及应用 源代码_【DMD应用】基于压缩感知超分辨鬼成像
  3. oracle中怎样查询用户权限
  4. 维度及长度均可任意变形的动态数组
  5. Java循环案例-银行存钱问题
  6. [转]各种字符集和编码详解
  7. smarty引擎之练习
  8. mysql第七章课后答案_mysql核心内幕第七章-查询解析与优化器
  9. PolarDB · 新品介绍 · 深入了解阿里云新一代产品 PolarDB
  10. Open3d之非阻塞可视化
  11. Qt编写OpenMP程序--双循环
  12. Windows下安装postgresql_psycopg2时出现 Unabled to find vcvarsall.bat 的解决办法
  13. GreenPlum数据库调研及架构介绍
  14. Calico BGP 功能介绍:实现
  15. 计算机操作系统轮转算法代码,实验四 时间片轮转调度算法
  16. 怎么看电脑的hdmi是输出还是输入_怎么看电脑显示器有没有hdmi接口
  17. 禅道 10.0.alpha 版本发布,全新的界面和交互体验
  18. 基于MSGEQ7的音乐节奏灯超详细适合入门
  19. SecureCRT的使用方法和技巧(二) 常用指令
  20. 计算机电源怎么设置玩游戏不卡,端游绝地求生怎么设置不卡

热门文章

  1. 从计算机硬件系统来看 不管计算机配置,计算机组装与维修章节练习题201311
  2. IIS优化,支持10万并发
  3. linux 下载工具
  4. 小学教育专业语文方向毕业论文怎么选题?
  5. 2021年中国宽带接入情况、用户规模及使用情况分析[图]
  6. WIN7家庭版升级到旗舰版操作
  7. CVTE实习应聘经验
  8. Linux文件颜色所代表的含义
  9. macOS 开发 - 使用 ScreenSaverView 制作屏幕保护程序
  10. word分栏对齐方法