我正在为 excel 即 xls 类型文档实现文件上传,在我提交 upload.parseRequest 时选择文件后返回空列表。
List<FileItem> items = (List<FileItem>)upload.parseRequest(request)
以下是供审查的 CommonsFileUploadServlet.java -
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class CommonsFileUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File fullFile = null;
FileItem item = null;
int FILE_SIZE = 2097152;
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = (List<FileItem>) upload.parseRequest(request);
System.out.println("Number of fields: " + items.size());
Iterator<FileItem> itr = items.iterator();
String msg = "";
while (itr.hasNext()) {
try {
item = (FileItem) itr.next();
if (item.isFormField()) {
String fieldName = item.getFieldName();
if (fieldName.equals("name")) {
request.setAttribute("msg", "Thank You: " + item.getString());
}
} else {
String path = request.getParameter("path");
String field = request.getParameter("field");
System.out.println("path >>" + path + "<br/><br/>");
fullFile = new File(item.getName());
String imageName = fullFile.getName();
File f = new File(path);
f.mkdirs();
File savedFile = new File(path + "/", imageName);
fullFile = uniqueFile(savedFile, path);
item.write(fullFile);
msg = "file uploaded successfully";
}
} catch (Exception e) {
e.printStackTrace();
msg = "Problem occured while uploading file.<br>Please try again";
}
}
} else {
System.out.println("not a multipart request");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public File uniqueFile(File f,String pa) {
return f;
}
}
dataMigration.jsp 如下 -
<body>
<center>
<form action="upload.jsp?path=<%=path%>&field=<%=field%>" name="form" method="POST" enctype="multipart/form-data">
<b><font face=verdana size=2>Upload File:<br><br></font></b>
<input type="file" name="uploadFile" id="uploadFile"/><br><br>
<input type="submit" name="Submit" value="Submit" onclick="return callCheck();"/>
</form>
</center>
</body>
请找到调试分析如下 -
- 在变量下的调试模式下,我可以看到请求变量在多文件表中有文件
- 但 items 变量为空 i/e modcount = 0 & size = 0
web.xml 文件 -
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>MySystem</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.emsproject</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<listener>
<description>sessionListener</description>
<listener-class>com.emsproject.action.common.SessionListener</listener-class>
</listener>
<context-param>
<param-name>tilesDefinitions</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/jsp/common/index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SysInfo</servlet-name>
<servlet-class>com.emsproject.action.common.SysInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SysInfo</servlet-name>
<url-pattern>/eapp/*</url-pattern>
</servlet-mapping>
</web-app>
我尝试了许多可能的替代方案,但 parseRequest 方法返回空列表
请在这方面帮助我,因为我整天都在努力解决这个问题:(我不想使用 s:file 即 struts fileupload 功能
谢谢pshinde31