<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    cAng^Er

    不懂我的人 , 離不了我 , 該了解了解我 !而懂我的人 , 更離不了我 , 因為他們愛我 。

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      30 隨筆 :: 16 文章 :: 18 評論 :: 0 Trackbacks

     用webwork框架做upload的java讀取代碼
        我的語言表達能力不是很好,說也許說不明白,可能給看的說得糊涂了,同樣
    這個對做上傳得新手很有用,不啰嗦了,先寫代碼吧:一共4個地方要弄,
    前臺,后臺,xwork和webwork.properties
    前臺:
    upload.jsp:
    =================================================================
    <%--
      Created by IntelliJ IDEA.
      User: lvjiachun
      Date: 2006-5-3
      Time: 9:32:35
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=utf-8" language="java" %>
    <%@ taglib prefix="ww" uri="/webwork" %>
    <html>
    <head><title>upload</title></head>
    <body><p align="center">
        <ww:form action="upload.action" name="upload" method="post" enctype="multipart/form-data">
            <ww:file name="file1" value="瀏覽" label="ReadFile"/>
            <ww:file name="file2" value="瀏覽" label="ReadFile"/>
            <ww:file name="file3" value="瀏覽" label="ReadFile"/>
            <ww:file name="file4" value="瀏覽" label="ReadFile"/>
            <ww:textfield name="model.fileURL" label="Road"/>  //指定上傳得文件保存到哪里
            <ww:submit/>
        </ww:form></p>
    </body>
    </html>
    =======================================================================
    upload-success.jsp
    =======================================================================
    <%--
      Created by IntelliJ IDEA.
      User: lvjiachun
      Date: 2006-5-3
      Time: 9:38:04
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=utf-8" language="java" %>
    <%@ taglib prefix="ww" uri="/webwork" %>
    <html>
    <head><title>upload-success</title></head>

    <body>
    <p align="center">
        你已經上傳成功,
        <a href="upload.jsp">請返回</a>
    </p>
    </body>
    </html>
    ========================================================================
    后臺:一共需要三個4個java文件
    1.  <FileMes>
    package upload.until;

    /**
     * Created by IntelliJ IDEA.
     * User: lvjiachun
     * Date: 2006-5-3
     * Time: 7:51:11
     * To change this template use File | Settings | File Templates.
     */
    public class FileMes {
        private String fileName;

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        private String filePath;
    }
    2.<FileSelect >
    package upload.until;

    /**
     * Created by IntelliJ IDEA.
     * User: lvjiachun
     * Date: 2006-5-3
     * Time: 7:51:23
     * To change this template use File | Settings | File Templates.
     */
    public class FileSelect {
        private String fileName;
        private String fileURL;
        private String type;

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public String getFileURL() {
            return fileURL;
        }

        public void setFileURL(String fileURL) {
            this.fileURL = fileURL;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
    3.<UploadUntil >
    package upload.until;

    import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
    import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest;
    import com.opensymphony.webwork.ServletActionContext;

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.Calendar;
    import java.io.*;

    /**
     * Created by IntelliJ IDEA.
     * User: lvjiachun
     * Date: 2006-5-3
     * Time: 7:51:00
     * To change this template use File | Settings | File Templates.
     */
    public class UploadUntil {
        public static List<FileMes> Upload(String smallPath) throws IOException {
            List<FileMes> fileMesList = new ArrayList<FileMes>();
            String path = smallPath;
            System.out.println("PATH 2:" + path);
            MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
            if (multiWrapper == null) return null;
            Enumeration e = multiWrapper.getFileNames();
            int i = 0;
    //===============================================================//
            //這個是單文件上傳的代碼;?。∪绻胱鰡挝募蟼鳎炎⑨尩舻拇a去掉就可以了
    //        while (e.hasMoreElements()) {
    //            String inputValue = (String) e.nextElement();
    //            String contentType = multiWrapper.getContentType(inputValue);
    //            String flie1 = multiWrapper.getFilesystemName(inputValue);
    //            String fileName = flie1;
    //            System.out.println("FileName underfinded:" + fileName);
    //            String savePath = path + fileName;
    //            File file = multiWrapper.getFile(inputValue);
    //            if (file != null) {
    //                InputStream inStream = new FileInputStream(file.getPath());
    //                FileMes fm = new FileMes();
    //                fm.setFileName(flie1);
    //                System.out.println("FileName:" + fm.getFileName());
    //                System.out.println("SmallPath in UploadUntil.java" + smallPath);
    //                fm.setFilePath(smallPath + fileName);
    //                System.out.println("FilePath:" + fm.getFilePath());
    //                fileMesList.add(fm);
    //                System.out.println("SavePath:" + savePath);
    //                FileOutputStream fs = new FileOutputStream(savePath);
    //                byte[]buffer = new byte[1444];
    //                int length;
    //                int bytesum = 0;
    //                int byteread = 0;
    //                while ((byteread = inStream.read(buffer)) != -1) {
    //                    bytesum += byteread;
    //                    fs.write(buffer, 0, byteread);
    //                }
    //============================================================//
    // 下面這個是多文件上傳-------------------
            while (e.hasMoreElements()) {
                String inputValue = (String) e.nextElement();
                String contentType = multiWrapper.getContentType(inputValue);
                String flie1 = multiWrapper.getFilesystemName(inputValue);
                String fileName = Calendar.getInstance().getTime().getTime() + "-" + i + flie1;
                String savePath = path + fileName;
                File file = multiWrapper.getFile(inputValue);
                if (file != null) {
                    InputStream inStream = new FileInputStream(file.getPath()); //讀入原文件
                    FileMes fm = new FileMes();
                    fm.setFileName(flie1);
                    fm.setFilePath(smallPath + fileName);
                    fileMesList.add(fm);
                    FileOutputStream fs = new FileOutputStream(savePath);
                    byte[] buffer = new byte[1444];
                    int length;
                    int bytesum = 0;
                    int byteread = 0;
                    while ((byteread = inStream.read(buffer)) != -1) {
                        bytesum += byteread; //字節(jié)數 文件大小
                        fs.write(buffer, 0, byteread);
                    }
    //==============================================================//
                    inStream.close();   //關閉輸入流
                    i++;
                    if (file.exists()) {
                        file.delete();
                    }   //如果上傳的文件存在則將其刪除;
                }

            }
            return fileMesList;
        }
    }
    4.也就是最后一個,action<UploadfilesAction>
    package upload.fileupload;

    import com.opensymphony.xwork.ActionSupport;
    import com.opensymphony.xwork.Action;
    import com.opensymphony.xwork.ModelDriven;
    import upload.until.FileSelect;

    /**
     * Created by IntelliJ IDEA.
     * User:lvjiachun
     * Date: 2006-5-3
     * Time: 7:52:34
     * To change this template use File | Settings | File Templates.
     */
    public class UploadfilesAction extends ActionSupport implements Action, ModelDriven {
        FileSelect fileMes = new FileSelect();

        public String upload() throws Exception {
            String smallPath = fileMes.getFileURL();
            System.out.println(smallPath+"xiaosao");
            java.io.File myFilePath = new java.io.File(smallPath);
            if (smallPath != null) {
                if (!myFilePath.exists()) {
                    myFilePath.mkdir();
                    System.out.println("*******MaKe DIR!*********");
                }
            }
            upload.until.UploadUntil.Upload(smallPath);
            return SUCCESS;

        }
        public Object getModel() {
            return fileMes;  //To change body of implemented methods use File | Settings | File Templates.
        }
    }
    **********************************************************************
    xwork中
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "
    http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
    <xwork>
        <include file="webwork-default.xml"/>
        <package name="upload" extends="webwork-default">
            <interceptors>
                <interceptor name="upload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>
                <interceptor-stack name="uploadStack">
                    <interceptor-ref name="upload"/>
                    <interceptor-ref name="defaultStack"/>
                </interceptor-stack>
            </interceptors>
            <default-interceptor-ref name="WSStack"/>
        
            <action name="upload" class="upload.fileupload.UploadfilesAction" method="upload">
                <result name="success">
                    <param name="location">/upload/upload-success.jsp</param>
                </result>
                <interceptor-ref name="uploadStack"/>
                <interceptor-ref name="model-driven"/>
            </action>
        </package>
    </xwork>
    webwork.properties
    在webwork.properties中要加上這段代碼
    webwork.multipart.parser=com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest
    webwork.multipart.saveDir=d:\\  #這個是上傳文件的默認保存路徑#

    xiaosao:具體的解析過程不是我寫的,是我工作室boss寫的,我也沒那個能力!具體怎么用
    看代碼就足夠了,這個是我在做上傳得時候用到的,開始做的時候沒有任何思路,在網上找
    也沒找到,憋了半個月也沒弄出來,用webwork可以得到上傳得文件,但傳得內容只有upload這
    幾個字(傳txt時候),后來才清楚是要解析過程的,后臺的前三段代碼就是解析過程,我是個新
    手,這個東西做的讓我腦袋都大了,為了大家方便,發(fā)到這里,有需要的來看一下!代碼是好
    使得,需要自己認真調試!有什么不足給點意見

    posted on 2006-05-11 09:43 cAng^Er 閱讀(1269) 評論(2)  編輯  收藏 所屬分類: |:Web View:|

    評論

    # re: 用webwork框架做upload的java讀取代碼 2006-11-30 14:01 ww
    上傳的功能好用, 我是想在上傳文件的同時,在存入數據庫中,怎么不能返回呢? 就是在你的return fileMesList;中去不到文件的名稱。為什么,我是新手,誰知道,請告訴我,先謝謝了。  回復  更多評論
      

    # re: 用webwork框架做upload的java讀取代碼 2006-12-05 14:14 cAng^Er
    @ww
    不好意思阿,好幾天沒有上博客了,今天在做新聞管理系統(tǒng),測試友情鏈接的時候才發(fā)現,你把你的聯(lián)系方式給我,或者看到了加我的QQ:6826462  回復  更多評論
      

    主站蜘蛛池模板: 国产AV无码专区亚洲AWWW| 国产美女a做受大片免费| 国产亚洲精品精品国产亚洲综合| 亚洲日韩av无码中文| 97视频免费在线| 亚洲综合色区中文字幕| 毛片在线免费视频| 亚洲欧洲免费无码| 国产精品免费_区二区三区观看| 亚洲AV无码资源在线观看| 青青草国产免费久久久下载| 亚洲国产成人无码AV在线 | 三年片在线观看免费观看大全动漫 | 亚洲国产小视频精品久久久三级 | 国产偷国产偷亚洲高清日韩| 人妻免费久久久久久久了| 久久亚洲国产精品五月天婷| 中国内地毛片免费高清| 亚洲国产精品线在线观看| 免费黄色福利视频| 日韩亚洲人成网站| 久久久久亚洲?V成人无码| 光棍天堂免费手机观看在线观看 | 亚洲性天天干天天摸| 国产精品久久永久免费| 亚洲日韩精品无码专区 | 亚洲色图古典武侠| 美女视频黄的全免费视频网站| 久久亚洲精品国产亚洲老地址| 成年美女黄网站18禁免费| 永久免费无码网站在线观看个| 国产亚洲一区二区三区在线| h视频在线观看免费网站| 亚洲国产AV无码一区二区三区| 亚洲国产成人久久综合野外| 久久精品免费视频观看| 亚洲国产欧洲综合997久久| 亚洲中文字幕不卡无码| 亚洲免费福利视频| 一区二区三区免费视频网站| 亚洲人成网www|