阅读Java中的PDF文件 - 无外部库

时间:2013-01-18 16:51:42

标签: java pdf file-io

过去几周我一直在编写一个简单的Java服务器。

首先,我想根据您启动服务器的位置显示文件系统。例如,如果您在src目录中启动了服务器,打开了浏览器,并转到localhost:5555,您将看到src中包含的文件和目录。每个都是链接的。而且我的工作正常。

如果单击某个目录,它会显示其内容(就像我提到的那样)。如果单击某个文件,它将读取该文件并以纯文本格式显示该文件。如果单击图像,则会为该图像提供服务。这一切都发生在浏览器中,您可以使用后退按钮返回您之前查看的目录列表或文件。这也很好,不使用外部库。

这是我用来读取文本文件的代码(使用阅读器):

private String readFile() {
    BufferedReader reader;
    String response = "";
    try {
        FileReader fileReader = new FileReader(requestedFile);
        reader = new BufferedReader(fileReader);
        String line;
        while ((line = reader.readLine()) != null) {
            response += line + "\n";
        }
        reader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

这是我用来提供图像的代码(输入流而不是阅读器):

public byte[] getByteArray() throws IOException {
    byte[] byteArray = new byte[(int) requestedFile.length()];
    InputStream inputStream;
    String fileName = String.valueOf(requestedFile);
    inputStream = new BufferedInputStream(new FileInputStream(fileName));
    int bytesRead = 0;
    while (bytesRead < byteArray.length) {
        int bytesRemaining = byteArray.length - bytesRead;
        int read = inputStream.read(byteArray, bytesRead, bytesRemaining);
        if (read > 0) {
            bytesRead += read;
        }
    }
    inputStream.close();
    FilterOutputStream binaryOutputStream = new FilterOutputStream(outputStream);
    byte [] binaryHeaders = headers.getBytes();
    byte [] fullBinaryResponse = new byte[binaryHeaders.length + byteArray.length];
    System.arraycopy(binaryHeaders, 0, fullBinaryResponse, 0, binaryHeaders.length);
    System.arraycopy(byteArray, 0, fullBinaryResponse, binaryHeaders.length, byteArray.length);
    try {
        binaryOutputStream.write(fullBinaryResponse);
        binaryOutputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我现在正在尝试的是提供PDF。如果我在其中一个目录中有PDF并单击它,它应该打开该PDF(在浏览器使用的任何默认阅读器中)。

我一直在谷歌上搜索这个话题并尝试了一两天的事情,我似乎无法得到它。我发现奇怪的是,当我点击PDF作为我目前的代码时,浏览器似乎正在打开PDF但没有出现文本。这是我们在点击PDF链接时常常看到的标准浏览器内PDF浏览器。但是没有内容。这只是一些空白页面。

任何人都可以帮忙吗?我不打算使用外部库。我只是想了解如何用Java打开PDF文件。

谢谢!

1 个答案:

答案 0 :(得分:2)

不要将其解析为文本,这将转换字符,可能是结束行,并可能会更改您不想要的内容。不要将整个事件缓冲为字节数组,而是直接写入输出流,因此不存在内存问题。相反,只需像这样提供文件:

public class FileServer extends javax.servlet.http.HttpServlet
{

public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
    OutputStream out=null;
    try {

        HttpSession session = req.getSession();

        out = resp.getOutputStream();
        resp.setContentType(-- specify content type here --);
        req.setCharacterEncoding("UTF-8");

        String pathInfo = req.getPathInfo();

        String fullPath = -- figure out the path to the file in question --;

        FileInputStream fis = new FileInputStream(fullPath);

        byte[] buf = new byte[2048];

        int amtRead = fis.read(buf);
        while (amtRead > 0) {
            out.write(buf, 0, amtRead);
            amtRead = fis.read(buf);
        }
        fis.close();
        out.flush();
    }
    catch (Exception e) {
        try {
            resp.setContentType("text/html");
            if (out == null) {
                out = resp.getOutputStream();
            }
            Writer w = new OutputStreamWriter(out);
            w.write("<html><body><ul><li>Exception: ");
            w.write(e.toString());
            w.write("</ul></body></html>");
            w.flush();
        }
        catch (Exception eeeee) {
            //nothing we can do here...
        }
    }
}
}
相关问题