【发布时间】:2013-09-08 14:01:47
【问题描述】:
我正在为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 文件上传功能
谢谢 pshinde31p>
【问题讨论】:
-
我之前确实检查过上面的帖子,但不确定如何实现相同的内容,以及我需要在我的 struts 操作中进行哪些显式修改,以便我可以从我的请求中接收文件列表,即存在于那里。请建议....
-
感谢您的及时回复。我已将上传代码从 jsp 移动到 servlet 类并编辑了原始帖子。我验证了上述所有参考资料,但仍无法上传文件。此外,由于没有记录错误,因此我很难进行必要的更改。从我上面的代码 sn-p 我可以知道需要进行哪些更改/修改。我的 Struts 操作已添加到原始帖子中。我也不能简单地绕过/忽略/避免 Struts 2 fileUpload 拦截器干扰这个实现。
-
我不明白,你为什么不使用默认的 Struts 2 上传功能?
标签: jsp file-upload struts2