【发布时间】:2010-12-18 01:58:11
【问题描述】:
我已经创建了自己的 HTTP 服务器。我需要将一个 PDF 文件(由 Jasper Reports 生成)返回到 Web 浏览器。但是,当我读取 PDF 文件并将其内容写入套接字时,Web 浏览器会收到一个空白 PDF 文件。当我保存此文件并将其与原始文件进行比较时,我看到许多字符已从其原始值转换为 0x3F(即 '?')。
当我读取文件时,我的调试输出显示读取了正确的值,并且将正确的值写入了套接字。谁能帮帮我?
这是读取 PDF 文件的代码(减去所有调试代码):
File f = new File(strFilename);
long len = f.length();
byteBuffPdfData = ByteBuffer.allocate( (int)len );
FileInputStream in = new FileInputStream(strFilename);
boolean isEOF = false;
while (!isEOF)
{
int iValue = in.read();
if (iValue == -1)
{
isEOF = true;
}
else
{
byteBuffPdfData.put( (byte)iValue );
}
}
接下来是从字节缓冲区写入套接字的代码...
printWriter = new PrintWriter( socket.getOutputStream(), true );
printWriter.write(strHttpHeaders);
// Headers:
// HTTP/1.0 200 OK
// Date: Wed, 18 Nov 2009 21:04:36
// Expires: Wed, 18 Nov 2009 21:09:36
// Cache-Control: public
// Content-Type: application/pdf
// Content-Length: 1811
// Connection: keep-alive
//
byteBuffPdfData.rewind();
while(byteBuffPdfData.hasRemaining())
{
printWriter.print( (char)byteBuffPdfData.get() );
}
printWriter.flush();
socket.close();
非常感谢您提供的任何帮助。我确信我需要对字符集做一些事情,但此时我已经尝试了一百万件事,但似乎没有任何效果。
约翰
【问题讨论】:
标签: http pdf sockets character-encoding