Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. char[] updatedArr = new char[pars.getHttpRequestParser().getFileName().length()-1];
  2. pars.getHttpRequestParser().getFileName().getChars(1, pars.getHttpRequestParser().getFileName().length(), updatedArr, 0);
  3. File startingDirectory = new File(String.valueOf(updatedArr));
  4. if (startingDirectory.isDirectory()) {
  5. PrintWriter out = new PrintWriter(new BufferedOutputStream(pars.getOutputStream()));
  6. BufferedOutputStream dataOut = new BufferedOutputStream(pars.getOutputStream());
  7. FileListing listing = new FileListing();
  8. List<File> files = listing.getFileListing(startingDirectory);
  9. File file1 = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);
  10. int fileLength = (int) file1.length();
  11. String contentMimeType = "text/html";
  12. long timeStamp = file1.lastModified();
  13. out.println("HTTP/1.1 200 OK");
  14. out.println("Server: Java HTTP Server from : 1.0");
  15. out.println("Last-Modified: " + timeStamp);
  16. out.println("Date: " + new Date());
  17. out.println("Content-type: " + contentMimeType);
  18. out.println("Content-length: " + fileLength);
  19. out.println(); // blank line between headers and content, very important !
  20. out.flush();
  21. dataOut.write("<html><body>".getBytes());
  22. for (File file : files) {
  23. Path rootPath = Paths.get(new File(String.valueOf(WEB_ROOT)).toURI()); // this can be static
  24. Path filePath = Paths.get(file.toURI());
  25. String pathString = rootPath.relativize(filePath).toString();
  26. String mesaj="<a href=\"/" + pathString + "\">" + file.getName() + "</a><br>";
  27. dataOut.write(mesaj.getBytes());
  28. dataOut.write("<a href=\"/".getBytes());
  29. dataOut.write(pathString.getBytes());
  30. dataOut.write("\">".getBytes());
  31. dataOut.write(file.getName().getBytes());
  32. dataOut.write("</a><br>".getBytes());
  33. }
  34. dataOut.write("</body></html>".getBytes());
  35. dataOut.flush();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement