在同一页面的文本区域中打印文件内容

发布于 2024-12-23 09:41:34 字数 1657 浏览 1 评论 0原文

我需要一个包含文件上传的页面和一个文本区域,文件的内容将打印在该页面中。

目前我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-12-30 09:41:34

所有表单字段都在此 else 块中可用,您在后处理中完全忽略了该块。

} else {
    String otherFieldName = item.getFieldName();
    String otherFieldValue = item.getString();
}

不要忽视它。这些值表示常规表单字段的名称=值对。

请注意,您不能在 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.

} else {
    String otherFieldName = item.getFieldName();
    String otherFieldValue = item.getString();
}

Don't ignore it. Those values represent the name=value pairs of regular form fields.

Note that you cannot use getParameter() on a multipart/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 a multipart/form-data request.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文