struts.xml文件
<struts>
<!-- 设置文件上传允许最大值 为10M-->
<constant name="struts.multipart.maxSize" value="10485760"></constant>
<!-- 配置上传文件的出错信息的资源文件 -->
<constant name="struts.custom.i18n.resources" value="cn.itcast.action.FileUploadMessage"/>
<constant name="struts.multipart.saveDir" value="/upload"/>
<package name="struts2" namespace="/" extends="struts-default">
<!-- 简单文件上传 -->
<!-- <action name="fileUpload" class="cn.itcast.action.FileUploadAction"> -->
<!-- <result name="success">/result.jsp</result> -->
<!-- </action> -->
<!-- 文件上传(限制大小和类型) -->
<action name="fileUpload" class="gzy.FileUploadAction">
<result name="success">/index.jsp</result>
<!--定义上传出错要跳转的页面 -->
<result name="input">/login.jsp</result>
<interceptor-ref name="defaultStack">
</interceptor-ref>
</action>
</package>
</struts>
login.jsp文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<s:form action="fileUpload" method="post" entype="multipart/form-data">
<s:textfield name="test" label="测试文本"></s:textfield>
<s:file label="上传文件" name="doc"></s:file>
<s:submit value="上传"/>
</s:form>
<br/>
<br/>
</body>
</html>
fileuploadaciton.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID=1L;
//提交过来的文件
private File doc;
//提交过来的file的名字
private String docFileName;
//提交过来的file的类型
private String docContentType;
private String test;
public String getTest(){
return test;
}
public void setTest(String test){
this.test=test;
}
public File getFile()
{
return doc;
}
public void setFile(File file)
{
this.doc = file;
}
public String getFileFileName()
{
return docFileName;
}
public void setFileFileName(String fileFileName)
{
this.docFileName = fileFileName;
}
public String getFileContentType()
{
return docContentType;
}
public void setFileContentType(String fileContentType)
{
this.docContentType = fileContentType;
}
public String execute() throws Exception
{
//文件输入流
InputStream is = new FileInputStream(doc);
//设置文件保存的目录
String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/upload");
//设置目标文件
File toFile = new File(uploadPath, this.getFileFileName());
//文件输出流
OutputStream os = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
int length = 0;
//读取file文件输出到toFile文件中
while(-1 != (length = is.read(buffer, 0, buffer.length)))
{
os.write(buffer);
}
//关闭输入流和输出流
is.close();
os.close();
return SUCCESS;
}
}
总是提示我的aciton类错了java.lang.NullPointerException
java.io.FileInputStream.(Unknown Source)
gzy.FileUploadAction.execute(FileUploadAction.java:56)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
就是这一行:
//文件输入流
InputStream is = new FileInputStream(doc);
求大神