Currently I am trying to read the contents of an uploaded file via the following code:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
String urlConnection = "/DisplayInfo.jsp";
Object htmlFile = request.getParameter("htmlFile");
String contents = "";
File f = (File) htmlFile;
if(f.canRead()) {
Scanner stdin = new Scanner(f);
while(stdin.hasNextLine()) {
contents += stdin.nextLine();
}
stdin.close();
}
//htmlFile = contents;
request.setAttribute("htmlFile1", contents);
getServletContext().getRequestDispatcher(urlConnection).forward(request, response);
}
finally {
out.close();
}
}
as defined by:
</form>
<form action ="MyServlet" method="post">
<input type="file" name="htmlFile">
<input type="submit" value="send">
</form>
and called via:
</b>
Something here: ${htmlFile1}
</body>
Essentially I am trying to read the contents of an html file and display said contents out on the website body. However, when I try to parse to the string contents
I get an exception indicating the value is null.
Is there a better way to do what I am attempting?