在同一页面的文本区域中打印文件内容
我需要一个包含文件上传的页面和一个文本区域,文件的内容将打印在该页面中。
目前我有一个jsp文件和一个servlet:
index.jsp的一部分:
<form action="FileReader" ENCTYPE="multipart/form-data" method="POST">
<textarea name="textinputarea" rows="14" cols="130" readonly>
Some text
</textarea>
<br> <br><tr>
<td valign="top" align="left" height="200" width="33%">
<img class="start_img" src="file_Selections.jpg"> <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>
servlet的一部分:
public void doPost(HttpServletRequest request, HttpServletResponse response)
String name = request.getParameter("textinputarea");
(...)
}else {
String otherFieldName = item.getFieldName();
String otherFieldValue = item.getString();}}
(...)
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());
};
}
这实际上在新页面中打印文件的内容。我尝试为文本区域提供相同的名称和“String name = request.getParameter(“textinputarea”);“..
感谢您的时间!
I need a single page with a file upload and a text area where the contents of the file are printed in this page.
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>
<br> <br><tr>
<td valign="top" align="left" height="200" width="33%">
<img class="start_img" src="file_Selections.jpg"> <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>
Part of the servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
String name = request.getParameter("textinputarea");
(...)
}else {
String otherFieldName = item.getFieldName();
String otherFieldValue = item.getString();}}
(...)
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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有表单字段都在此
else
块中可用,您在后处理中完全忽略了该块。不要忽视它。这些值表示常规表单字段的名称=值对。
请注意,您不能在
multipart/form-data
编码请求上使用getParameter()
。这正是您使用 Apache Commons FileUpload 来提取文件的原因。您应该使用相同的 API 来提取multipart/form-data
请求的其他部分。另请参阅:
All form fields are available in this
else
block which you completely ignored in the postprocessing.Don't ignore it. Those values represent the name=value pairs of regular form fields.
Note that you cannot use
getParameter()
on amultipart/form-data
encoded request. That's exactly why you were using Apache Commons FileUpload to extract the file. You should use the same API to extract the other parts of amultipart/form-data
request.See also: