1. 程式人生 > >apache檔案上傳元件+oracle+SWFupload實現檔案上傳

apache檔案上傳元件+oracle+SWFupload實現檔案上傳

  來韓國三個月了,最近做的專案中韓國人要求上傳的檔案儲存到資料庫,讓人難以接受的是在上傳選擇檔案的時候他們要求能夠同時實現多檔案選擇並上傳,多選這個問題我以前沒做過,google一下,發現有很多辦法,最終發現了SWFupload元件,於是問題解決,現貼出程式碼以供以後備用和提供給網路朋友參考。
  上傳頁:
   <%@ page language="java" import="java.util.*" pageEncoding="EUC-KR"%>

<!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=euc-kr" />
        <title>upload page</title>
        <link href="../../style/Common.css" rel="stylesheet" type="text/css" />
        <link href="../../style/style.css" rel="stylesheet" type="text/css" />

        <script type="text/javascript" src="/js/SWFUpload.js"></script>
        <script type="text/javascript" src="/js/upload_callbacks.js"></script>
        <script type="text/javascript">
        var swfu;

        window.onload = function() {       
            // Max settings
            MAIN_FORM.FILE_CMMT.focus();
            swfu = new SWFUpload({
                create_ui : true,
                debug : false,
                upload_script : "../web/es/ES0123_Action.jsp",
                target : "SWFUploadTarget",
                flash_path : "/js/SWFUpload.swf",
                allowed_filesize : 102400,    // 30 MB
                allowed_filetypes : "*.*",
                allowed_filetypes_description : "All files...",
                file_upload_limit:10,
                browse_link_ID : "browse_link",
                upload_link_ID : "upload_link",
                flash_loaded_callback : 'swfu.flashLoaded',
                upload_file_queued_callback : "fileQueuedByCustomize",
                upload_file_start_callback : 'uploadFileStart',
                upload_progress_callback : 'uploadProgress',
                upload_file_complete_callback : 'uploadFileComplete',
                upload_file_cancel_callback : 'uploadFileCancelled',
                upload_queue_complete_callback : 'uploadQueueComplete',
                upload_error_callback : 'uploadError',
                upload_cancel_callback : 'uploadCancel',
                auto_upload : false,
                upload_file_error_callback:"whenerror"
            });
            //fix flash refresh;
            swfu.loadFlash();
        }
       </script>

        <script type="text/javascript">
         
          function doAction(){
           
           // var listingfiles = document.getElementById("fileList");
           // var rowLength = listingfiles.rows.length;
           var dis_value = document.getElementById("bg_02").style.display;
            if(dis_value=="none"){
              alert("There isn't file to upload!");
              return ;
            }
           
            MAIN_FORM.method="POST";
            MAIN_FORM.action="ES0123_Action.jsp";
            MAIN_FORM.submit();
           
         }

        function keyPress() {
            var eventKey = window.event.keyCode ;
            if( eventKey == 13 ) {
                doAction();
            }
          }
   
         document.onkeypress = keyPress ;
         
         function validate(){
          var author  = MAIN_FORM.FILE_WRITER.value;
        
           if(author==""){
             alert("The author cann't be null!");
             MAIN_FORM.FILE_WRITER.focus();
             return ;
           }
         }
        </script>
    </head>

    <body>
        <div id="Layer1" align="center">
            <form name="MAIN_FORM"
                target="hidden_frame">
                <input type="hidden" name="ACTION" value="INDEX" />
                <input type="hidden" name="IDX_ID"
                    value="<%=request.getParameter("IDX_ID")%>" />
                <input type="hidden" name="FILE_PATH"
                value="<%=request.getParameter("FILE_PATH")%>" />
                <input type="hidden" name="USERID"
                value="<%=request.getParameter("USERID")%>" />
                <input type="hidden" name="NUM" id="NUM_ID"/>
                <input type="hidden" name="NAME" id="NAME_ID"/>
                <br />
                <table width="640" align="center" border="0">
                    <tr>
                        <td width="5" height="21">
                            <img src="../../image/img_title.gif" />
                        </td>
                        <td class="title" align="left">
                            upload file
                        </td>
                        <td width="400" align="right">

                            <a href="#" id="upload_link" ><img
                                    src="../../image/btn/btn_file_upload.gif" width="80"
                                    height="23" border="0" onclick="doAction();"/> </a>
                        </td>
                    </tr>
                </table>
               
                <table width="602" height="70" cellpadding="5" cellspacing="0"
                    align="center" bordercolor="dde4f7" border="5"
                    style="border-collapse:collapse;">
                   
                    <tr>
                        <td colspan="2">
                            <table width="377" border="0" align="left" cellpadding="1"
                                cellspacing="1">
                                <tr>
                                    <td align="left">
                                        <label>
                                            SELECT_FILE:
                                        </label>
                                    </td>
                                    <td width="100%" align="left" valign="top">

                                        <div class="but_01" valign="top">
                                            <span id="show_id"></span>
                                            <input type="button" id="browse_link" name="button"
                                                class="input" value="select file" />
                                        </div>

                                        <div id="bg_02" style="display:none">
                                            <div class="note">
                                                limit:all files size is not more 250M all 5 files,one file
                                                is not more 50 M
                                            </div>
                                            <div align="right">
                                                <input type="button" id="clear_all" name="button"
                                                class="input" value="clear files"
                                                onclick="javascript:cancelQueue();" />
                                            </div>
                                            <div class="file">
                                                <span class="font_red" id="fileCount">0</span> file
                                                upload,all
                                                <span class="font_red" id="fileTotleSize">0K</span>
                                            </div>
                                            <div class="table">
                                                <table width="100%" border="0" cellpadding="5"
                                                    cellspacing="1">
                                                    <tr>
                                                        <td bgcolor="#EAEAEA">
                                                            <strong>file name</strong>
                                                        </td>
                                                        <td width="150" bgcolor="#EAEAEA">
                                                            <strong>file size</strong>
                                                        </td>
                                                        <td width="47" bgcolor="#EAEAEA">
                                                            <strong>clear</strong>
                                                        </td>
                                                    </tr>
                                                </table>
                                                <div id="box"
                                                    style="overflow:scroll; height:180px; width:100%; font-size: 12px; color: #999999; font-style: normal;">
                                                    <table id="fileList" cellpadding="5" cellspacing="1">

                                                    </table>
                                                </div>
                                            </div>
                                        </div>

                                    </td>

                                </tr>

                                <tr>
                                    <td align="left">
                                        <label>
                                            FILE_CMMT:
                                        </label>
                                    </td>
                                    <td align="left" width="100%">
                                        <label>
                                            <textarea name="FILE_CMMT" rows="4" cols="76.5" class="input" onblur="validate();"></textarea>
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="left">
                                        <label>
                                            FILE_WRITER:
                                        </label>
                                    </td>
                                    <td align="left" width="100%">
                                        <label>
                                            <input type="text" size="78" name="FILE_WRITER" class="input" value="<%=request.getParameter("USERID")%>" onblur="validate();"/>
                                        </label>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>
            </form>
        </div>
    </body>
</html>

上傳處理:
 
  <%@ page language="java" import="java.util.*" pageEncoding="EUC-KR"%>
<%@ page import="com.idhc.base.bean.MapBEAN"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileUploadException"%>

<%
    Map map = new HashMap();
    MapBEAN map2 = new MapBEAN();
    byte[] file     = null;
    String name     = null;
   
    String contentType = request.getContentType();
    if(contentType.indexOf("multipart/form-data") >= 0){
       
        DiskFileUpload fu = new DiskFileUpload();
       
        fu.setSizeMax(10 * 1024 * 1024);
   
        fu.setSizeThreshold(1024 * 1024);
       
        Integer cnt = (Integer)session.getAttribute("cnt");
        try {
            List fileItems = fu.parseRequest(request);
            Iterator iterator = fileItems.iterator();
            while (iterator.hasNext()) {
                FileItem item = (FileItem) iterator.next();
                if (!item.isFormField()) {
                    name = item.getName();
                    name = new String(name.getBytes("ISO-8859-1"),"EUC-KR");
                    file = item.get();
                    String FileType=item.getContentType();
                    String FileSize = String.valueOf(item.getSize());
                    map.put("FILE_SAV_NM",name);
                    map.put("FILE_TYPE",FileType);
                    map.put("FILE_SIZE",FileSize);
                    map.put("file",file);
                    if(cnt==null){
                       cnt = new Integer(1);
                    }else{
                      cnt = new Integer(cnt.intValue()+1);
                    }
                    session.setAttribute("cnt",cnt);
                    if(session.getAttribute("map"+cnt)!=null){
                      session.removeAttribute("map"+cnt);
                    }
                    session.setAttribute("map"+cnt,map);
                }
            }
        }catch (FileUploadException e) {
            e.printStackTrace();
        }
      
    }else{
     
        String IDX_ID    = request.getParameter("IDX_ID");
        String FILE_PATH = request.getParameter("FILE_PATH");
        String USERID    = request.getParameter("USERID");
        String FILE_WRITER = request.getParameter("FILE_WRITER");
        String FILE_CMMT   = request.getParameter("FILE_CMMT");
        map2.set("IDX_ID",IDX_ID);
        map2.set("FILE_PATH",FILE_PATH);
        map2.set("USERID",USERID);
        map2.set("FILE_WRITER",FILE_WRITER);
        map2.set("FILE_CMMT",FILE_CMMT);
        if(session.getAttribute("map")!=null){
          session.removeAttribute("map"); 
        }
        session.setAttribute("map",map2);
    }
%>

oracle存入資料庫:

public boolean uploadFile(Connection con, HttpServletRequest request,String NOTICE_ID) throws IOException {
        int NUM = Integer.parseInt(request.getParameter("NUM"));
        String NAME = request.getParameter("NAME");
        NAME = NAME.substring(0,NAME.length()-1);
        String names[] = NAME.split(";");
        HttpSession session = request.getSession(true);
        MapBEAN map = null;
        boolean boo = false;
        int cnt = 0;
       
        Map file_map = new HashMap();
        if(session.getAttribute("map")!=null){
            map = (MapBEAN) session.getAttribute("map");
        }
        for(int i=1;i<=NUM;i++){
            if(session.getAttribute("map"+i)!=null){
                file_map = (Map) session.getAttribute("map"+i);
                String FILE_CMMT   = map.getString("FILE_CMMT");
                String FILE_WRITER = map.getString("FILE_WRITER");
                String FILE_PATH   = map.getString("FILE_PATH");
                //String IDX_ID      = map.getString("IDX_ID");
                String AUDIT_ID    = map.getString("USERID");
                String FILE_SAV_NM = names[i-1];
                String FILE_SIZE   = (String) file_map.get("FILE_SIZE");
                String FILE_TYPE   = (String) file_map.get("FILE_TYPE");
                byte[] fileContent = (byte[]) file_map.get("file");
                SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
                SimpleDateFormat time = new SimpleDateFormat("HHmmss");
       
                String AUDIT_DATE = date.format(new Date());
                String AUDIT_TIME = time.format(new Date());
               
                PreparedStatement ptmt = null;
                StringBuffer query = new StringBuffer();
               
                query.setLength(0);
                query
                        .append("INSERT INTO T_STTL_DOC_LST                                                  /n");
                query
                        .append("VALUES (                                                                                  /n");
                query
                        .append("T_STTL_DOC_LST_SEQ.NEXTVAL                                               /n");
                query
                        .append(",?,?,?,?,?,?                                                                                  /n");
                query
                        .append(",?                                                                                              /n");
                query
                        .append(",?,? ,EMPTY_BLOB(),?,?,'N'                                                         /n");
                query
                        .append("   )                                                                                                 ");
                try {
       
                    ptmt = con.prepareStatement(query.toString());//insert
                    ptmt.clearParameters();
                    ptmt.setString(1, FILE_WRITER);
                    ptmt.setString(2, FILE_SAV_NM);
                    ptmt.setString(3, AUDIT_DATE);
                    ptmt.setString(4, FILE_CMMT);
                    ptmt.setString(5, FILE_PATH);
                    ptmt.setString(6, NOTICE_ID);
                    ptmt.setString(7, AUDIT_ID);
                    ptmt.setString(8, AUDIT_DATE);
                    ptmt.setString(9, AUDIT_TIME);
                    ptmt.setString(10, FILE_SIZE);
                    ptmt.setString(11, FILE_TYPE);
                    int cnt2 = ptmt.executeUpdate();
                    if(cnt2!=0){
                        cnt++;
                    }
                    closeStatement(ptmt);   
                    ptmt         = con.prepareStatement(" SELECT T_STTL_DOC_LST_SEQ.CURRVAL FROM DUAL ");//lock
                    ResultSet rs = ptmt.executeQuery();
                    int sequence = -1;
                    if (rs.next())
                    sequence = rs.getInt(1);
                    closeResultSet(rs);
                    closeStatement(ptmt);
                   
                    query.setLength(0);
                    query.append("SELECT FILE_DAT                                                               /n");
                    query.append("FROM T_STTL_DOC_LST                                                      /n");
                    query.append("WHERE FILE_ID = ?                                                               /n");
                    query.append("FOR UPDATE                                                                            ");
                    ptmt = con.prepareStatement(query.toString());//read into db
                    ptmt.setInt(1, sequence);
                    ResultSet rs2 = ptmt.executeQuery();
                   
                    while(rs2.next()){
                       
                        weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob
) rs2.getBlob(1);
                        OutputStream out = blob.getBinaryOutputStream();
                        out.write(fileContent);
                        out.close();
                    }
                    closeResultSet(rs2);
                    closeStatement(ptmt);
                   
                } catch (SQLException e) {
       
                    e.printStackTrace();
                } finally {
                    if (ptmt != null) {
                        closeStatement(ptmt);
                    }
                }
            }
            if(session.getAttribute("map"+i)!=null){
                session.removeAttribute("map"+i);   
            }
        }
        if(cnt==NUM){
            boo = true;
        }
        if(session.getAttribute("map")!=null){
            session.removeAttribute("map");   
        }
        if(session.getAttribute("cnt")!=null){
            session.removeAttribute("cnt");
        }
        return boo;
    }
   我用的是weblogic伺服器,所以在上傳的時候blob型別要用weblogic內建的檔案型別而不是oracle的
   SWFupload元件我上傳到我的資源中,供大家下載:
  http://download.csdn.net/user/guying1028

相關推薦

apache檔案元件+oracle+SWFupload實現檔案

  來韓國三個月了,最近做的專案中韓國人要求上傳的檔案儲存到資料庫,讓人難以接受的是在上傳選擇檔案的時候他們要求能夠同時實現多檔案選擇並上傳,多選這個問題我以前沒做過,google一下,發現有很多辦法,最終發現了SWFupload元件,於是問題解決,現貼出程式碼以供以後備用和

檔案控制元件Fileupload(實現檔案並寫入資料庫)

首先我們來說一下Fileupload這個檔案上傳控制元件的幾大敗筆: 1.上傳之後按F5重新整理,重複提交 2.提交以後按後退鍵Fileupload中的資訊還在 3.不支援多檔案上傳 4.上傳前不能檢測檔案大小 解決方法: 1.建立iframe在子頁面實現或者重定向語句(R

ckeditor添加自定義按鈕整合swfupload實現批量圖片

下載 了解 nbsp 文件 mouseover 去掉 dial size pro ckeditor添加自定義按鈕整合swfupload實現批量上傳圖片給ckeditor添加自定義按鈕,由於ckeditor只能上傳一張圖片,如果要上傳多張圖片就要結合ckfinder,而ckf

定製html原生元件樣式,實現相容包括IE在內的所有瀏覽器。

說起檔案上傳,撇開藉助於flash外掛或silverlight外掛或各類瀏覽器自家外掛的上傳元件不說,我們來看看瀏覽器原生的: 早些年大多數人直接使用的瀏覽器的原生上傳元件樣式,但這樣會導致不同瀏覽器下使用體驗不一致,比如下圖: 過了些時候,產品經理覺得太醜陋,各種瀏覽

Bootstrap框架---krajee外掛fileinput--最好用的檔案元件----單多張圖片互動方式三(推薦)

我們在前一章已經實現了 Bootstrap框架---Uploadify外掛----多張圖片上傳互動方式二 。本章主要關注單多張圖片上傳在Bootstrap框架中的佈局和實現。我們在之前的文章中已經在SpringMVC基礎框架的基礎上應用了BootStrap的後臺框架,在此基礎

Apache Commons FileUpload實現檔案

Commons FileUpload簡介 Apache Commons是一個專注於可重用Java元件開發的 Apache 專案。Apache Commons專案由三個部分組成: 1、Commons Proper - 可重用Java元件的儲存庫。 2、The Commons Sandbox - 用於Jav

前端元件WebUploader大檔案與Python結合的實現

Python實現大檔案分片上傳 引言 想借著這篇文章簡要談談WebUploader大檔案上傳與Python結合的實現。 WebUploader是百度團隊對大檔案上傳的前端實現,而後端需要根據不同的語言自己實現。這裡我採用Python語言的Flask框架搭建後端,

SWFUpload實現檔案DEMO

         引言:最近專案中需要用到多檔案上傳,在網上找了很多資料,最開始使用的是uploadify這個外掛,在使用的過程中各種問題,什麼Flash版本的問題,瀏覽器相容性的問題總之是一大堆,最後在眾多問題下,無奈只好放棄了。最後選擇了SWFUpload,在網上下載了

Java Web中使用JSPSmartUpload控制元件實現檔案和下載(解決了中文亂碼問題)(JSP頁面採用GBK編碼)

package edu.uestc.updown; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException

swfupload檔案控制元件的使用

swfupload是一個基於flash的多檔案上傳元件,前端是採用javascript指令碼編寫的,它與flash指令碼進行互動,以實現多檔案的上傳功能。因前端是js指令碼,所以在做web頁面整合時很方便,以下通過對swfupload自帶的demo進行一些修改,以實現簡單的多檔案上傳功能。然後再簡單介紹下

在EasyUI專案中使用FileBox控制元件實現檔案處理

我在較早之前的隨筆《基於MVC4+EasyUI的Web開發框架形成之旅--附件上傳元件uploadify的使用》Web框架介紹中介紹了基於Uploadify的檔案上傳操作,免費版本用的是Jquery+Flash實現檔案的上傳處理,HTML5收費版本的沒有試過。隨著Flash逐漸退出整個環境,很多瀏覽器都可能不

struts2實現檔案與下載功能

一、Demo介紹 基於struts2框架,實現多檔案的上傳和下載功能。 實現原理圖: 部分介面圖: 上傳成功及下載頁面: 二、主要程式碼 uploadFile.jsp:在form表單中包含一個文字框(上傳使用者的姓名)和兩個檔案上傳選項. <%@

Java Springboot結合FastDFS實現檔案以及根據圖片url將圖片至圖片伺服器

上一篇文章我們已經講解了如何搭建FastDFS圖片伺服器,環境我們準備好了現在就讓我們開始與Java結合將他應用到實際的專案中吧。本篇文章我們將會展示上傳圖片到FastDFS圖片伺服器以及通過外網的圖片url將圖片上傳至我們自己的圖片伺服器中。 1.建立springbo

Bootstrap fileinput.js,最好用的檔案元件

本篇介紹如何使用bootstrap fileinput.js(最好用的檔案上傳元件)來進行圖片的展示,上傳,包括springMVC後端檔案儲存。 一、demo 二、外掛引入 <link type="text/css" rel="stylesheet" h

yii框架實現檔案

yii框架實現檔案上傳 1.首先yii框架下載uploadFile類 2.html程式碼 <input type="file" class="file" style="display: none" name="business_license" /> 3.j

原生javascript實現檔案功能程式碼

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, ini

【SSH網上商城專案實戰13】Struts2實現檔案功能

  轉自:https://blog.csdn.net/eson_15/article/details/51366384 上一節我們做完了新增和更新商品的功能,這兩個部分裡有涉及到商品圖片的上傳,並沒有詳細解說。為此,這篇文章詳細介紹一下Struts2實現檔案上傳的功能。 1

關於myeclipse實現檔案與使用的路徑問題

在檔案上傳的時候編寫檔案儲存應該儲存到 myeclipse 的workspace的工程目錄下面,而不是放到tomcat的webapps下面。否則eclipse 無法更新檔案。 換句話講,在eclipse中新增檔案,comcat的專案檔案中可以看見新增的文體,但是反過來,在comcat的工程目錄下

AjaxFileUpload檔案元件 php jQuery ajax

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!