java web在线预览word,pdf,ppt,jpg等文件

技术新人牛 2017-03-02 04:15:29
在做一个附件预览的功能,代码写了一些,但是在页面展示不出来,浏览器总是提示下载

代码如下使用spring mvc
controller中的

@RequestMapping("/read.bo")
public void read(HttpServletRequest request, HttpServletResponse response,
BaseRequest brqs) {

OutputStream os = null;

try {
// 验证参数
String id = request.getParameter("id");

if (id == null || id.length() == 0) {
throw new WAFException("参数缺失!");
}

AttachmentVO attachmentVO = getService().getVOById(id);

if (attachmentVO == null) {
throw new WAFException("参数缺失!");
}

String path = attachmentVO.getUrl() + File.separator
+ attachmentVO.getPhysicalname();

String fileRealPath = request.getServletContext().getRealPath(path);
File file = new File(fileRealPath);
String filename = attachmentVO.getSourcename();

if (!file.exists()) {
throw new WAFException("阅读的附件不存在!");
}

// 读取文件
InputStream fis = new BufferedInputStream(new FileInputStream(
fileRealPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();

// 设置下载文件名等
String showFileName = new String(filename.replaceAll(" ", "")
.getBytes("utf-8"), "iso8859-1");
String extName = showFileName.substring(
showFileName.lastIndexOf(".")).toLowerCase();
response.addHeader("Content-Disposition", "attachment;filename="
+ showFileName);
response.addHeader("Content-Length", "" + file.length());
// 设置内容编码
response.setCharacterEncoding(WAFConstants.CHARSET_DEFAULT);
// 设置响应MIME
if (".doc".equals(extName)) {
response.setContentType("application/msword");
} else if (".docx".equals(extName)) {
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
} else if (".pdf".equals(extName)) {
response.setContentType("application/pdf");
} else if (".xls".equals(extName)) {
response.setContentType("application/vnd.ms-excel");
} else if (".xlsx".equals(extName)) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
} else if (".ppt".equals(extName)) {
response.setContentType("application/vnd.ms-powerpoint");
} else if (".pptx".equals(extName)) {
response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
} else if (".bmp".equals(extName)) {
response.setContentType("image/bmp");
} else if (".gif".equals(extName)) {
response.setContentType("image/gif");
} else if (".ief".equals(extName)) {
response.setContentType("image/ief");
} else if (".jpeg".equals(extName)) {
response.setContentType("image/jpeg");
} else if (".jpg".equals(extName)) {
response.setContentType("image/jpeg");
} else if (".png".equals(extName)) {
response.setContentType("image/png");
} else if (".tiff".equals(extName)) {
response.setContentType("image/tiff");
} else if (".tif".equals(extName)) {
response.setContentType("image/tif");
}

os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer);// 输出文件
os.flush();

response.flushBuffer();

} catch (Exception e) {
e.printStackTrace();
if (os != null) {
try {
os.write(e.getLocalizedMessage().getBytes(
WAFConstants.CHARSET_DEFAULT));
os.flush();
} catch (Exception le) {
// NOOP
}
}
} finally {
if (os != null) {
try {
os.close();
} catch (Exception e) {
// NOOP
}
}
}

}


jsp页面

$(function(){
var id = "${id}";
var url = waf.contextPath + "/bpms/peform/core/attachment/read.bo?id=" + id;

$("#v_waf_bpms_attach_read").html("<iframe width='100%' height='100%' src='" + url + "' />");
})


代码就是这么多,总是提示下载。请各位大神帮帮忙,谢谢
...全文
618 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36439926 2017-11-08
  • 打赏
  • 举报
回复
在哪下载啊?
网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R,修复! Windows XP(包括 Windows 2000)的控制台命令是在系统出现一些意外情况下的一种非常有效的诊断和测试以及恢复系统功能的工具。小编的确一直都想把这方面的命令做个总结,这次辛苦老范给我们整理了这份实用的秘笈。   Bootcfg   bootcfg 命令启动配置和故障恢复(对于大多数计算机,即 boot.ini 文件)。   含有下列参数的 bootcfg 命令仅在使用故障恢复控制台时才可用。可在命令提示符下使用带有不同参数的 bootcfg 命令。   用法:   bootcfg /default  设置默认引导项。   bootcfg /add    向引导列表中添加 Windows 安装。   bootcfg /rebuild  重复全部 Windows 安装过程并允许用户选择要添加的内容。   注意:使用 bootcfg /rebuild 之前,应先通过 bootcfg /copy 命令备份 boot.ini 文件。   bootcfg /scan    扫描用于 Windows 安装的所有磁盘并显示结果。   注意:这些结果被静态存储,并用于本次会话。如果在本次会话期间磁盘配置发生变化,为获得更新的扫描,必须先重新启动计算机,然后再次扫描磁盘。   bootcfg /list   列出引导列表中已有的条目。   bootcfg /disableredirect 在启动引导程序中禁用重定向。   bootcfg /redirect [ PortBaudRrate] |[ useBiosSettings]   在启动引导程序中通过指定配置启用重定向。   范例: bootcfg /redirect com1 115200 bootcfg /redirect useBiosSettings   hkdsk   创建并显示磁盘的状态报告。Chkdsk 命令还可列出并纠正磁盘上的错误。   含有下列参数的 chkdsk 命令仅在使用故障恢复控制台时才可用。可在命令提示符下使用带有不同参数的 chkdsk 命令。   vol [drive:] [ chkdsk [drive:] [/p] [/r]   参数  无   如果不带任何参数,chkdsk 将显示当前驱动器中的磁盘状态。 drive: 指定要 chkdsk 检查的驱动器。 /p   即使驱动器不在 chkdsk 的检查范围内,也执行彻底检查。该参数不对驱动器做任何更改。 /r   找到坏扇区并恢复可读取的信息。隐含着 /p 参数。   注意 Chkdsk 命令需要 Autochk.exe 文件。如果不能在启动目录(默认为 %systemroot%System32)中找到该文件,将试着在 Windows 安装 CD 中找到它。如果有多引导系统的计算机,必须保证是在包含 Windows 的驱动器上使用该命令。 Diskpart   创建和删除硬盘驱动器上的分区。diskpart 命令仅在使用故障恢复控制台时才可用。   diskpart [ /add |/delete] [device_name |drive_name |partition_name] [size]   参数 无   如果不带任何参数,diskpart 命令将启动 diskpart 的 Windows 字符模式版本。   /add   创建新的分区。   /delete   删除现有分区。   device_name   要创建或删除分区的设备。设备名称可从 map 命令的输出获得。例如,设备名称:   DeviceHardDisk0   drive_name   以驱动器号表示的待删除分区。仅与 /delete 同时使用。以下是驱动器名称的范例:   D:   partition_name   以分区名称表示的待删除分区。可代替 drive_name 使用。仅与 /delete 同时使用。以下是分区名称的范例:   DeviceHardDisk0Partition1    大小   要创建的分区大小,以兆字节 (MB)表示。仅与 /add 同时使用。   范例   下例将删除分区: diskpart /delete Device HardDisk0 Partition3 diskpart /delete F:   下例将在硬盘上添加一个 20 MB 的分区:   diskpart /add Device HardDisk0 20   Fixboot

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧