【问题标题】:How can I upload a file in JSP without using two JSP files?如何在不使用两个 JSP 文件的情况下在 JSP 中上传文件?
【发布时间】:2020-07-16 11:56:59
【问题描述】:

【问题讨论】:

  • 欢迎来到 StackOverflow @n-s。请在您的问题中放置任何相关代码,而不是代码图片的链接。
  • 很简单,因为 POST 表单的 URL不应该 指向 JSP,而是指向 Servlet(或请求处理程序,如果您正在使用其他框架)。您使用 JSP 呈现 HTML,即显示表单,然后使用 Servlet 处理 POST 请求,当上传完成处理后,该请求将重定向回表单(或其他地方)。

标签: java html jsp web project


【解决方案1】:

单独的 JSP,文件 = upload.jsp

<form method="post" enctype="multipart/form-data">
<input type=file name=file1>
<input type=submit>
</form>

<%
  if (request.getContentType()==null) return;
  <... your upload code, whether this is Apache Commons or straight JSP...>
%>
<% out.println("File has been uploaded."); %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 2011-08-18
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2012-03-21
    • 1970-01-01
    相关资源
    最近更新 更多