使用ExtJs的插件UploadDialog+struts2.0实现多文件上传

阅读更多
前一段时间项目需求要实现一个多文件上传的问题,项目框架为SSH,使用的是ExtJs的一个不是很成熟的插件UloadDialog上查了好多资料,发现好多都是copy有粘贴的内容,都很雷同,其中的一些问题还是要自己才找到答案,现在功能基本实现了,把我的代码贴出来供大家学习使用:
另外:咨询一个问题:使用UploadDialog能否实现一个选多个文件的?如果有哪位朋友实现了,帮忙贴出来供大家学习呀。
1.前台代码
在前台的html代码中只需要引入UploadDialog的包,在引入含下面代码的JS就可以使用了,很简单。




2.upload.js
function btn_show(){
		var subject = Ext.getCmp('subjectId').getValue();
		var role = Ext.getCmp('roleId').getValue();
		var publicSound = Ext.getCmp('publicSoundId').getValue();
		
		if(subject==""||role==""){
			Ext.MessageBox.alert('提示信息', '请先选择音频科目和角色');
		}else{
			var dialog = new Ext.ux.UploadDialog.Dialog({
					        autoCreate			: true,
							minWidth			: 450,
							minHeight			: 300,
							fileSize			: 10*1024*1024,
					        title				: '文件上传',			//是否自动上传
							permitted_extensions: ['MP3','mp3','MP4','mp4','WMA','wma'],	//允许上传的文件的类型,区分大小写
							closable			: true,     
							collapsible			: false,     
							draggable			: true,       
							proxyDrag			: true,     
							resizable			: true,     
							constraintoviewport	: true, 
							modal				: true,
							reset_on_hide		: false,
							allow_close_on_upload: true,
							url					: 'uploadAudioHandelAction.action',
							base_params			:{
													subjectID		: subject,
													roleID			: role,
													publicSound		: publicSound,
													userName		: userName
								}
				});
			dialog.show('show-button');   
		    dialog.on('uploadsuccess',onUploadSuccess);//定义上传前的回调函数
			dialog.on('uploaderror',onUploadFailed); //定义上传出错回调函数
			dialog.on('fileadd',onFileAdd);//定义添加文件时验证文件的函数
			dialog.on('uploadcomplete',onUploadComplete);//定义所有文件上传完成回调函数
		}	
};

3.行处理的java代码:
package com.dfsoft.lion.process.demo.control.model.dismodel.audio;

import java.io.File;
import java.util.Date;


import com.dfsoft.lion.commons.EagleBaseAction;
import com.dfsoft.lion.domain.demo.measurement.entity.Audio;
import com.dfsoft.lion.domain.demo.measurement.entity.AudioSubjectInfo;
import com.dfsoft.lion.domain.demo.measurement.entity.Role;
import com.dfsoft.lion.domain.demo.measurement.entity.Users;
import com.dfsoft.lion.process.demo.process.model.dismodel.AudioProcess;
import com.dfsoft.lion.process.demo.process.model.dismodel.AudioSubjectInfoProcess;
import com.dfsoft.lion.process.demo.process.model.dismodel.RoleProcess;
import com.dfsoft.lion.process.demo.process.model.dismodel.UsersProcess;


import org.apache.commons.io.FileUtils; 
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;


/**
 * @描述:上传音频处理Action
 * @author lichenglin
 *
 */
public class UploadAudioHandelAction extends EagleBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7716496253903888292L;
	
	private Long subjectID ;//科目ID
	private Long roleID ;// 角色ID
	private int publicSound;
	private String userName ;
	private String filesPath;
	//接收文件信息
	private File[] files;  
	private String[] filesFileName;
	private String[] filesContentType;
	
	transient private AudioProcess audioProcess;
	transient private RoleProcess roleProcess;
	transient private AudioSubjectInfoProcess audioSubjectInfoProcess;
	transient private UsersProcess usersProcess;
	transient private GetPath getPath;
	
	public String execute() throws Exception{
		Audio audio = new Audio() ;
		Users users = this.usersProcess.getUserByName("userName", userName);
		AudioSubjectInfo audioSubjectInfo = new AudioSubjectInfo();
		
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=GBK");   
		response.setCharacterEncoding("GBK");
		Role role = this.roleProcess.getRole(roleID);
		/*文件存放路径:为根路径+角色名*/
		String path = getPath.getFilesPath()+"/"+role.getRoleName();
		String filePath = null ; //写入数据库的文件的路径
		double audioSize = 0;    //文件大小
		
		for(int i = 0 ; i < files.length ; i++){            
			File io = new File(path,filesFileName[i]);  
	        try{  
	            /*对文件进行处理*/
	            audioSize = (files[i].length())/1024;//将文件大小从字节数(byte)转化成(KB)为单位
	            filePath = path+"/"+filesFileName[i];
	            /*音频属性*/
	            audio.setAudioName(filesFileName[i]);
	            audio.setAudioSize(audioSize);
	            audio.setDownloads(0);	//初始化下载次数为0
	            audio.setFilePath(filePath);
	            if(publicSound==2){
	            	audio.setIsPublicSound(false);//2表示“否”
//	            	System.err.println("否");
	            }else if(publicSound==1){
	            	audio.setIsPublicSound(true);//1表示“是”
//	            	System.err.println("是");
	            }
	            audio.setRoleID(roleID);		//音频上传人
	            audio.setUploadDate(new Date());//音频上传时间为当前时间
	            audio.setUserID(users.getUserID());
	            
	            /*判断当前音频是否已存在*/
	            Audio temp_audio = this.audioProcess.getAudioByName("audioName",filesFileName[i]);
	            if(temp_audio==null){   //所上传的音频尚不存在
	            	/*上传音频到服务器*/
	            	FileUtils.copyFile(files[i],io);
	            	this.audioProcess.addAudio(audio,subjectID);	            		
            		response.getWriter().write("{success:true,message:'上传新文件成功'}");
	            }else if(temp_audio!=null){
	            	audioSubjectInfo.setAudioID(temp_audio.getAudioID());
	            	audioSubjectInfo.setSubjectID(subjectID);
	            	AudioSubjectInfo temp_auAudioSubjectInfo = this.audioSubjectInfoProcess.getAudioSubjectInfo(audioSubjectInfo);
	            	if(temp_auAudioSubjectInfo==null){
	            		/*该音频已经上传,但未关联该科目,则直接向关系表中添加一条关系数据*/
	            		this.audioSubjectInfoProcess.addAudioSubjectInfo(audioSubjectInfo);
	            		response.getWriter().write("{success:true,message:'上传文件成功'}");
	            	}
	            	else{
	            		/*同音频同科目已经被上传,则不再上传,直接返回false*/
	            		response.getWriter().write("{success:true,message:'该文件已经被上传'}"); 
	            	}
	            }
	        }catch (Exception e) {
	        	response.getWriter().write("{success:false,message:'上传文件失败'}");
	            e.printStackTrace();  
	        }  
		}  
	return NONE;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setUsersProcess(UsersProcess usersProcess) {
		this.usersProcess = usersProcess;
	}

	public void setAudioSubjectInfoProcess(
			AudioSubjectInfoProcess audioSubjectInfoProcess) {
		this.audioSubjectInfoProcess = audioSubjectInfoProcess;
	}
	
	public void setRoleProcess(RoleProcess roleProcess) {
		this.roleProcess = roleProcess;
	}

	public Long getSubjectID() {
		return subjectID;
	}
	
	public void setSubjectID(Long subjectID) {
		this.subjectID = subjectID;
	}
	
	public Long getRoleID() {
		return roleID;
	}
	
	public void setRoleID(Long roleID) {
		this.roleID = roleID;
	}
	
	public int getPublicSound() {
		return publicSound;
	}
	
	public void setPublicSound(int publicSound) {
		this.publicSound = publicSound;
	}
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
	public void setAudioProcess(AudioProcess audioProcess) {
		this.audioProcess = audioProcess;
	}
	
	public File[] getFiles() {
		return files;
	}
	
	public String[] getFileFilesName() {
		return filesFileName;
	}
	
	public String[] getFilesContentType() {
		return filesContentType;
	}
	
	public void setFiles(File[] files) {
		this.files = files;
	}
	
	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}
	
	public void setFilesContentType(String[] filesContentType) {
		this.filesContentType = filesContentType;
	}

	public String getFilesPath() {
		return filesPath;
	}

	public void setFilesPath(String filesPath) {
		this.filesPath = filesPath;
	}

	public void setGetPath(GetPath getPath) {
		this.getPath = getPath;
	}
	
}


当然,其中还有一些其他的参数,请各位在参考的时候灵活些。

另外:注意,在前面dialog生成时,我并没有给他fileName 我尝试过写fileName属性,可是那样会出现java代码中fileName=null的错误,经过多次尝试,我就把那个去掉了,后面实现也没问题了。
还有:java代码中,response返回的字符串是必须的,如果没有写返回,则前面dialog的grid框中总是提示上传错误。因为没有收到success的标识。
  • UploadDialog.rar (241.5 KB)
  • 下载次数: 87

你可能感兴趣的:(ExtJs,UploadDialog,struts2.0)