Struts 2文件上传 - 空指针异常 [英] Struts 2 File Upload - Null Pointer Exception
问题描述
filename
和文件内容类型都以 null
出现。我试图寻找这个问题,但没有结果。以下是我的文件上传代码。 <%@ taglib prefix =suri =/ struts-tags%>
< html>
< head>
< s:head />
< / head>
< s:actionerror />
< s:form action =uploadActionmethod =POSTenctype =multipart / form-data>
< s:file name =fileUploadlabel =选择文件size =40/>
< s:submit value =Uploadname =submit/>
< / s:form>
Struts.xml
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE struts PUBLIC
- // Apache Software Foundation // DTD Struts Configuration 2.0 // EN
http://struts.apache.org/的DTD /支杆-2.0.dtd>
< struts>
< include file =struts-default.xml/>
< package name =aextends =struts-default>
< action name =resultActionclass =ManagePlanDataFileUploadActionBeanmethod =executeFileUploadDemo>
< interceptor-ref name =fileUpload>
< param name =maximumSize> 10240< / param>
< param name =allowedTypes> text / plain< / param>
< / interceptor-ref>
< interceptor-ref name =defaultStack/>
< result name =success> displayResultsJSP< / result>
< / action>
< / package>
< / struts>
applicationContext-web.xml
<?xml version =1.0encoding =UTF-8?>
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :jee =http://www.springframework.org/schema/jee
xmlns:aop =http://www.springframework.org/schema/aopxmlns:tx =http:// www.springframework.org/schema/tx
xsi:schemaLocation =http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/ spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http:// www .springframework.org / schema / tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd\">
< bean id =ManagePlanDataFileUploadActionBeanscope =prototype
class =com.hix.action.planmanagement.ManagePlanDataFileUploadAction>
< / bean>
< / beans>
ManagePlanUploadAction
< pre class =lang-java prettyprint-override> package com.hix.action.planmgmt;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
导入org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class ManagePlanUploadAction extends ActionSupport {
private HttpServletRequest request;
私人档案fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public void setServletRequest(HttpServletRequest paramHttpServletRequest){
this.request = paramHttpServletRequest;
}
public HttpServletRequest getServletRequest(){
return request;
$ b $ public File getFileUpload(){
return fileUpload;
}
public void setFileUpload(File fileUpload){
this.fileUpload = fileUpload;
public String getFileUploadContentType(){
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType){
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName(){
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName){
this.fileUploadFileName = fileUploadFileName;
}
public String executeFileUploadDemo()throws Exception {
try {
String filePath =C:/ Myuploads2;
System.out.println(Server path:+ filePath);
文件fileToCreate =新文件(filePath,fileUploadFileName);
FileUtils.copyFile(fileUpload,fileToCreate);
} catch(Exception e){
e.printStackTrace();信范读范范辛亦读范辛预预读亦亦辛中中预秘
回报SUCCESS;
}
System.out.println(File:+ fileUpload);
System.out.println(Filename:+ fileUploadFileName);
System.out.println(File type:+ fileUploadContentType);
回报SUCCESS;
tiles-def.xml
$ b
<!DOCTYPE tiles-definitions PUBLIC
- // Apache Software Foundation // DTD Tiles Configuration 2.0 // EN
http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">
< tiles-definitions>
< put-attribute name =headervalue =/ jsp / template / defaultHeader.jsp/>
< put-attribute name =footervalue =/ jsp / template / defaultFooter.jsp/>
< / definition>信息信息亦读目的数信息中信息
Result.jsp
<%@ taglib prefix =suri =/ struts-tags%>
< html>
< body>
档案名称:< s:property value =fileUploadFileName/>
< br />
内容类型:< s:property value =fileUploadContentType/>
< br />
档案:< s:property value =fileUpload/>
< / body>
< / html>
现在我的问题已经解决。我不得不添加jar(commons fileupload和commons io)不仅仅是我的类路径,而且也是RAD的部署汇编。
I'm trying to upload a file using Struts2 in conjunction with Spring. But, somehow after reaching my action class, my file, filename
and file content type are all coming out as null
. I tried searching for the problem but for no results. Below is the code for my file upload.
index.jsp
:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<s:actionerror />
<s:form action="uploadAction" method="POST" enctype="multipart/form-data">
<s:file name="fileUpload" label="Choose File" size="40" />
<s:submit value="Upload" name="submit" />
</s:form>
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="a" extends="struts-default">
<action name="resultAction" class="ManagePlanDataFileUploadActionBean" method="executeFileUploadDemo">
<interceptor-ref name="fileUpload">
<param name="maximumSize">10240</param>
<param name="allowedTypes">text/plain</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success">displayResultsJSP</result>
</action>
</package>
</struts>
applicationContext-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="ManagePlanDataFileUploadActionBean" scope="prototype"
class="com.hix.action.planmanagement.ManagePlanDataFileUploadAction">
</bean>
</beans>
ManagePlanUploadAction
package com.hix.action.planmgmt;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class ManagePlanUploadAction extends ActionSupport{
private HttpServletRequest request;
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
this.request = paramHttpServletRequest;
}
public HttpServletRequest getServletRequest() {
return request;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public String executeFileUploadDemo() throws Exception {
try {
String filePath = "C:/Myuploads2";
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, fileUploadFileName);
FileUtils.copyFile(fileUpload, fileToCreate);
} catch(Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return SUCCESS;
}
System.out.println("File :" + fileUpload);
System.out.println("Filename : " + fileUploadFileName);
System.out.println("File type : " + fileUploadContentType);
return SUCCESS;
}
}
tiles-def.xml
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="displayResultsJSP" template="/jsp/planmgmt/Result.jsp">
<put-attribute name="header" value="/jsp/template/defaultHeader.jsp" />
<put-attribute name="footer" value="/jsp/template/defaultFooter.jsp" />
</definition>
</tiles-definitions>
Result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
File Name : <s:property value="fileUploadFileName"/>
<br/>
Content Type : <s:property value="fileUploadContentType"/>
<br/>
File : <s:property value="fileUpload"/>
</body>
</html>
My issue is resolved now. I had to add the jars (commons fileupload and commons io) not just to my classpath but also to "Deployment Assembly" of RAD.
这篇关于Struts 2文件上传 - 空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!