日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Struts1.2實現(xiàn)動態(tài)多文件(不定個數(shù))上傳

Struts1.2依然是應(yīng)用很廣的框架,實現(xiàn)動態(tài)多文件(不定個數(shù))上傳請先看下面的代碼

關(guān)鍵代碼

< SCRIPT language=javascript type=text/javascript >
function createElement(tagName, type, name)
{
   var element = null;
   try
   {
      element = document.createElement('<' + tagName + '
name="'+name+'" size=30 onchange="add();"/>');
      element.type = type;
      element.value = value;
   }
   catch (e)
   {
   }
   if ( ! element)
   {
      element = document.createElement(tagName);
      element.setAttribute("type", type);
   }
   return element;
}
// 動態(tài)創(chuàng)建表單控件的方法
var i = 1;
function add()
{
   // 動態(tài)創(chuàng)建控件名稱
   var name = "attachment" + i;
   var input1 = createElement("input", "file", name);
   var br = document.createElement("
");
   var files = document.getElementById("files");
   files.appendChild(br);
   files.appendChild(input1);
   i ++ ;
}
< /SCRIPT>

< DIV id=files>
< INPUT onchange=add(); alt=選擇后即可動態(tài)添加文件域 size=30 type=file name=attachment0>
< INPUT onclick=add(); value=多個附件 alt=手動添加文件域 type=button >
 支持的文件類型(.rar,.zip,.txt,.sql,.ini,.jpg,.bmp,.gif)

Action中的關(guān)鍵代碼:

FormFile[] formFile = null;
        // 得到所有的文件請求元素
        Hashtable files = bbsForm.getMultipartRequestHandler()
                .getFileElements();
        if (files != null && files.size() > 0)
        {
            // 初始化FormFile
            formFile = new FormFile[files.size()];
            // 得到files的keys
            Enumeration enums = files.keys();
            String fileKey = null;
            int i = 0;
            // 遍歷枚舉
            while (enums.hasMoreElements())
            {
                // 取得key
                fileKey = (String) (enums.nextElement());
                System.out.println("key:" + fileKey);
                // 初始化每一個FormFile(接口)
                formFile[i] = (FormFile) files.get(fileKey);
                // 分別上傳
                upload(formFile[i], request, bbs);
                i++;
            }

        }

upload方法:

/** *//**
     * 文件上傳的方法
     *
     * @param file
     * @param request
     * @param bbs
     */
    public void upload(FormFile file, HttpServletRequest request, Bbs bbs)
    {
        //只有選擇了文件時才上傳
        if ("".equals(file.getFileName()) || null == file.getFileName())
        {
            return ;
        }
        // 得到當(dāng)前網(wǎng)站的絕對路徑
        String path = this.getServlet().getServletContext().getRealPath("/");
        FileOutputStream fileOutput;
        try
        {
            // 文件操作
            fileOutput = new FileOutputStream(path + "main/upload/"
                    + file.getFileName());
            fileOutput.write(file.getFileData());
            fileOutput.flush();
            fileOutput.close();
            log.info("BbsAction:附件上傳成功");
        }
        catch (FileNotFoundException e)
        {
            log.info("BbsAction:找不到文件");
        }
        catch (IOException e)
        {
            log.info("BbsAction:文件IO異常");
        }
        // 數(shù)據(jù)庫操作
        Fileupload fileupload = new Fileupload();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + request.getContextPath()
                + "/";
        fileupload.setFilename(basePath + "/main/upload/" + file.getFileName());
        // 得到文件的擴展名
        int point = file.getFileName().lastIndexOf(".");
        String ext = file.getFileName().substring(point + 1);
        //擴展名處理
        if ("jpg".equals(ext.toLowerCase()) || "bmp".equals(ext.toLowerCase())
                || "gif".equals(ext.toLowerCase()))
        {
            ext = "img";
        }
        if ("rar".equals(ext.toLowerCase()) || "zip".equals(ext.toLowerCase())
                || "jar".equals(ext.toLowerCase()))
        {
            ext = "rar";
        }
        if("txt".equals(ext.toLowerCase()) || "sql".equals(ext.toLowerCase()) || "ini".equals(ext.toLowerCase()))
        {
            ext = "txt";
        }
        fileupload.setFileext(ext);
        fileupload.setFilesize(file.getFileSize());
        fileupload.setUptime(new Date());
        fileupload.setDowncount(0);
        fileupload.setUpuser(((User) request.getSession().getAttribute("user"))
                .getUserName());
        fileupload.setFlag((short) 0);
        fileupload.setAnnounceId(bbs.getAnnounceId());
        fileupload.setName(file.getFileName());
        fileuploadManager.save(fileupload);
        log.info("BbsAction:附件信息已經(jīng)成功保存到數(shù)據(jù)庫");
    }

這樣就實現(xiàn)的動態(tài)的多文件上傳.關(guān)鍵還是

Hashtable files = bbsForm.getMultipartRequestHandler()
                .getFileElements();

的處理.

您正在閱讀的是“Struts1.2實現(xiàn)動態(tài)多文件(不定個數(shù))上傳”


網(wǎng)頁標(biāo)題:Struts1.2實現(xiàn)動態(tài)多文件(不定個數(shù))上傳
本文地址:http://m.5511xx.com/article/djhdpeg.html