struts2+hibernate上传name值不同的多个文件

iamhere,always.

---幸运皮

以前的时候,好像做过任意上传多个文件的.哦,不对,应该是这样说,最最初,是上传固定的一个文件,用的struts2和hibernate,后来,做了固定上传多个的,再后来,做了不固定个数上传的,现在总结起来有这么几种情况:

<input type="file" name="upload"/>

看上面的文件代码,我们分这么几种上传的情况:

一,上传一个文件,当然,这样的话,name值只有一个

二,上传多个.上传多个其实包含了固定个数上传和不定个数的上传,一般来说,不定个数上传,那么,name的值一般都是相同的,在后台接受的时候,用一个数组接受.而对于上传固定的多个文件来说,就要遇到name值不同的情况.

接下来要讨论的问题就是上传固定多个文件,并且name值不相同的情况.

先看一下项目的结构吧:


步骤一:创建数据库表:

CREATE TABLE uploadfile (
id INT(32) NOT NULL PRIMARY KEY AUTO_INCREMENT,
path VARCHAR(255)
);

步骤二:新建一个web project,引入struts2和hibernate(这些不细说)

步骤三:看一下index.jsp文件,就是文件选择并提交的页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>upload test</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>
    <form id="form1" name="form1" method="post" enctype="multipart/form-data"  action="upload.action">
<table bgcolor="#999999" width="700" border="0" cellspacing="1" cellpadding="0">
 <tr>
    <td height="35" align="left" bgcolor="#BBBBBB">
		文件一:<input type="file" name="upload1"/>
	</td>
	</tr>
	<tr>
    <td height="35" align="left" bgcolor="#BBBBBB">
		文件二:<input type="file" name="upload2"/>
	</td>
	</tr>
  <tr>
    <td height="35" align="left" bgcolor="#BBBBBB">
		<input type="submit" name="button" id="button" value="提交" />
	</td>
	</tr>
</table>
</form>
  </body>
</html>
注意,上面name分别设置了"upload1"和"upload2".
步骤四:配置struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package namespace="/" name="" extends="struts-default">
<action name="upload" class="com.xingyunpi.action.UploadFile">
			<param name="savePath1">/upload1</param>
			<param name="savePath2">/upload2</param>
		 <result name="success">suc.jsp</result>
		</action>
		</package>
</struts>    

步骤五:用hibernate引入对数据库的操作:EntityDao.java

package com.hibernate.data;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hibernate.session.HibernateSessionFactory;

public class EntityDao {
	//获取session
	public static Session getSession(){
		return HibernateSessionFactory.getSession();
	}
	
	public static void save(Uploadfile obj){
		Session se = getSession();
		Transaction tran = se.beginTransaction();//开始事物   
	    getSession().saveOrUpdate(obj);//执行   
	    tran.commit();//提交 
		se.close();
	}
	
}

步骤六:最主要的是action部分:

package com.xingyunpi.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.hibernate.data.EntityDao;
import com.hibernate.data.Uploadfile;
import com.opensymphony.xwork2.ActionSupport;

public class UploadFile extends ActionSupport {
	private java.io.File upload1; // 客户端文件域  
    private String upload1ContentType; // 上传的文件类型  
    private String upload1FileName; // 上传的文件名  
      
    private java.io.File upload2; // 客户端文件域  
    private String upload2ContentType; // 上传的文件类型  
    private String upload2FileName; // 上传的文件名  
    
    String savePath1;
    String savePath2;
    public String execute(){
    String fileName = "";//输出流
    if(upload1!=null){
		String fileType = upload1FileName.substring(upload1FileName.lastIndexOf("."));
		fileName = (new Date().getTime()+0+1)+ fileType;
		
		//文件上传
		FileOutputStream fos;
		try {
			//上传到upload文件夹下
			fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("upload") + "\\" + fileName);
			FileInputStream fis = new FileInputStream(upload1);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
			{
				fos.write(buffer , 0 , len);
			}   
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//存储
		Uploadfile uf = new Uploadfile();
		uf.setPath("upload/" + fileName);
		EntityDao.save(uf);
		
	}//上面是上传第一个图片
	if(upload2!=null ){//文件
	String fileType = new String();
	fileType = upload2FileName.substring(upload2FileName.lastIndexOf("."));
	fileName = (new Date().getTime()+1+1)+ fileType;
//		文件上传
	FileOutputStream fos;
	try {
		//上传到upload文件夹下
		fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("upload") + "\\" + fileName);
		FileInputStream fis = new FileInputStream(upload2);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0)
		{
			fos.write(buffer , 0 , len);
		}   
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	//存储
	Uploadfile uf = new Uploadfile();
	uf.setPath("upload/" + fileName);
	EntityDao.save(uf);
	
	}//上传第二个图片
		return SUCCESS;
    }
    
    String getFilename(String name){
		   
		   /**
		    * 获取文件名的后缀
		    * @return String
		    */
		   int i = name.lastIndexOf(".")+1;
		   	return name.substring(i,name.length());
	   }
	   
	
	public String getSavePath1() {
		return savePath1;
	}
	public void setSavePath1(String savePath1) {
		this.savePath1 = savePath1;
	}
	public String getSavePath2() {
		return savePath2;
	}
	public void setSavePath2(String savePath2) {
		this.savePath2 = savePath2;
	}
	public java.io.File getUpload1() {
		return upload1;
	}
	public void setUpload1(java.io.File upload1) {
		this.upload1 = upload1;
	}
	public String getUpload1ContentType() {
		return upload1ContentType;
	}
	public void setUpload1ContentType(String upload1ContentType) {
		this.upload1ContentType = upload1ContentType;
	}
	public String getUpload1FileName() {
		return upload1FileName;
	}
	public void setUpload1FileName(String upload1FileName) {
		this.upload1FileName = upload1FileName;
	}
	public java.io.File getUpload2() {
		return upload2;
	}
	public void setUpload2(java.io.File upload2) {
		this.upload2 = upload2;
	}
	public String getUpload2ContentType() {
		return upload2ContentType;
	}
	public void setUpload2ContentType(String upload2ContentType) {
		this.upload2ContentType = upload2ContentType;
	}
	public String getUpload2FileName() {
		return upload2FileName;
	}
	public void setUpload2FileName(String upload2FileName) {
		this.upload2FileName = upload2FileName;
	}
    
}

由此需要注意的是,jsp里面配置的name和struts.xml中传递的param的name再加上action接受的"

private java.io.File upload1; // 客户端文件域

"的命名是一样的.

这样,就能用struts2把不同name的文件名上传到后台


最后加上下载整个项目的程序:

FileUpload.rar

发布了132 篇原创文章 · 获赞 32 · 访问量 30万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章