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文档并在线浏览的更多相关文章

  1. 自动把动态的jsp页面(或静态html)生成PDF文档,并且上传至服务器

    置顶2017年11月06日 14:41:04 阅读数:2311 这几天,任务中有一个难点是把一个打印页面自动给生成PDF文档,并且上传至服务器,然而公司框架只有手动上传文档,打印时可以保存为PDF在本 ...

  2. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  3. C#生成PDF文档,读取TXT文件内容

    using System.IO;using iTextSharp.text;using iTextSharp.text.pdf; //需要在项目里引用ICSharpCode.SharpZipLib.d ...

  4. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  5. ABBYY PDF Transformer+从文件选项中创建PDF文档的教程

    可使用OCR文字识别软件ABBYY PDF Transformer+从Microsoft Word.Microsoft Excel.Microsoft PowerPoint.HTML.RTF.Micr ...

  6. 怎么保护PDF文档和扫描文件里的机密信息

    从事商务工作的人,必然要处理带有机密信息的文档,需要分享这些文档的时候,如何谨慎小心地对待那些机密信息,说到底还是取决于自己.分享文档的目的不同,对文档的保护类型和级别也不一样.例如,只有授权的读者才 ...

  7. java将office文档pdf文档转换成swf文件在线预览

    第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...

  8. 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】

    iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  9. 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 打开需要 ...

随机推荐

  1. 高级组件——分割面板JSplitPane

    分割面板:JSplitPane(方向),默认水平分割.JSplitPane.HORIZONTAL_SPLIT,JSplitPane.VERTICAL_SPLITJSplitPane(方向,boolea ...

  2. MySql 5.7.23安装

    1.首先上MySql的官网下载  https://dev.mysql.com/downloads/mysql/ 选择源码包: 1. 新建/usr/local/src目录,保存下载的各类安装包 1 mk ...

  3. python自动化开发-[第十二天]-前端Css

    CSS基本语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. selector { property: value; property: value; ... property: v ...

  4. 删除mysql数据库中表分区数据

    删除mysql数据库中表分区数据 zabbix 几个大表创建了分区,由于磁盘空间告警,特将3月前的分区给予删除. 1.查看表的数据占用磁盘空间情况 2.登录mysql中,查看表的分区情况. 3.删除表 ...

  5. Linux记录-CPU指标介绍

    在linux的系统维护中,可能需要经常查看cpu使用率,分析系统整体的运行情况.而监控CPU的性能一般包括以下3点:运行队列.CPU使用率和上下文切换. 对于每一个CPU来说运行队列最好不要超过3,例 ...

  6. SpringCloud微服务之跨服务调用后端接口

    SpringCloud微服务系列博客: SpringCloud微服务之快速搭建EurekaServer:https://blog.csdn.net/egg1996911/article/details ...

  7. HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)

    http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...

  8. 解决centos7命令行中文乱码

    -------------centos7解决中文乱码问题[root@localhost ~]# cat /etc/locale.conf LANG=en_US.UTF-8[root@localhost ...

  9. node的优缺点及应用场景

    Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎(V8引擎执行Javascript的速度非常快,性能非常好) 可以说node是运行在服务器端V8引擎上的Ja ...

  10. GIT与SVN的比较

    Git: 1. 分布式版本控制2. 不需要网络,本地就可以操作版本控制3. 可以是公用的,可以分享4. 不依赖于中央服务器,即使服务器有问题也不会有影响5. 传输方式不一样,git要比svn快很多6. ...