根据传入的文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览
1.通过百度编辑器上传pdf文档等附件时,在上传方法中将返回的url进行设定,以达到后期点击后可进行浏览的效果:
public static final State save(HttpServletRequest request,
Map<String, Object> conf) {
FileItemStream fileStream = null;
boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null; if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
} ServletFileUpload upload = new ServletFileUpload(
new DiskFileItemFactory()); if ( isAjaxUpload ) {
upload.setHeaderEncoding( "UTF-8" );
} try {
FileItemIterator iterator = upload.getItemIterator(request); while (iterator.hasNext()) {
fileStream = iterator.next(); if (!fileStream.isFormField())
break;
fileStream = null;
} if (fileStream == null) {
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
} String savePath = (String) conf.get("savePath");
String originFileName = fileStream.getName();
String suffix = FileType.getSuffixByFilename(originFileName);
//将名字重复名,以免重复导致图片被覆盖并且防由于特殊字符导致无法上传
originFileName = DateFormatHelper.getNowTimeStr("yyyyMMddHHmmssSSS")+CodeManager.getRandomNum(10);//originFileName.substring(0,originFileName.length() - suffix.length());
savePath = savePath + suffix; long maxSize = ((Long) conf.get("maxSize")).longValue(); if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
} savePath = PathFormat.parse(savePath, originFileName); String physicalPath = (String) conf.get("rootPath") + savePath; InputStream is = fileStream.openStream(); FileModel filemodel=uploadFiles(is, originFileName + suffix);
System.out.println(filemodel.toString()); State storageState = new BaseState(true);
String temp=filemodel.getRemotePaths();
String temp2=temp.substring(getCharacterPosition(temp), temp.length());
System.out.println(temp2);
storageState.putInfo( "title", filemodel.getFileName()); if (storageState.isSuccess()) {
//storageState.putInfo("url", temp2);//PathFormat.format(savePath)
storageState.putInfo("url", getDownloadPath(suffix, originFileName + suffix, temp2));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
} return storageState;
} catch (FileUploadException e) {
log.error("FileUploadException",e);
return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
} catch (IOException e) {
log.error("IOException",e);
}
return new BaseState(false, AppInfo.IO_ERROR);
}
设定返回url方法:
/**
* 返回可在线浏览pdf的url
* @param suffix
* @param fileName
* @param imgUrl
* @return
*/
private static String getDownloadPath(String suffix,String fileName,String imgUrl){
String[] downFileSuffix = new String[]{".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml",".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso"};
List<String> list = Arrays.asList(downFileSuffix);
String returnUrl = imgUrl;
if(list.contains(suffix)){
returnUrl = "/browsePdf?enclosure="+fileName;
}
return returnUrl;
}
2.下边为在线浏览的控制层方法:
/**
* 在线浏览pdf
* @param request
* @param response
* hdf
* @throws Exception
*/
@RequestMapping(value = "/browsePdf", method = RequestMethod.GET)
public void browsePdf(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String enclosure = request.getParameter("enclosure");
// 在线浏览pdf
fileUpload.browsePdf(request, response, enclosure);
}
通过文件名获取流,将流以pdf的格式输出,注意:1.格式必须设置为pdf,2,需要将浏览的类型设置为:inline
/**
* 通过web应用来l浏览pdf文档
* @param request HttpServletRequest对象
* @param response HttpServletResponse对象
* @param fileName 真实的文件名字,也就是记录在File模块里面的名字
* Add by hdf when 2017.10.10
*/
public void browsePdf(HttpServletRequest request,
HttpServletResponse response, String fileName){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
//设置字符集
//response.setContentType("charset=UTF-8");
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8"); //根据文件名字在file记录里面获取相应的记录
//FileModel fm = fileup.getOneFileModel(fileName);
FileModel fm = fileService.getOneFileModel(fileName);
String mogilefsPath = fm.getRemotePaths();
//设置文件下载中,对话框显示的文件名字,也就是真实名字
response.setHeader("Content-disposition", "inline; filename="
+ new String(fm.getFileName().getBytes("gb2312"), "ISO8859-1")); //通过URL从Mogilefs去获取文件内容
URL url = new URL(mogilefsPath); bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead = 0;
//判断是否需要编码转换
int lastPointSite = fm.getFileName().lastIndexOf(".");
String fileType = fm.getFileName().substring(lastPointSite);
boolean needTrans = this.needTransCharacterEncoding.contains(fileType);
//流式获取内容并流式输出
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
if(needTrans){
String tempS = new String(buff,"utf-8");
bos.write(tempS.getBytes());
buff = new byte[1024];
}else{
bos.write(buff, 0, bytesRead);
}
}
if(bos !=null){
bos.close();
}
}catch(Exception err){
log.error(err);
// err.printStackTrace();
throw new LogException("file.downLoad", "downloadFile error,fileName="+fileName, err);
}finally{
try{
if(bis !=null){
bis.close();
}
}catch(Exception err){
log.error(err);
// err.printStackTrace();
}
try{
if(bos !=null){
bos.close();
}
}catch(Exception err){
log.error(err);
// err.printStackTrace();
}
}
}
根据传入的文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览的更多相关文章
- 自动把动态的jsp页面(或静态html)生成PDF文档,并且上传至服务器
置顶2017年11月06日 14:41:04 阅读数:2311 这几天,任务中有一个难点是把一个打印页面自动给生成PDF文档,并且上传至服务器,然而公司框架只有手动上传文档,打印时可以保存为PDF在本 ...
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- C#生成PDF文档,读取TXT文件内容
using System.IO;using iTextSharp.text;using iTextSharp.text.pdf; //需要在项目里引用ICSharpCode.SharpZipLib.d ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- ABBYY PDF Transformer+从文件选项中创建PDF文档的教程
可使用OCR文字识别软件ABBYY PDF Transformer+从Microsoft Word.Microsoft Excel.Microsoft PowerPoint.HTML.RTF.Micr ...
- 怎么保护PDF文档和扫描文件里的机密信息
从事商务工作的人,必然要处理带有机密信息的文档,需要分享这些文档的时候,如何谨慎小心地对待那些机密信息,说到底还是取决于自己.分享文档的目的不同,对文档的保护类型和级别也不一样.例如,只有授权的读者才 ...
- java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...
- 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】
iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- OrCAD Capture CIS 16.6 从PDF文档中提取引脚定义,实现快速地编辑Part的引脚名称
操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 工具2:Excel 工具3:Solid Converter 打开需要 ...
随机推荐
- IO 多路复用介绍
IO多路复用 概念说明 在进行解释之前,首先要说明几个概念: 用户空间和内核空间 进程切换 进程的阻塞 文件描述符 缓存 I/O 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系 ...
- xlrd(excel导入mysql数据库)
一.xlrd模块: python 对 excel 文件进行读操作 1.下载xlrd 并安装 http://pypi.python.org/pypi/xlrd wget https://pypi.pyt ...
- 用jQuery写的轮播图
效果图: GitHub地址:https://github.com/123456abcdefg/Javascript 大家可以下载源码查看. 与前一篇写的轮播图实现的效果一致,这个是用jQuery写的, ...
- qemu与libvirt编译与安装
qemu与libvirt编译与安装 一 .qemu编译前准备 以下链接为的本次编译环境为前提继续进行. http://www.cnblogs.com/grglym/p/7891253.html 1.安 ...
- docker swarm(当前官网示例)
介绍 Docker Swarm 是 Docker 公司推出的官方容器集群平台,基于 Go 语言实现 作为容器集群管理器,Swarm 最大的优势之一就是 100% 支持标准的 Docker API.各种 ...
- 关于Mysql的高级查询的操作
前言:作为一名后端的程序员操作数据库的能力是我们基本的技能,而连表查询是我们的这个技能的关键点所在.注意这里顾明思义是对数据的查询的操作 (一).联合查询(关键字union/union all) 什么 ...
- http-request详解
HTTP请求 请求数据格式 响应数据格式 request
- VScode-Common-Shortcut
Keyboard shortcuts for windows Keyboard Shortcut For Windows General Shortcut Commands Ctrl + Shift ...
- golang使用redis
redigo使用 手册地址:http://godoc.org/github.com/garyburd/redigo/redis github地址:https://github.com/garyburd ...
- npm与nrm
npm npm是Node.js 平台的默认包(模块依赖)管理工具 Node Package Manager nrm 一个npm的源管理器(管理工具) 允许快速的在 npm 源间切换 两者关系 npm是 ...