cAng^Er

          不懂我的人 , 離不了我 , 該了解了解我 !而懂我的人 , 更離不了我 , 因?yàn)樗麄儛?ài)我 。

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

           用webwork框架做upload的java讀取代碼
              我的語(yǔ)言表達(dá)能力不是很好,說(shuō)也許說(shuō)不明白,可能給看的說(shuō)得糊涂了,同樣
          這個(gè)對(duì)做上傳得新手很有用,不啰嗦了,先寫代碼吧:一共4個(gè)地方要弄,
          前臺(tái),后臺(tái),xwork和webwork.properties
          前臺(tái):
          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">
              你已經(jīng)上傳成功,
              <a href="upload.jsp">請(qǐng)返回</a>
          </p>
          </body>
          </html>
          ========================================================================
          后臺(tái):一共需要三個(gè)4個(gè)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;
          //===============================================================//
                  //這個(gè)是單文件上傳的代碼;!!如果想做單文件上傳,把注釋掉的代碼去掉就可以了
          //        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);
          //                }
          //============================================================//
          // 下面這個(gè)是多文件上傳-------------------
                  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é)數(shù) 文件大小
                              fs.write(buffer, 0, byteread);
                          }
          //==============================================================//
                          inStream.close();   //關(guān)閉輸入流
                          i++;
                          if (file.exists()) {
                              file.delete();
                          }   //如果上傳的文件存在則將其刪除;
                      }

                  }
                  return fileMesList;
              }
          }
          4.也就是最后一個(gè),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:\\  #這個(gè)是上傳文件的默認(rèn)保存路徑#

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

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

          評(píng)論

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

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

          主站蜘蛛池模板: 甘南县| 张家口市| 湘潭县| 龙陵县| 清水县| 沽源县| 宿州市| 乐至县| 沈丘县| 东丰县| 淮南市| 兴文县| 凤凰县| 承德市| 工布江达县| 绵竹市| 南漳县| 襄垣县| 阆中市| 本溪| 湘乡市| 泊头市| 汉寿县| 博乐市| 巨鹿县| 宝山区| 焦作市| 茶陵县| 广汉市| 盐津县| 荥经县| 康乐县| 定边县| 泽普县| 沙田区| 江川县| 孝感市| 金坛市| 新源县| 五莲县| 乌拉特后旗|