当使用 enctype="multipart/form-data" 属性时

根据Sending additional data with multipart,request.getParameter;不能使用 with enctype="multipart/form-data"。在 process.jsp,我没有使用request.getParameter. 但jsp:getProperty返回空值。删除enctype="multipart/form-data"工作正常。我想知道 enctype="multipart/form-data" 效果如何jsp:setProperty and jsp:getProperty。它们是如何连接的?我知道jsp:setProperty不是更喜欢的方式。当我使用旧代码时,没有使用框架或没有使用 MVC。但我必须使用 servlet 3.0 和 tomcat 8.5 运行。enctype="multipart/form-data"在使用Jsp 到 Jsp时,还有其他方法可以传递数据吗?


表单.jsp


<!--    <form action="process.jsp" method="post" enctype="application/x-www-form-urlencoded"> -->

<!--    <form action="process.jsp" method="post"> -->

    <form action="process.jsp" method="post" enctype="multipart/form-data">

        Name:<input type="text" name="name"><br> 

        Password:<input type="password" name="password"><br> 

        Email:<input type="text" name="email"><br> 

        File:<input type="file" name="fileName"><br> 

        <hr>

        <input type="submit" value="register">

    </form>

进程.jsp


<jsp:useBean id="bean" class="dao.User" scope="page">

<jsp:setProperty property="*" name="bean"/>  

</jsp:useBean>  


Record:<br>  

<jsp:getProperty property="name" name="bean"/><br>  

<jsp:getProperty property="password" name="bean"/><br>  

<jsp:getProperty property="email" name="bean" /><br>  

User.java


public class User {


    private String name;

    private String password;

    private String email;

    private String fileName;


// getter and setter...


}


慕码人8056858
浏览 175回答 2
2回答

慕娘9325324

您可以使用 jsp 将文件上传到带有其他输入字段的服务器。前任。索引.jsp<form action="upload.jsp" method="post" enctype="multipart/form-data"><input type="file" name="file" size="50" /><br><input type="text" name="name" /><br /><input type="submit" value="Upload File" /></form>上传.jsp<%@ page import="java.io.*,java.util.*, javax.servlet.*"%><%@ page import="javax.servlet.http.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="org.apache.commons.fileupload.disk.*"%><%@ page import="org.apache.commons.fileupload.servlet.*"%><%@ page import="org.apache.commons.io.output.*"%><html><head></head><body>&nbsp; &nbsp; <%&nbsp; &nbsp; &nbsp; &nbsp; File file;&nbsp; &nbsp; &nbsp; &nbsp; int maxFileSize = 5000 * 1024;&nbsp; &nbsp; &nbsp; &nbsp; int maxMemSize = 5000 * 1024;&nbsp; &nbsp; &nbsp; &nbsp; String filePath = "D:/";&nbsp; &nbsp; &nbsp; &nbsp; String contentType = request.getContentType();&nbsp; &nbsp; &nbsp; &nbsp; if ((contentType.indexOf("multipart/form-data") >= 0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DiskFileItemFactory factory = new DiskFileItemFactory();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.setSizeThreshold(maxMemSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factory.setRepository(new File("c:\\temp"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServletFileUpload upload = new ServletFileUpload(factory);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upload.setSizeMax(maxFileSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List fileItems = upload.parseRequest(request);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator i = fileItems.iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("<html>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("<body>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (i.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileItem fi = (FileItem) i.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!fi.isFormField()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fieldName = fi.getFieldName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileName = fi.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean isInMemory = fi.isInMemory();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long sizeInBytes = fi.getSize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file = new File(filePath + fileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //fi.write(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("Uploaded Filename: " + filePath + fileName + "<br>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(fi.getFieldName() +":"+fi.getString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("</body>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("</html>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("<html>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("<body>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("<p>No file uploaded</p>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("</body>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println("</html>");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; %></body></html>您需要在 servlet jar 中包含 jar 文件 commons-fileupload。

慕雪6442864

我发现为什么enctype="multipart/form-data"不使用jsp:setProperty and jsp:getProperty. 当我使用 tomcat 运行时,process.jsp 生成为 process_jsp.java。&nbsp; dao.User bean = null;&nbsp; bean = (dao.User) _jspx_page_context.getAttribute("bean", javax.servlet.jsp.PageContext.PAGE_SCOPE);&nbsp; if (bean == null){&nbsp; &nbsp; bean = new dao.User();&nbsp; &nbsp; _jspx_page_context.setAttribute("bean", bean, javax.servlet.jsp.PageContext.PAGE_SCOPE);&nbsp; &nbsp; out.write('\n');&nbsp; &nbsp; org.apache.jasper.runtime.JspRuntimeLibrary.introspect(_jspx_page_context.findAttribute("bean"), request);&nbsp; &nbsp; out.write(' ');&nbsp; &nbsp; out.write(' ');&nbsp; &nbsp; out.write('\n');&nbsp; }根据上面的源代码org.apache.jasper.runtime.JspRuntimeLibrary.introspect被调用。&nbsp; &nbsp;public static void introspect(Object bean, ServletRequest request) throws JasperException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Enumeration<String> e = request.getParameterNames();&nbsp; &nbsp; &nbsp; &nbsp; while ( e.hasMoreElements() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name&nbsp; = e.nextElement();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String value = request.getParameter(name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; introspecthelper(bean, name, value, request, name, true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }上面的代码映射请求参数和名称(bean 属性名称),然后introspecthelper将通过使用将值传递给适当的 setter 方法java.lang.reflect.Method.invoke。与 一起工作时enctype="multipart/form-data",Enumeration<String> e = request.getParameterNames()是问题所在。没有找到元素,所以introspecthelper永远不会执行。因为JspRuntimeLibrary.introspect是静态方法。我不能覆盖它的行为。那么,编写自定义标签或遵循如何使用 JSP/Servlet 将文件上传到服务器?是解决问题的唯一方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java