I need a single page with a file upload and a text area where the contents of the file are printed there.
At the moment I have a jsp file and a servlet:
Part of index.jsp :
<form action=”FileReader” ENCTYPE=”multipart/form-data” method=”POST”>
<textarea name=”textinputarea” rows=”14″ cols=”130″ readonly>
Some text </textarea>
</td>
</tr>
<!– Tools Selection –>
<tr>
<td valign=”top” align=”left” height=”200″ width=”33%”>
<img class=”start_img” src=”file_Selections.jpg”> <br> <br>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000″ />
<input type=”file” name=”user_file” accept=”text/xml”>
<input type=”submit” value=”Validate” /> <br>
</form>
Here is the “POST” part of the servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter(“textinputarea”);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
File uploadedFile = null;
String dirPath=”C:\fileuploads”;
while (iterator.hasNext()) {
FileItem item = iterator.next();
if (!item.isFormField()) {
String fileNameWithExt = item.getName();
File filePath = new File(dirPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
uploadedFile = new File(dirPath + “/” + fileNameWithExt);
item.write(uploadedFile);
}
else {
String otherFieldName = item.getFieldName();
String otherFieldValue = item.getString();}}
FileInputStream fstream = new FileInputStream(uploadedFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null;
out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Processing get requests with data</title>”);
out.println(“</head>”);
// body section of document
out.println(“<body>”);
while ((strLine = br.readLine()) != null) {
// Print the content on the console
out.println(strLine + “</br>”);
}
out.println(“</body>”);
// end of html document
out.println(“</html>”);
out.close();
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
};
}
This is actually prints the content of file in a new page. I tried to give the same name of text area and the “String name = request.getParameter(“textinputarea”); “..
Thanks for your time!