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. Elastic 开发篇 javaAPI(4)

    1.解决精确匹配问题,如果不加配置,搜索农大,会搜出“农”“大”这两个字的匹配,我们要的是“农大”,那么好了,加上一个条件搞定: MatchQuery.Type.PHRASE 完整代码: packag ...

  2. tar压缩解压文件

    查看visualization1.5.tar.gz 压缩包里面的内容: $ tar -tf visualization1.5.tar.gz 解压指定文件JavascriptVisualRelease/ ...

  3. Vue(基础七)_webpack打包工具用法(上)

    一.前言 1.webpack原理 二.主要内容 1.webpack原理: (1)官网图:我们的项目有多个js, css文件的时候还需要考虑先引入哪一个后引入哪一个,因为这些js文件是相互依赖的,web ...

  4. http协议中的请求方式

    get:获取url传的查询字符串(action=show)表单和连接的url中传的值.容量2K左右. post:以post方式提交,获取表单和连接的url中传的值.容量8M左右. delete:删除某 ...

  5. C++: 带参数回调函数和不带参数的回调函数;

    在C++中,回调函数的应用比较广泛且重要. 通过传递函数指针到其他地方,能够实现远程回调的作用,能够实现远程调用而不需要事件触发信号或者其他机制来实现,方便而快捷: 首先,回调函数有两种形式:  静态 ...

  6. CodeForces992E 二分 + 树状数组(线段树)

    http://codeforces.com/problemset/problem/992/E 题意:给定一个序列 ai​ ,记其前缀和序列为 si​ ,有 q 个询问,每次单点修改,询问是否存在一个  ...

  7. golang rpc介绍

    rpc包提供了通过网络或其他I/O连接对一个对象的导出方法的访问.服务端注册一个对象,使它作为一个服务被暴露,服务的名字是该对象的类型名.注册之后,对象的导出方法就可以被远程访问.服务端可以注册多个不 ...

  8. GlusterFS 分布式文件系统的使用入门-管理GlusterFS卷

    GlusterFS 分布式文件系统的使用入门-管理GlusterFS卷 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.卷的扩容 您可以根据需要在群集联机且可用时扩展卷.例如,您 ...

  9. es6模块化导入导出

    模块化指的就是将一个大程序拆分成若干个互相依赖的小文件,然后在用简单的方法拼装起来. 在 ES6 之前,JS没有模块化系统,社区制定了一些模块加载方案 最主要的有 CommonJS(Asynchron ...

  10. 常用Java数据库连接池

    概述 在这里所谓的数据库连接是指通过网络协议与数据库服务之间建立的TCP连接.通常,与数据库服务进行通信的网络协议无需由应用程序本身实现,原因有三: 实现复杂度大,需要充分理解和掌握相应的通信协议. ...