* tif图片预览,改为支持转换为jpg后预览(JAI支持);加入tif->jpg->pdf转换后,以pdf格式预览,此种模式可完美支持打印纸张自适应。
This commit is contained in:
BIN
server/lib/jai_codec-1.1.3.jar
Normal file
BIN
server/lib/jai_codec-1.1.3.jar
Normal file
Binary file not shown.
BIN
server/lib/jai_core-1.1.2-beta.jar
Normal file
BIN
server/lib/jai_core-1.1.2-beta.jar
Normal file
Binary file not shown.
@ -250,6 +250,26 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.lowagie</groupId>
|
||||||
|
<artifactId>itext</artifactId>
|
||||||
|
<version>2.1.7</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.media</groupId>
|
||||||
|
<artifactId>jai_core</artifactId>
|
||||||
|
<version>1.1.2-beta</version>
|
||||||
|
<systemPath>${basedir}/lib/jai_core-1.1.2-beta.jar</systemPath>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.media</groupId>
|
||||||
|
<artifactId>jai_codec</artifactId>
|
||||||
|
<version>1.1.3</version>
|
||||||
|
<systemPath>${basedir}/lib/jai_codec-1.1.3.jar</systemPath>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -67,7 +67,7 @@ media.convert.disable = ${KK_MEDIA_CONVERT_DISABLE:false}
|
|||||||
#支持转换的视频类型
|
#支持转换的视频类型
|
||||||
convertMedias = ${KK_CONVERTMEDIAS:avi,mov,wmv,mkv,3gp,rm}
|
convertMedias = ${KK_CONVERTMEDIAS:avi,mov,wmv,mkv,3gp,rm}
|
||||||
#office类型文档(word ppt)样式,默认为图片(image),可配置为pdf(预览时也有按钮切换)
|
#office类型文档(word ppt)样式,默认为图片(image),可配置为pdf(预览时也有按钮切换)
|
||||||
office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:image}
|
office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:pdf}
|
||||||
#是否关闭office预览切换开关,默认为false,可配置为true关闭
|
#是否关闭office预览切换开关,默认为false,可配置为true关闭
|
||||||
office.preview.switch.disabled = ${KK_OFFICE_PREVIEW_SWITCH_DISABLED:false}
|
office.preview.switch.disabled = ${KK_OFFICE_PREVIEW_SWITCH_DISABLED:false}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,6 @@ public interface FilePreview {
|
|||||||
String COMPRESS_FILE_PREVIEW_PAGE = "compress";
|
String COMPRESS_FILE_PREVIEW_PAGE = "compress";
|
||||||
String MEDIA_FILE_PREVIEW_PAGE = "media";
|
String MEDIA_FILE_PREVIEW_PAGE = "media";
|
||||||
String PICTURE_FILE_PREVIEW_PAGE = "picture";
|
String PICTURE_FILE_PREVIEW_PAGE = "picture";
|
||||||
String TIFF_FILE_PREVIEW_PAGE = "tiff";
|
|
||||||
String OFD_FILE_PREVIEW_PAGE = "ofd";
|
String OFD_FILE_PREVIEW_PAGE = "ofd";
|
||||||
String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture";
|
String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture";
|
||||||
String TXT_FILE_PREVIEW_PAGE = "txt";
|
String TXT_FILE_PREVIEW_PAGE = "txt";
|
||||||
|
|||||||
@ -1,11 +1,20 @@
|
|||||||
package cn.keking.service.impl;
|
package cn.keking.service.impl;
|
||||||
|
|
||||||
|
import cn.keking.config.ConfigConstants;
|
||||||
import cn.keking.model.FileAttribute;
|
import cn.keking.model.FileAttribute;
|
||||||
|
import cn.keking.model.FileType;
|
||||||
|
import cn.keking.model.ReturnResponse;
|
||||||
import cn.keking.service.FilePreview;
|
import cn.keking.service.FilePreview;
|
||||||
import cn.keking.utils.WebUtils;
|
import cn.keking.utils.ConvertPicUtil;
|
||||||
|
import cn.keking.utils.DownloadUtils;
|
||||||
|
import cn.keking.web.filter.BaseUrlFilter;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tiff 图片文件处理
|
* tiff 图片文件处理
|
||||||
@ -16,9 +25,8 @@ import org.springframework.util.StringUtils;
|
|||||||
public class TiffFilePreviewImpl implements FilePreview {
|
public class TiffFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
private final PictureFilePreviewImpl pictureFilePreview;
|
private final PictureFilePreviewImpl pictureFilePreview;
|
||||||
private static final String INITIALIZE_MEMORY_SIZE = "initializeMemorySize";
|
|
||||||
//默认初始化 50MB 内存
|
private final String fileDir = ConfigConstants.getFileDir();
|
||||||
private static final long INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT = 1024L * 1024 * 50;
|
|
||||||
|
|
||||||
public TiffFilePreviewImpl(PictureFilePreviewImpl pictureFilePreview) {
|
public TiffFilePreviewImpl(PictureFilePreviewImpl pictureFilePreview) {
|
||||||
this.pictureFilePreview = pictureFilePreview;
|
this.pictureFilePreview = pictureFilePreview;
|
||||||
@ -26,13 +34,55 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
pictureFilePreview.filePreviewHandle(url,model,fileAttribute);
|
String inputFileName = url.substring(url.lastIndexOf("/") + 1);
|
||||||
String fileSize = WebUtils.getUrlParameterReg(url,INITIALIZE_MEMORY_SIZE);
|
String inputFileExt = inputFileName.substring(inputFileName.lastIndexOf(".") + 1);
|
||||||
if(StringUtils.hasText(fileSize)){
|
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
||||||
model.addAttribute(INITIALIZE_MEMORY_SIZE,fileSize);
|
String tiffFileName = uuid + "." + inputFileExt;
|
||||||
}else {
|
|
||||||
model.addAttribute(INITIALIZE_MEMORY_SIZE,Long.toString(INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT));
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, tiffFileName);
|
||||||
}
|
if (response.isFailure()) {
|
||||||
return TIFF_FILE_PREVIEW_PAGE;
|
return NOT_SUPPORTED_FILE_PAGE;
|
||||||
|
}
|
||||||
|
String strTiffPath = response.getContent();
|
||||||
|
|
||||||
|
File fileTiff = new File(strTiffPath);
|
||||||
|
|
||||||
|
File fileJpg = ConvertPicUtil.convertPic2Jpg(strTiffPath, fileDir + uuid + ".jpg");
|
||||||
|
|
||||||
|
if(fileJpg.exists()){
|
||||||
|
// 转换后的tif没用了,可以删掉了
|
||||||
|
fileTiff.delete();
|
||||||
|
|
||||||
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||||
|
if("pdf".equalsIgnoreCase(fileAttribute.getOfficePreviewType())){
|
||||||
|
File filePdf = ConvertPicUtil.convertJpg2Pdf(fileDir + uuid + ".jpg", fileDir + uuid + ".pdf");
|
||||||
|
if(filePdf.exists()){
|
||||||
|
// 转换后的jpg没用了,可以删掉了
|
||||||
|
fileJpg.delete();
|
||||||
|
|
||||||
|
String pdfUrl = baseUrl + uuid + ".pdf";
|
||||||
|
model.addAttribute("pdfUrl", pdfUrl);
|
||||||
|
|
||||||
|
return PDF_FILE_PREVIEW_PAGE;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
String jpgUrl = baseUrl + uuid + ".jpg";
|
||||||
|
|
||||||
|
fileAttribute.setName(uuid + ".jpg");
|
||||||
|
fileAttribute.setType(FileType.PICTURE);
|
||||||
|
fileAttribute.setSuffix("jpg");
|
||||||
|
fileAttribute.setUrl(jpgUrl);
|
||||||
|
|
||||||
|
List<String> imgUrls = new ArrayList<>();
|
||||||
|
imgUrls.add(jpgUrl);
|
||||||
|
|
||||||
|
model.addAttribute("imgUrls", imgUrls);
|
||||||
|
model.addAttribute("currentUrl", jpgUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return PICTURE_FILE_PREVIEW_PAGE;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
107
server/src/main/java/cn/keking/utils/ConvertPicUtil.java
Normal file
107
server/src/main/java/cn/keking/utils/ConvertPicUtil.java
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
package cn.keking.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import com.lowagie.text.Document;
|
||||||
|
import com.lowagie.text.Image;
|
||||||
|
import com.lowagie.text.Rectangle;
|
||||||
|
import com.lowagie.text.pdf.PdfWriter;
|
||||||
|
import com.sun.media.jai.codec.ImageCodec;
|
||||||
|
import com.sun.media.jai.codec.ImageEncoder;
|
||||||
|
import com.sun.media.jai.codec.JPEGEncodeParam;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.media.jai.JAI;
|
||||||
|
import javax.media.jai.RenderedOp;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ConvertPicUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片 转 JPG。
|
||||||
|
* 支持输入格式如下:BMP、GIF、FlashPix、JPEG、PNG、PMN、TIFF、WBMP
|
||||||
|
* @param strInputFile 输入文件的路径和文件名
|
||||||
|
* @param strOutputFile 输出文件的路径和文件名
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static File convertPic2Jpg(String strInputFile, String strOutputFile) {
|
||||||
|
// 读取源图片文件
|
||||||
|
RenderedOp roInput = JAI.create("fileload", strInputFile);
|
||||||
|
try {
|
||||||
|
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
|
||||||
|
File fileJpgPath = new File(strOutputFile.substring(0, strOutputFile.lastIndexOf("/")));
|
||||||
|
if(!fileJpgPath.exists()){
|
||||||
|
fileJpgPath.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
File fileJpg=new File(strOutputFile);
|
||||||
|
OutputStream ops = new FileOutputStream(fileJpg);
|
||||||
|
// 文件存储输出流
|
||||||
|
JPEGEncodeParam param = new JPEGEncodeParam();
|
||||||
|
ImageEncoder image = ImageCodec.createImageEncoder("JPEG", ops,
|
||||||
|
param); // 指定输出格式
|
||||||
|
// 解析输出流进行输出
|
||||||
|
image.encode(roInput);
|
||||||
|
// 关闭流
|
||||||
|
ops.close();
|
||||||
|
|
||||||
|
return fileJpg;
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将Jpg图片转换为Pdf文件
|
||||||
|
* @param strJpgFile 输入的jpg的路径和文件名
|
||||||
|
* @param strPdfFile 输出的pdf的路径和文件名
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static File convertJpg2Pdf(String strJpgFile, String strPdfFile) {
|
||||||
|
Document document = new Document();
|
||||||
|
// 设置文档页边距
|
||||||
|
document.setMargins(0,0,0,0);
|
||||||
|
FileOutputStream fos = null;
|
||||||
|
try {
|
||||||
|
fos = new FileOutputStream(strPdfFile);
|
||||||
|
PdfWriter.getInstance(document, fos);
|
||||||
|
// 打开文档
|
||||||
|
document.open();
|
||||||
|
// 获取图片的宽高
|
||||||
|
Image image = Image.getInstance(strJpgFile);
|
||||||
|
float floatImageHeight=image.getScaledHeight();
|
||||||
|
float floatImageWidth=image.getScaledWidth();
|
||||||
|
// 设置页面宽高与图片一致
|
||||||
|
Rectangle rectangle = new Rectangle(floatImageWidth, floatImageHeight);
|
||||||
|
document.setPageSize(rectangle);
|
||||||
|
// 图片居中
|
||||||
|
image.setAlignment(Image.ALIGN_CENTER);
|
||||||
|
//新建一页添加图片
|
||||||
|
document.newPage();
|
||||||
|
document.add(image);
|
||||||
|
} catch (Exception ioe) {
|
||||||
|
ioe.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
//关闭文档
|
||||||
|
document.close();
|
||||||
|
try {
|
||||||
|
fos.flush();
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
File filePDF = new File(strPdfFile);
|
||||||
|
return filePDF;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user