过去几周我一直在编写一个简单的 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 文件。
谢谢!
最佳答案
不要将其解析为文本,这会转换字符,可能是结束行,并且可能会更改您不希望的内容。不要将整个内容作为字节数组进行缓冲,而是直接写入输出流,这样就不会出现内存问题。相反,只需像这样提供文件:
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...
}
}
}
}
关于java - 在 Java 中读取 PDF 文件 - 没有外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14403670/