日期:2014-05-16  浏览次数:20351 次

关于JSP中的文件上传
最近因为项目需要,做了一个XLS文件上传.但不知道如何下手.就拿来别人的代码看了一下.如下:
首先,在application-data.xml中添加:
<bean id="attachmentDao" class="com.ibm.process.persistence.dao.ProcessAttachmentDAO">
		<property name="sessionFactory">
			<ref local="sessionFactory"/>
		</property>
	</bean>

这里用到了一个设计模式――工厂模式,用户程序从工厂类SessionFactory中取得Session的实例,可以用来刷新数据库.
SessionFactory在Hibernate中实际起到了一个缓冲区的作用,它缓冲了Hibernate自动生成的SQL语句和一些其它的映射数据,还缓冲了一些将来有可能重复利用的数据。

在com.ibm.process.persistence.dao.ProcessAttachmentDAO"中
/*
 * 创建日期: 2006-4-29
 * Version: 1.0
 */
package com.ibm.process.persistence.dao;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibm.process.persistence.pojo.ProcessAttachment;

/**
 * 
 * TJMCC EIP II Copyright:IBM BCS
 * 
 * @author fengfei
 * 
 */
public class ProcessAttachmentDAO extends HibernateDaoSupport {

//查询
public ProcessAttachment query(Integer fileId) {
		java.util.List list = this.getHibernateTemplate().find(
				"from ProcessAttachment a where a.fileId=?", fileId);
		if (list == null || list.size() == 0)
			return null;
		return (ProcessAttachment) list.get(0);
	}

//增加保存
	public void add(ProcessAttachment attachment) {
		this.getHibernateTemplate().save(attachment);
	}

	public void delete(ProcessAttachment attachment) {
		this.getHibernateTemplate().delete(attachment);
	}

//修改
	public void modify(ProcessAttachment attachment) {
		this.getHibernateTemplate().update(attachment);
	}

//查询多个
	public List queryAttachmentList(Integer processId) {
		List list = null;
		if (processId != null && !"".equals(processId)) {
			list = this.getHibernateTemplate()
					.find("from ProcessAttachment pc where pc.processId=?",
							processId);
		}
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				ProcessAttachment temp = (ProcessAttachment) list.get(i);
				temp.setTempFileName(temp.getFileName().substring(14,
						temp.getFileName().length()));
			}

		}
		return list;
	}

	public List ListProcessAttachment(Integer processId) {
		List list = null;
		if (processId != null && !processId.equals("")) {
			list = this.getHibernateTemplate()
					.find("from ProcessAttachment pc where pc.processId=?",
							processId);
		}
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				ProcessAttachment temp = (ProcessAttachment) list.get(i);
				temp.setTempFileName(temp.getFileName().substring(14,
						temp.getFileName().length()));

			}
		}
		return list;
	}

//删除
	public void removeByFormId(Integer processId) {
		List list = this.queryAttachmentList(processId);

		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				delete((ProcessAttachment) list.get(i));
			}
		}

	}

}



其次,在application-service.xml中添加:
<bean id="attachmentHandle" class="com.ibm.process.web.fileupload.ProcessAttachmentHandler">
		<property name="dao" ref="attachmentDao"/>
		<property name="upload" ref="upload"/>
	</bean>	


再次,把ProcessAttachmentHandler注入到Spring中让Action使用.
<bean name="/Apply" parent="actionTemplate">
 <property name="target">
  <bean class="Action">	
   <property name="handle" ref="attachmentHandle"/>
  </bean>
 </property>
</bean>


在Action中添加如下代码:
import com.ibm.process.web.fileupload.ProcessAttachmentHandler;

private ProcessAttachmentHandler handle;

	/**
	 * @return Returns the handle.
	 */
	public ProcessAttachmentHandler getHandle() {
		return handle;
	}

	/**
	 * @param handle
	 *            The handle to set.
	 */
	public void setHandle(ProcessAttachme