根据Sending additional data with multipart,request.getParameter;
无法与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时,还有其他方法可以传递数据吗?
form.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>
process.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...
}
答案 0 :(得分:0)
我发现了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
将java.lang.reflect.Method.invoke
的值传递给适当的setter方法。
使用enctype="multipart/form-data"
时,Enumeration<String> e = request.getParameterNames()
是问题所在。找不到元素,因此introspecthelper
永远不会执行。
因为JspRuntimeLibrary.introspect
是静态方法。我无法覆盖其行为。因此,编写自定义标签或遵循How to upload files to server using JSP/Servlet?是解决问题的唯一方法。
答案 1 :(得分:0)
您可以使用jsp通过其他输入字段将文件上传到服务器。
例如 index.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>
upload.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。