我有一個典型的servlet將pdf流式傳輸到瀏覽器。這些pdf存儲在我的servlet從中獲取的內部服務器上。如果我直接從瀏覽器中打開servlet,則會顯示pdf。如果我在網頁中的<IMG>
標記中嘗試使用相同的網址,那麼...破損的管道。通過servlet將pdf流式傳輸到瀏覽器
任何洞察力爲什麼這應該是?
作爲一個實驗,我可以流沒有問題的GIF。
這裏,我幾乎從innerwebs清除代碼:
public class PdfServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(PdfServlet.class.getName());
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) {
String url = (String) req.getParameter("url");
log.info("The URL is " + url);
String format = "application/pdf";
// String format = "image/gif";
streamBinaryData(url, format, res);
}
/*
* This Method Handles streaming Binary data
* <p>
* @param String urlstr ex: http;//localhost/test.pdf etc.
* @param String format ex: pdf or audio_wav or msdocuments etc.
* @param ServletOutputStream outstr
* @param HttpServletResponse resp
*/
private void streamBinaryData(
String urlstr,
String format,
HttpServletResponse resp) {
ServletOutputStream outstr = null;
String ErrorStr = null;
try {
outstr = resp.getOutputStream();
//find the right MIME type and set it as contenttype
resp.setContentType(format);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL(urlstr);
URLConnection urlc = url.openConnection();
int length = urlc.getContentLength();
resp.setContentLength(length);
// resp.setHeader("Content-Length", String.valueOf(+length));
// resp.setHeader("Content-Disposition", "inline");
// Use Buffered Stream for reading/writing.
InputStream in = urlc.getInputStream();
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(outstr);
byte[] buff = new byte[length];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
log.info("Got a chunk of " + bytesRead);
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
ErrorStr = "Error Streaming the Data";
outstr.print(ErrorStr);
} finally {
log.info("finally!!!");
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
if (outstr != null) {
outstr.flush();
outstr.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
...和HTML文件。即使內容類型被返回爲'application/pdf',pdf也會失敗並顯示一個損壞的管道並顯示gif圖像。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cheesy Servlet Experiment</title>
</head>
<body>
<h1>Cheesy Servlet Experiment</h1>
<P>
<img src="http://10.0.0.9/ServletExperiment/pdf?url=http%3a%2f%2fwww.samplepdf.com%2fsample.pdf" alt="yah mon">
<P>
<img src="http://10.0.0.9/ServletExperiment/pdf?url=http%3a%2f%2fbbs.homeshopmachinist.net%2fimages%2fstatusicon%2fforum_new.gif" alt="yah mon">
</body>
</html>
編輯 - 下面的作品在FF中。我不知道它有多標準。
<object data="http://www.samplepdf.com/sample.pdf" type="application/pdf" width="600" height="600">
alt : <a href="http://www.samplepdf.com/sample.pdf">test.pdf</a>
</object>
Interesting info here。看起來相當好的支持。
編輯與可能的解決方案在發佈。 – 2010-10-06 06:32:11
什麼是功能要求?在線顯示PDF? 「
在這一點上,我認爲要求是*東西*的工作,哈哈。帶有PDF的服務器位於我們的防火牆後面。當它被測試時,測試者顯然仍然在VPN上或者有一個緩存頁面。我們不知道。但是我們的客戶都沒有看到他們的PDF文件,而且事情都很緊張。我們在週六推動推動。 – 2010-10-06 12:17:29