新手练习求助~!~Struts2+hibernate+spring上传文件action老是接收null,导致NullPointerException

a179626102 2012-11-14 12:12:22
新手练习求助~~Struts2+hibernate+spring上传文件action老是接收null,导致NullPointerException

UserDao.java

public interface UserDao {
public void sendEmail(Email email) throws IOException;
}
UserDaoImpl.java


public class UserDaoImpl extends HibernateTemplate implements UserDao {
private SessionFactory sessionFactory;
private Email email;
private File file;
public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public Email getEmail() {
return email;
}

public void setEmail(Email email) {
this.email = email;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void sendEmail(Email email) throws IOException{
Session session = sessionFactory.openSession();
FileInputStream in = new FileInputStream(getFile());
byte[] buffer = new byte[in.available()];
in.read(buffer);


Transaction ts = session.beginTransaction();
email.setImage(Hibernate.createBlob(buffer));
session.merge(email);
in.close();
ts.commit();
session.close();

}
}
/*********************/
LoginAction.java
public class LoginAction extends ActionSupport {
private UserDao userDao;
private Email email;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public Email getEmail() {
return email;
}

public void setEmail(Email email) {
this.email = email;
}

public String sendEmail() throws IOException{
this.userDao.sendEmail(email);
return "success";
}

}
/**************************/
Email.java

public class Email {
private long id;
private String title;
private String context;
private String receiveuser;
private String senduser;
private Blob image;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}
public String getSenduser() {
return senduser;
}
public void setSenduser(String senduser) {
this.senduser = senduser;
}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getReceiveuser() {
return receiveuser;
}
public void setReceiveuser(String receiveuser) {
this.receiveuser = receiveuser;
}
}
Email.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.lucas.po.Email" table="EMAIL">
<id name="id" type="long">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>


<property name="title" type="java.lang.String">
<column name="TITLE" length="20" />
</property>

<property name="context" type="java.lang.String">
<column name="CONTEXT" length="100" />
</property>

<property name="receiveuser" type="java.lang.String">
<column name="RECEIVEUSER" length="20" />
</property>

<property name="senduser" type="java.lang.String">
<column name="SENDUSER" length="20" />
</property>

<property name="image" type="java.sql.Blob">
<column name="IMAGE" />
</property>

</class>


</hibernate-mapping>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:12521:xe</value>
</property>
<property name="username">
<value>x5sys</value>
</property>
<property name="password">
<value>x5</value>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>

<property name="mappingResources">
<list>
<value>com/lucas/po/User.hbm.xml</value>
<value>com/lucas/po/Email.hbm.xml</value>
</list>
</property>

</bean>

<bean id="userDao" class="com.lucas.daoImpl.UserDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="LoginAction" class="com.lucas.action.LoginAction">
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
</beans>

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>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.devMode" value="true" />
<package name="login" extends="struts-default">
<action name="login" class="LoginAction">
<result>/index.jsp</result>
<result name="fail">/login.jsp</result>
</action>

<action name="update" class="LoginAction" method="update">
<result name="success">/index.jsp</result>
</action>

<action name="queryAll" class="LoginAction" method="queryAll">
<result name="success">/email.jsp</result>
</action>

<action name="sendEmail" class="LoginAction" method="sendEmail">
<result name="success">/email.jsp</result>
</action>

</package>
</struts>

email.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>email page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>

<body>
<s:form action="sendEmail" method="post" enctype="multipart/form-data">
<s:select list="users" listValue="username" listKey="username" name="email.receiveuser" label="收件人"/>
<s:textfield name="email.title" label="标题"/>
<s:textarea name="email.context" label="正文" rows="10"/>
<input name="email.senduser" value="${user.username}" type="hidden"/>
<s:file name="file" label="附件"></s:file>
<s:submit value="summit"/>
</s:form>

</body>
</html>





完全求救啊,谁能帮我看看是不是我哪里弄错了,按道理应该action怎么也接收一下嘛,但是老是null,困扰死了


...全文
1148 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sheep212 2012-12-27
  • 打赏
  • 举报
回复
jsp页面 <input type="file" name="image"> action直接写 private File image; //image要跟页面上传文件处的name一致 private String imageFileName; private String imageContentType; 然后get,set这3个属性就能获取到了
yjflinchong 2012-12-27
  • 打赏
  • 举报
回复
用action中的 file 查看一下对象。 断点调试啊
RuanJianBuLiuLei 2012-12-27
  • 打赏
  • 举报
回复
偶买噶!死代码!这么长 !怎么看!上传那么简单的东西
kindy_zh 2012-12-27
  • 打赏
  • 举报
回复
什么原因呀?请说清楚点吧。我也遇到这个问题。
a179626102 2012-11-19
  • 打赏
  • 举报
回复
问题已经解决,单个File 其实是可以的,搞到最后原来是我的odbc14.jar版本不合适,我泪奔
  • 打赏
  • 举报
回复
引用 6 楼 yao752915708 的回复:
/** * 文件上传的3个文件属性,文件,名称,类型, */ 文件上传 后台必须要有这三个参数 不然就得不到文件 报空
不是吧,我有测试 不封装 contentType 也可以的捏...
关键我是洛哥 2012-11-18
  • 打赏
  • 举报
回复
/** * 文件上传的3个文件属性,文件,名称,类型, */ 文件上传 后台必须要有这三个参数 不然就得不到文件 报空
IT0918liu 2012-11-16
  • 打赏
  • 举报
回复

public class UploadAction extends ActionSupport{
	/**
	 * 文件上传的3个文件属性,文件,名称,类型,
	 */
	private File[] face;
	private String faceFileName;
	private String faceContentType;
	//图片绝对路径
        private String path;
    
	@Override
	public String execute() throws Exception {
		String filename = "";
		// 得到文件的绝对路径
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		// 封装上传文件
		File file = new File(realpath);
		for (int i = 0; i < face.length; i++) {
		System.out.println(realpath + "----" + faceContentType);
		// 为防止文件同名,加上时间戳
		String time = String.valueOf(System.nanoTime());
		// 文件后缀名.jpg/.txt...等等
		String format = faceFileName.substring(faceFileName.lastIndexOf("."));
		// 组装得到独一无二的文件名
		filename = time + format;
		// 替换名称uploadImageFileName
		FileUtils.copyFile(face[i], new File(file, filename));
	}
        this.path = realpath+"\\"+filename;
        System.out.println(path);
        //用Session存放服务器端图片的路径
        ActionContext ac=ActionContext.getContext();
		ac.getSession().put("face",path);
		return SUCCESS;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public File[] getFace() {
		return face;
	}
	public void setFace(File[] face) {
		this.face = face;
	}
	public String getFaceFileName() {
		return faceFileName;
	}
	public void setFaceFileName(String faceFileName) {
		this.faceFileName = faceFileName;
	}
	public String getFaceContentType() {
		return faceContentType;
	}
	public void setFaceContentType(String faceContentType) {
		this.faceContentType = faceContentType;
	}
}
struts.xml配置:

<!-- 头像上传 -->
<action name="upload" class="com.chinasoft.action.UploadAction">
	<interceptor-ref name="fileUpload">
		<param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/pjpeg,image/x-png</param>
		<param name="maximumSize">20000000000</param>
	</interceptor-ref>
	<interceptor-ref name="defaultStack" />
		<result name="success">/index.jsp</result>
		<result name="input">/error.jsp</result>
</action>
血饮 2012-11-15
  • 打赏
  • 举报
回复
这么长串代码 ,晕死
a179626102 2012-11-15
  • 打赏
  • 举报
回复
求救~~~~~~~~
forgetsam 2012-11-14
  • 打赏
  • 举报
回复
你不会用struts上传文件,自己搜一下。
attached 2012-11-14
  • 打赏
  • 举报
回复
你那个File对象的注入,是不是应该写到action中呢,另外
FileInputStream in = new FileInputStream(getFile());
byte[] buffer =  new byte[in.available()];
in.read(buffer);
struts2上传文件需要这样写吗? 看了好半天代码,没看出其他猫腻

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧