当前位置: 代码迷 >> Web前端 >> struts2 + ext 下传多文件
  详细解决方案

struts2 + ext 下传多文件

热度:194   发布时间:2012-10-06 17:34:01.0
struts2 + ext 上传多文件
upload.js
Ext.onReady(function(){
Ext.QuickTips.init();    

                  dialog = new Ext.ux.UploadDialog.Dialog({
            autoCreate: true,  
            closable: true,  
            collapsible: false,  
            draggable: true,  
            minWidth: 400,        
            minHeight: 200,  
            width: 400,  
            height: 350,  
            permitted_extensions:[
            'JPG','jpg','jpeg','JPEG',
            'GIF','gif','pdf','PDF',
            'txt','text','zip','ZIP',
            'doc','DOC','RAR','rar','xls'],  
            proxyDrag: true,  
            resizable: true,  
            constraintoviewport: true,  
            title: '文件上传',  
            url: 'Upload.action',  
            reset_on_hide: false,  
            allow_close_on_upload: true
           })
              dialog.show();

 
});

用来中文化
参考贴:http://lisanlai.iteye.com/blog/666926
本人研究很久一直没研究成功这个帖子的后台代码,贴主没发全

ru.utf-8_zh.js

Ext.apply( 
Ext.ux.UploadDialog.Dialog.prototype.i18n, 

    title: '上传头像', 
    state_col_title: '状态', 
    state_col_width: 70, 
    filename_col_title: '文件名', 
    filename_col_width: 230, 
    note_col_title: '备注', 
    note_col_width: 150, 
    add_btn_text: '添加', 
    add_btn_tip: '添加文件到上传列表', 
    remove_btn_text: '删除', 
    remove_btn_tip: '从上传列表中上传文件', 
    reset_btn_text: '重置', 
    reset_btn_tip: '重置文件上传列表', 
    upload_btn_start_text: '开始上传', 
    upload_btn_stop_text: '停止上传', 
    upload_btn_start_tip: '开始上传', 
    upload_btn_stop_tip: '停止上传', 
    close_btn_text: '关闭', 
    close_btn_tip: '关闭上传窗口', 
    progress_waiting_text: '等待……', 
    progress_uploading_text: '正在上传: {0} / {1} 上传成功', 
    error_msgbox_title: '错误信息', 
    permitted_extensions_join_str: ',', 
    err_file_type_not_permitted: '文件类型错误: 只可以上传{1}', 
    note_queued_to_upload: '等待上传', 
    note_processing: '上传中……', 
    note_upload_failed: '上传失败', 
    note_upload_success: '上传成功', 
    note_upload_error: '上传出错', 
    note_aborted: '忽略', 
    note_canceled: '取消' 

); 
upload.html
不用多说了,需要什么去网上下.自己导入
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="./ext/resources/css/ext-all.css" />
<link rel="stylesheet" href="./Ext.ux.UploadDialog/css/Ext.ux.UploadDialog.css"/>
<script type="text/javascript" src="./ext/ext-base.js"></script>
<script type="text/javascript" src="./ext/ext-all.js"></script>
<script type="text/javascript" src="./ext/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="./Ext.ux.UploadDialog/UploadDialog/Ext.ux.UploadDialog.js"></script>
<script type="text/javascript" src="./Ext.ux.UploadDialog/UploadDialog/locale/ru.utf-8_zh.js"></script>
<link rel="stylesheet" type="text/css" href="./Ext.ux.UploadDialog/UploadDialog/css/Ext.ux.UploadDialog.css" />
<script type="text/javascript" src="./Ext.ux.UploadDialog/Ext.ux.UploadDialog.js"></script>
<script type="text/javascript" src="upload.js"></script>

</head>
<body>
</body>
</html>

后台代码:

参考贴:http://hi.baidu.com/freespace520/blog/item/d789b40f2a6564226159f308.html#comment
基本上看这个贴子可以做出来后台的代码..出现的问题,该楼主已经有了答复。
public class UploadFileAction extends ActionSupport {

private File file;

private String fileFileName;

private String fileContentType;

public String getFileContentType() {
   return fileContentType;
}

public void setFileContentType(String fileContentType) {
   this.fileContentType = fileContentType;
}

public String getFileFileName() {
   return fileFileName;
}

public void setFileFileName(String fileFileName) {
   this.fileFileName = fileFileName;
}

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

public File getFile(){
   return file;
}

public String execute(){
       System.out.println("begin upload file.... ");

 
   LinkPath link = LinkPath.newInstance();//这是一个存放路径类,直接写个路径就好了,也可以用struts2配置默认路径
   String dataPath = link.getdataPath();
 
   DateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        Date date=new Date();
        String dateDir = format.format(date);
        File f = new File(dataPath+"\\"+dateDir);
       
        if(!f.exists()){
        f.mkdirs();
        }
       
   File dataFile = new File(dataPath+"\\"+dateDir+"\\"+this.getFileFileName());
 
   try {
      //将第一个参数对应的 文件 copy 到 第二个参数对应的文件中
    FileUtil.copyFile(this.file,dataFile);
 
 
    if(dataFile.exists()){
   
     String fileType = dataFile.getPath().substring

(dataFile.getPath().lastIndexOf("."),dataFile.getPath().length());
   
     if(".zip".equals(fileType)||".ZIP".equals(fileType)){
      UpZIP zip = new UpZIP();//这是一个压缩类
    
      zip.unzip(dataFile.getPath(), dataPath+"\\"+dateDir);
    
      dataFile.delete();
     }
    }
  
    Struts2Utils.renderText("{success:true,message:'上传成功'}");
   } catch (IOException e) {
    Struts2Utils.renderText("{success:flase,message:'失败'}");
    e.printStackTrace();
   }
   return null;
}
}

压缩类参考贴:http://www.blogjava.net/dreamstone/archive/2007/08/09/134986.html
当然不要压缩这个功能也没事...

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {
    static final int BUFFER = 2048;

    public static void main(String argv[]) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream("E:\\test\\myfiles.zip");
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];
            File f = new File("e:\\test\\a\\");
            File files[] = f.listFiles();

            for (int i = 0; i < files.length; i++) {
                FileInputStream fi = new FileInputStream(files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(files[i].getName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
大体就这样了,struts.xml配置,我基本上没配置多少....

接下来做附件查看和附件删除了...

能上传当然要能删除....累啊

我很菜很菜很菜很菜很菜很菜很菜很菜很菜很菜很菜很菜很菜很菜.......
  相关解决方案