为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕码人8056858 2022-07-27 21:32:21
根据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.javapublic class User {    private String name;    private String password;    private String email;    private String fileName;// getter and setter...}
查看完整描述

2 回答

?
慕娘9325324

TA贡献1783条经验 获得超5个赞

您可以使用 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>

    <%

        File file;

        int maxFileSize = 5000 * 1024;

        int maxMemSize = 5000 * 1024;

        String filePath = "D:/";


        String contentType = request.getContentType();

        if ((contentType.indexOf("multipart/form-data") >= 0)) {


            DiskFileItemFactory factory = new DiskFileItemFactory();

            factory.setSizeThreshold(maxMemSize);

            factory.setRepository(new File("c:\\temp"));

            ServletFileUpload upload = new ServletFileUpload(factory);

            upload.setSizeMax(maxFileSize);

            try {

                List fileItems = upload.parseRequest(request);

                Iterator i = fileItems.iterator();

                out.println("<html>");

                out.println("<body>");

                while (i.hasNext()) {

                    FileItem fi = (FileItem) i.next();

                    if (!fi.isFormField()) {

                        String fieldName = fi.getFieldName();

                        String fileName = fi.getName();

                        boolean isInMemory = fi.isInMemory();

                        long sizeInBytes = fi.getSize();

                        file = new File(filePath + fileName);

                        //fi.write(file);

                        out.println("Uploaded Filename: " + filePath + fileName + "<br>");

                    }else{

                        System.out.println(fi.getFieldName() +":"+fi.getString());

                    }

                }

                out.println("</body>");

                out.println("</html>");

            } catch (Exception ex) {

                System.out.println(ex);

            }

        } else {

            out.println("<html>");

            out.println("<body>");

            out.println("<p>No file uploaded</p>");

            out.println("</body>");

            out.println("</html>");

        }

    %>

</body>

</html>

您需要在 servlet jar 中包含 jar 文件 commons-fileupload。


查看完整回答
反对 回复 2022-07-27
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

我发现为什么enctype="multipart/form-data"不使用jsp:setProperty and jsp:getProperty. 当我使用 tomcat 运行时,process.jsp 生成为 process_jsp.java。


  dao.User bean = null;

  bean = (dao.User) _jspx_page_context.getAttribute("bean", javax.servlet.jsp.PageContext.PAGE_SCOPE);

  if (bean == null){

    bean = new dao.User();

    _jspx_page_context.setAttribute("bean", bean, javax.servlet.jsp.PageContext.PAGE_SCOPE);

    out.write('\n');

    org.apache.jasper.runtime.JspRuntimeLibrary.introspect(_jspx_page_context.findAttribute("bean"), request);

    out.write(' ');

    out.write(' ');

    out.write('\n');

  }

根据上面的源代码org.apache.jasper.runtime.JspRuntimeLibrary.introspect被调用。


   public static void introspect(Object bean, ServletRequest request) throws JasperException

    {

        Enumeration<String> e = request.getParameterNames();

        while ( e.hasMoreElements() ) {

            String name  = e.nextElement();

            String value = request.getParameter(name);

            introspecthelper(bean, name, value, request, name, true);

        }

    }

上面的代码映射请求参数和名称(bean 属性名称),然后introspecthelper将通过使用将值传递给适当的 setter 方法java.lang.reflect.Method.invoke。


与 一起工作时enctype="multipart/form-data",Enumeration<String> e = request.getParameterNames()是问题所在。没有找到元素,所以introspecthelper永远不会执行。


因为JspRuntimeLibrary.introspect是静态方法。我不能覆盖它的行为。那么,编写自定义标签或遵循如何使用 JSP/Servlet 将文件上传到服务器?是解决问题的唯一方法。


查看完整回答
反对 回复 2022-07-27
  • 2 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号