根据传入的文件名称动态从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 打开需要 ...
随机推荐
- Tomcat 启动时 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}
在Eclipse 中,启动Tomcat 时,出现: 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting proper ...
- linux环境java入门
1. 安装java开发环境 安装jre和jdk $ sudo apt-get install default-jre$ sudo apt-get install default-jdk 2. 设置环境 ...
- Luogu P4484 [BJWC2018]最长上升子序列
状压\(DP\)+打表,要命的题目..... 具体思路请参考这位巨佬的博客,本蒟蒻对这道题感到心力交瘁,决定不再作出补充.. 关键的要学习的是:对于排列问题,从左到右处理比较困难的话,考虑从小到大把数 ...
- python 发送邮件模板
##发送普通txt文件(与发送html邮件不同的是邮件内容设置里的type设置为text,下面代码为发送普通邮件的另一种方法) import smtplibimport stringfrom emai ...
- JDK动态代理和CGLIB代理的区别
一.原理区别: java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理. 而cglib动态代理是利用asm开源包,对代理对象类的class文件 ...
- java io系列20之 PipedReader和PipedWriter
本章,我们学习PipedReader和PipedWriter.它们和“PipedInputStream和PipedOutputStream”一样,都可以用于管道通信. PipedWriter 是字符管 ...
- NPOI使用教程附Helper
1 NPOI简介 1.1 NPOI是什么 NPOI是POI的.NET版本,POI是一套用Java写成的库,我们在开发中经常用到导入导出表格.文档的情况,NPOI能够帮助我们在没有安装微软Office的 ...
- CSS3笔记4
1.CSS3盒子模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- HTML第四耍 超链接标签
1.HTML 超链接(链接) 一.HTML 超链接 HTML中使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 超链接可以是一个字,一个词 ...
- MySQL忘记root密码的解决办法
# 1.停掉MySQL进程 [root@standby ~]# /etc/init.d/mysqld stop Shutting down MySQL... SUCCESS! [root@standb ...