Google Docs Viewer使用笔记

时间:2024-03-12 20:37:59
通常情况下这样使用:

http://docs.google.com/viewer?url=http://abc/test.pdf&embedded=true

这样相当于把我们的文件直接暴露给外部了,这样就没办法处理文件权限等问题,我们希望这个test.pdf是由一个servlet来处理,在servlet里面我们可以做文件权限的处理,如果没有权限,就不让用户浏览文件,如果有权限才返回文件流信息。测试时发现response里面要设置一些header信息,不然google docs viewer没法解析文件。

 

DocServlet.java
 1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 2     {
 3         doPost(request, response);
 4     }
 5     public void doPost(HttpServletRequest request, HttpServletResponse response)
 6             throws ServletException, IOException
 7     {
 8         try
 9         {
10             int index = request.getRequestURI().lastIndexOf(".");
11             String suffix = request.getRequestURI().substring(index + 1);
12             String path = "test." + suffix;
13             
14             
15             File file = new File(request.getRealPath("/") + "/" + path);
16             long fileLen = file.length();
17             String filename = file.getName();
18             InputStream fis = new BufferedInputStream(new FileInputStream(file));
19             byte[] buffer = new byte[fis.available()];
20             fis.read(buffer);
21             fis.close();
22 //            response.reset();
23 //            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1"));
24             if(suffix.equals("pdf"))
25             {
26                 response.setContentType("application/pdf");
27                 response.addHeader("Content-Type", "application/pdf");
28             }
29             else if(suffix.equals("xls"))
30             {
31                 response.setContentType("application/vnd.ms-excel");
32                 response.addHeader("Content-Type", "application/vnd.ms-excel");
33             }
34             else if(suffix.equals("xlsx"))
35             {
36                 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
37                 response.addHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
38             }
39             else if(suffix.equals("ppt"))
40             {
41                 response.setContentType("application/vnd.ms-powerpoint");
42                 response.addHeader("Content-Type", "application/vnd.ms-powerpoint");
43             }
44             else if(suffix.equals("pptx"))
45             {
46                 response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
47                 response.addHeader("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
48             }
49             else if(suffix.equals("docx"))
50             {
51                 response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
52                 response.addHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
53             }
54             else
55             {
56                 response.setContentType("application/msword");
57                 response.addHeader("Content-Type", "application/msword");
58             }
59             response.addHeader("Content-Length", "" + fileLen);
60             response.addHeader("Accept-Ranges", "bytes");
61             response.addHeader("Last-Modified", toGMT(new Date()));
62             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
63             toClient.write(buffer);
64             toClient.flush();
65             toClient.close();
66         }
67         catch (Exception e)
68         {
69             e.printStackTrace();
70         }
71     }
72     private static String toGMT(Date date)
73     {
74         try
75         {
76             Calendar cd = Calendar.getInstance();  
77             cd.setTimeInMillis(date.getTime());                  
78             SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss \'GMT\'", Locale.US);  
79             sdf.setTimeZone(TimeZone.getTimeZone("GMT"));  
80             String timeStr = sdf.format(cd.getTime());
81             return timeStr;
82         } catch (Exception e)
83         {
84             e.printStackTrace();
85         }
86         return null;
87     }

 然后在web.xml中配置:

 

web.xml
 1     <servlet>
 2         <servlet-name>DocServlet</servlet-name>
 3         <servlet-class>cn.nami.servlet.DocServlet</servlet-class>
 4     </servlet>
 5     <servlet-mapping>
 6         <servlet-name>DocServlet</servlet-name>
 7         <url-pattern>/docServlet.doc</url-pattern>
 8     </servlet-mapping>
 9     <servlet-mapping>
10         <servlet-name>DocServlet</servlet-name>
11         <url-pattern>/docServlet.docx</url-pattern>
12     </servlet-mapping>
13     <servlet-mapping>
14         <servlet-name>DocServlet</servlet-name>
15         <url-pattern>/docServlet.xls</url-pattern>
16     </servlet-mapping>
17     <servlet-mapping>
18         <servlet-name>DocServlet</servlet-name>
19         <url-pattern>/docServlet.xlsx</url-pattern>
20     </servlet-mapping>
21     <servlet-mapping>
22         <servlet-name>DocServlet</servlet-name>
23         <url-pattern>/docServlet.ppt</url-pattern>
24     </servlet-mapping>
25     <servlet-mapping>
26         <servlet-name>DocServlet</servlet-name>
27         <url-pattern>/docServlet.pptx</url-pattern>
28     </servlet-mapping>
29     <servlet-mapping>
30         <servlet-name>DocServlet</servlet-name>
31         <url-pattern>/docServlet.pdf</url-pattern>
32     </servlet-mapping>

 然后就可以http://docs.google.com/viewer?url=http://abc/docServlet.[pdf/doc/docx/xls/xlsx/ppt/pptx]&embedded=true浏览文件了。