(1).框架为SSH:html
xml配置:(blank.jsp页面是啥都没有,彻底一个空白页面)java
<!-- 发文管理详细信息,显示PDF发文 --> <action name="showPdf" method="showPdf" class="standard-fileDataAction"> <result name="success" > /blank.jsp </result> </action>
JAVA后台:web
public String showPdf() throws IOException{ FileData fileData = fileDataService.getById(id); byte[] bit = fileData.getData(); if(bit != null){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentLength(bit.length); response.setContentType("application/pdf;charset=UTF-8"); OutputStream test = response.getOutputStream(); DataOutput output = new DataOutputStream(test); for( int i = 0; i < bit.length; i++ ) { output.writeByte(bit[i] ); } test.flush(); test.close(); test=null; response.flushBuffer(); } return ActionSupport.SUCCESS; }
JSP页面请求:数据库
./iface/showPdf?id="+fileDataObject.getId()
(2)这个测试可行,在网上也搜索了其余的一些用法,不过没有测试过:app
把PDF文档经过JSP页面展现给用户。主要使用Object标签来实现的。框架
<html> <head> <title>Insert title here</title> </head> <body> <% String docPath = request.getParameter("docPath ") == null ? "": request.getParameter("docPath "); %> <object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="990" height="700" border="0" top="-10" name="pdf"> <param name="toolbar" value="false"> <param name="_Version" value="65539"> <param name="_ExtentX" value="20108"> <param name="_ExtentY" value="10866"> <param name="_StockProps" value="0"> <!-- 下面是指明你的PDF文档所在地,相对于发布web目录 --> <param name="SRC" value="<%=docPath%>"> </object> </body> </html>
(3)利用java读取某个路径下的pdf文件,并在JSP页面打开:jsp
response.setContentType("application/pdf"); FileInputStream in = new FileInputStream(new File("d:/1.pdf")); OutputStream out = response.getOutputStream(); byte[] b = new byte[512]; while ((in.read(b)) != -1) { out.write(b); } out.flush(); in.close(); out.close();
解释下MYSQL中BLOB类型的字段:用于存储二进制数据(利用inputStream来读取文件实际上也是获取文件的二进制数据,和在数据库中获取blob字段值是同样的)测试
MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的惟一区别是在存储文件的最大大小上不一样。编码
MySQL的四种BLOB类型
类型 大小(单位:字节)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4Gspa
解释下字节流:
字节流是由字节组成的,字符流是由字符组成的. Java里字符由两个字节组成.字节流是最基本的,全部的InputStream和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的但实际中不少的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化。在从字节流转化为字符流时,实际上就是byte[]转化为String时,public String(byte bytes[], String charsetName)有一个关键的参数字符集编码,一般咱们都省略了,那系统就用操做系统默认的lang