`
lh44601
  • 浏览: 6994 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

关于response返回的文件格式和下载如何在浏览器中直接打开文件

 
阅读更多

/**
*application/pdf 设置返回的流的格式
*
*Content-Disposition 设置浏览器对流的处理方式--inline为在浏览器中直接打开,attachment为提供浏览器下载和打开
*
*
*/

HttpServletResponse res=ServletActionContext.getResponse();
res.reset();
res.setContentType("application/pdf;charset=UTF-8");
res.addHeader("Content-Disposition", "inline;filename=\""+pdfName+"\"");
res.addHeader("Content-Disposition","filename="+pdfName);
BufferedOutputStream out=new BufferedOutputStream(res.getOutputStream());
out.write(BASE64.decode(data));
out.close();

不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.
代码如:
<% response.ContentType ="text/html" %>
<!--#i nclude virtual="/ContentType.html" -->

显示的为网页,而
<% response.ContentType ="text/plain" %>
<!--#i nclude virtual="/sscript/ContentType.html" -->

则会显示html原代码.

以下为一些常用的 ContentType
GIF images
<% response.ContentType ="image/gif" %>
<!--#i nclude virtual="/myimage.gif" -->

JPEG images
<% response.ContentType ="image/jpeg" %>
<!--#i nclude virtual="/myimage.jpeg" -->

TIFF images
<% response.ContentType ="image/tiff" %>
<!--#i nclude virtual="/myimage.tiff" -->

MICROSOFT WORD document
<% response.ContentType ="application/msword" %>
<!--#i nclude virtual="/myfile.doc" -->

RTF document
<% response.ContentType ="application/rtf" %>
<!--#i nclude virtual="/myfile.rtf" -->

MICROSOFT EXCEL document
<% response.ContentType ="application/x-excel" %>
<!--#i nclude virtual="/myfile.xls" -->

MICROSOFT POWERPOINT document
<% response.ContentType ="application/ms-powerpoint" %>
<!--#i nclude virtual="/myfile.pff" -->

PDF document
<% response.ContentType ="application/pdf" %>
<!--#i nclude virtual="/myfile.pdf" -->

ZIP document
<% response.ContentType ="application/zip" %>
<!--#i nclude virtual="/myfile.zip" -->

下面是更详细的ContentType
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk'

分享到:
评论

相关推荐

    asp在IE浏览器中下载服务端上的各类文件的实现方法

    即直接提示用户下载而不是由浏览器打开某些文件。注意,下面的代码拷贝到ASP文件中后,不要再添加一些非ASP代码在页面中:如HTML和javascript客户端的代码。 代码如下:&lt;&#37; ‘——————————————– ...

    dashboardAntDesign:可以在Response JS和Ant Design中进行仪表板响应和响应。 Espero te Sea de Gran utilidad

    打开在浏览器中查看。 如果进行编辑,页面将重新加载。 您还将在控制台中看到任何棉绒错误。npm test 在交互式监视模式下启动测试运行程序。 有关更多信息,请参见关于的部分。npm run build 构建生产到应用程序...

    Python网络爬虫项目实战-网页内容解析

    打开一个Url链接,浏览器自动向Url链接的服务器发送一个请求(Request),告诉服务器说我需要访问这个Url链接的内容,请返回数据给我。服务器就处理该请求,响应该请求并返回结果给浏览器。 既然爬虫需要模拟该过程。...

    如何将一个TXT文档里内容导入到文本框里

    (此页是AJAX异步提交txt文本文件的路径到ajax_txt_save.asp页面,并从ajax_txt_save.asp获取返回的数据) &lt;!DOCTYPE ...

    ASPJPEG组件,ASPJPEG

    将以下代码保存为asp,拷贝在服务器上,用浏览器打开,看返回结果。 set jpeg=server.createobject("persits.jpeg") response.write jpeg.expires '检测aspjpeg的注册状态 '注册成功则到期时间为:9999-9-9 '否则为...

    aspjpeg2.0注册码 aspjpeg破解版 安装aspjpeg2.0组件

    将以下代码保存为asp,拷贝在服务器上,用浏览器打开,看返回结果。 set jpeg=server.createobject("persits.jpeg") response.write jpeg.expires '检测aspjpeg的注册状态 '注册成功则到期时间为:9999-9-9 '否则为...

    乐优商城.xmind

    如果服务器允许跨域,需要在返回的响应头中携带下面信息 Access-Control-Allow-Origin:可接受的域,是一个具体域名或者*,代表任意 Access-Control-Allow-Credentials:是否允许携带cookie,默认情况下,cors...

    fso浏览54646465465464564

    &lt;tr&gt;&lt;th&gt;FSO文件浏览器 - 系统磁盘信息&lt;/th&gt;&lt;/th&gt; &lt;tr&gt;&lt;th width="10%"&gt;盘符&lt;/th&gt;&lt;th width="15%"&gt;类型&lt;/th&gt;&lt;th width="20%"&gt;卷标&lt;/th&gt;&lt;th width="15%"&gt;文件系统&lt;/th&gt;&lt;th width="20%"&gt;总容量&lt;/th&gt;&lt;th width="20%...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -修正basic/hello.aspx示例在单独浏览器打开后,不能弹出对话框的BUG。 -隐藏示例首页最外层RegionPanel的边框ShowBorder="false"。 +集成Extjs最新版本v3.1.1。 -增加一个新的Theme - Access。 -修正了Fire...

    ExtAspNet_v2.3.2_dll

    -修正basic/hello.aspx示例在单独浏览器打开后,不能弹出对话框的BUG。 -隐藏示例首页最外层RegionPanel的边框ShowBorder="false"。 +集成Extjs最新版本v3.1.1。 -增加一个新的Theme - Access。 -修正了Fire...

    计算机二级Web程序设计模拟卷(二).doc

    在Servlet中,response.getWriter()返回的是() A.JspWriter对象 B.PrintWriter对象 C.Out对象 D.ResponseWriter对象 正确答案:B 您的答案: 本题解析: 暂无解析 10.在Servlet中,下列语句可以正确获取PrintWriter...

    ASP3《高级编程》(第一部分)

    包含在Windows 2000中的Active Server Pages 3.0 (ASP 3.0)是Microsoft公司推出的又一个支持Internet的功能强大的网页制作软件包,除了继续保持其适应于各种浏览器的基本特征外,与ASP 2.0相比,功能更加强大,目前...

    ASP3《高级编程》(第二部分)

    包含在Windows 2000中的Active Server Pages 3.0 (ASP 3.0)是Microsoft公司推出的又一个支持Internet的功能强大的网页制作软件包,除了继续保持其适应于各种浏览器的基本特征外,与ASP 2.0相比,功能更加强大,目前...

    Delphi5开发人员指南

    4.6.4 在DPR文件中增加代码 107 4.6.5 覆盖应用程序的异常处理 107 4.6.6 显示一个封面 109 4.6.7 使窗体尺寸最小 110 4.6.8 运行没有窗体的项目 111 4.6.9 退出Windows 112 4.6.10 防止关闭Windows 113 4.7 总结 ...

    蓝焰设计站图文管理系统

    ⑶ 新闻阅读:在其他页面中点击标题链接即进入新闻阅读页面,此时,每条新闻的详细信息将被取出,包括内容、标题、关键字等,并按照相对固定的格式放置在页面的不同区域,所有新闻使用大致相同的页面布局,只是各...

    LotusDomino学习笔记(400页涵盖代理和公式使用)

    72. 关于创建、删除、编辑、打开和保存文档的 URL 命令 156 73. Domino URL 命令 158 74. 在打开有下面这段代码的页面时将会跳出一个468x60大小的小窗口 160 75. javascript的容错脚本 161 76. Web search with ...

    LotusDomino学习笔记.doc

    72. 关于创建、删除、编辑、打开和保存文档的 URL 命令 156 73. Domino URL 命令 158 74. 在打开有下面这段代码的页面时将会跳出一个468x60大小的小窗口 160 75. javascript的容错脚本 161 76. Web search with ...

    C#编程经验技巧宝典

    111 &lt;br&gt;0184 如何在ASP.NET中获取文件的扩展名 111 &lt;br&gt;0185 如何在ASP.NET中用URL在页面之间传值 112 &lt;br&gt;0186 如何使用IsPostBack实现ASP.NET页面加载 112 &lt;br&gt;0187 如何利用输出缓存技术缓存...

    Lotus Domino WEB 开发技术积累-DOC(313页)

    72. 关于创建、删除、编辑、打开和保存文档的 URL 命令 156 73. Domino URL 命令 158 74. 在打开有下面这段代码的页面时将会跳出一个468x60大小的小窗口 160 75. javascript的容错脚本 161 76. Web search with ...

Global site tag (gtag.js) - Google Analytics