uploadify+strust2 文件上传

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%@ include file="/context/mytags.jsp"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="content-type" content="text/html; charset=utf-8" />  
<title>数据报表</title>  
<link href="${root}/js/uploadify-3.1/uploadify.css" rel="stylesheet" type="text/css" />  
<t:base type="jquery,jqueryui,dataTables,datePicker,lhgdialog"></t:base>  
<style>  
.lac_search span{  
margin-right: 0px;  
}  
</style>  
<script type="text/javascript" charset="utf-8">  
  
//防止客户端缓存文件,造成uploadify.js不更新,而引起的“喔唷,崩溃啦”  
document.write("<script type='text/javascript' "  
        + "src='${root}/js/uploadify-3.1/jquery.uploadify-3.1.js?" + new Date()  
        + "'></s" + "cript>");  
          
$(function(){    
    $("#uploadify").uploadify({        
        'debug'     : false, //开启调试    
        'auto'           : false, //是否自动上传       
        'swf'            : '${root}/js/uploadify-3.1/uploadify.swf',  //引入uploadify.swf      
        'uploader'       : 'oc_execute.action',//请求路径      
        'queueID'        : 'fileQueue',//队列id,用来展示上传进度的      
        'width'     : '75',  //按钮宽度      
        'height'    : '24',  //按钮高度    
  //      'queueSizeLimit' : 3,  //同时上传文件的个数      
  //      'fileTypeDesc'   : 'Excel文件',    //可选择文件类型说明    
        'fileTypeExts'   : '*.xls', //控制可上传文件的扩展名      
        'multi'          : false,  //允许多文件上传      
        'buttonText'     : '选择文件',//按钮上的文字      
        'fileSizeLimit' : '2MB', //设置单个文件大小限制       
        'fileObjName' : 'uploadify',  //<input type="file"/>的name      
        'method' : 'post',      
        'removeCompleted' : true,//上传完成后自动删除队列      
        'onFallback':function(){        
            alert("您未安装FLASH控件,无法上传!请安装FLASH控件后再试。");        
        },    
        'onUploadStart' : function(file) {//上传前执行  
           $("#uploadify").uploadify("settings","formData",{'type':$('#type').val()});//动态参数传递  
    },   
        'onUploadSuccess' : function(file, data, response){//单个文件上传成功触发      
                               //data就是action中返回来的数据      
        },'onQueueComplete' : function(){//所有文件上传完成      
            alert("文件上传成功!");    
            }      
        });    
      
});    
</script>    
</head>    
  
<body>    
<input type="file" id="uploadify" name="uploadify">      
<div id="fileQueue"></div>      
<a href="javascript:$('#uploadify').uploadify('upload','*')">开始上传</a>      
<a href="javascript:$('#uploadify').uploadify('cancel')">取消所有上传</a>      
</body>    
</html> 


package com.sr.tlm.info.action;  
  
import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.Iterator;  
import java.util.List;  
  
import javax.annotation.Resource;  
import javax.servlet.http.HttpServletRequest;  
  
import org.apache.commons.fileupload.FileItem;  
import org.apache.commons.fileupload.FileUploadBase;  
import org.apache.commons.fileupload.FileUploadException;  
import org.apache.commons.fileupload.RequestContext;  
import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
import org.apache.commons.fileupload.servlet.ServletFileUpload;  
import org.apache.commons.fileupload.servlet.ServletRequestContext;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.struts2.ServletActionContext;  
import org.springframework.stereotype.Controller;  
  
import com.framework.core.common.base.action.BaseAction;  
import com.framework.core.common.util.ExcelUtils;  
import com.sr.tlm.info.appservice.ImportExcelCompanyService;  
  
@Controller  
public class OtherCompanyInfoAction extends BaseAction{  
      
    private static final long serialVersionUID = -2520518801213531530L;  
  
    @Resource  
    private ImportExcelCompanyService importExcelCompanyService;  
      
      
    private File uploadify;//上传文件file对象    
        private String uploadifyFileName;//上传文件名    
        private String uploadifyContentType;//上传文件类型    
        private String description;//上传文件的描述    
  
       
  
    /** 
     * 上传文件保存到指定位置 
     * @return 
     */  
    public String execute(){    
        String newFileName=null;    
        //得到当前时间开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名    
        long now=new Date().getTime();    
        //得到保存上传文件的真实路径    
 //       String path=ServletActionContext.getServletContext().getRealPath(uploadDir);    
        File dir=new File("上传文件保存的路径");    
        //如果这个目录不存在,则创建它    
        if (!dir.exists()) {    
            dir.mkdir();    
        }    
        int index=uploadifyFileName.lastIndexOf(".");    
            
        //判断上传文件是否有扩展名,以时间戳作为新的文件名    
        if (index!=-1) {    
            newFileName=now+uploadifyFileName.substring(index);    
        }else {    
            newFileName=Long.toString(now);    
        }    
        BufferedOutputStream bos=null;    
        BufferedInputStream bis=null;    
            
        //读取保存在临时目录下的上传文件,写入到新的文件中    
        try {    
            FileInputStream fis=new FileInputStream(uploadify);    
            bis=new BufferedInputStream(fis);    
                
            FileOutputStream fos=new FileOutputStream(new File(dir,newFileName));    
            bos=new BufferedOutputStream(fos);    
                
            byte [] buf=new byte[4096];    
            int len=-1;    
            while ((len=bis.read(buf))!=-1) {    
                bos.write(buf,0,len);    
            }    
        } catch (FileNotFoundException e) {    
            e.printStackTrace();    
        } catch (IOException e) {  
            e.printStackTrace();    
        }finally{    
            if (null!=bis) {    
                try {    
                    bis.close();    
                } catch (IOException e) {   
                    e.printStackTrace();    
                }    
            }    
            if (null!=bos) {    
                try {    
                    bos.close();    
                } catch (IOException e) {    
                    e.printStackTrace();    
                }    
            }    
        }    
        return null;    
    }    
    
    public File getUploadify() {    
        return uploadify;    
    }    
    
    public void setUploadify(File uploadify) {    
        this.uploadify = uploadify;    
    }    
    
    public String getUploadifyFileName() {    
        return uploadifyFileName;    
    }    
    
    public void setUploadifyFileName(String uploadifyFileName) {    
        this.uploadifyFileName = uploadifyFileName;    
    }    
    
    public String getUploadifyContentType() {    
        return uploadifyContentType;    
    }    
    
    public void setUploadifyContentType(String uploadifyContentType) {    
        this.uploadifyContentType = uploadifyContentType;    
    }    
    
    public String getDescription() {    
        return description;    
    }    
    
    public void setDescription(String description) {    
        this.description = description;    
    }    
    
  
    public String getFilePath() {  
        return filePath;  
    }  
  
    public void setFilePath(String filePath) {  
        this.filePath = filePath;  
    }  
  
    public File getUpFile() {  
        return upFile;  
    }  
  
    public void setUpFile(File upFile) {  
        this.upFile = upFile;  
    }  
  
    public String getUpFileFileName() {  
        return upFileFileName;  
    }  
  
    public void setUpFileFileName(String upFileFileName) {  
        this.upFileFileName = upFileFileName;  
    }  
  
    public String getUpFileContentType() {  
        return upFileContentType;  
    }  
  
    public void setUpFileContentType(String upFileContentType) {  
        this.upFileContentType = upFileContentType;  
    }  
  
}

你可能感兴趣的:(java,jsp)