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

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

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

    wonderer's program

    everything will be better
    posts - 19, comments - 6, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    JEE上傳文件的IO流

    Posted on 2007-12-23 00:52 wonderer 閱讀(1446) 評論(0)  編輯  收藏 所屬分類: java

    OYM的任務中,有個要求,上傳一個Excel文件,檢查他的內容是否合法,并返回信息。

    今天想了一下,第一個要解決的問題就是上傳一個Excel文件,上傳文件的組件到挺多的,網上一搜,就有一大堆教程,但是現在并不是要上傳一個文件到服務器以作存儲之用,而是要上傳一個文件到內存里,以Java的數據結構存儲起來,并檢查,把合乎要求的數據寫到數據庫里。所以在網上的一大堆上傳文件的組件并不合用。于是又想自己寫,思路就是從客戶端那里獲取一個InputStream,然后就對這個InputStream做一系列的檢查。代碼如下:

    ServletInputStream sis =  request.getInputStream();
    InputStreamReader isr = new InputStreamReader(sis);
                 
    int ch;
    while((ch = isr.read()) != -1 ) {          
       out.println((char)ch);
    }
                 
    System.out.flush();

    結果的出去就是如下(輸出東西寫到頁面):

    -----------------------------7d7ea23120550 
    Content-Disposition: form-data; name="file1"; 
    filename="C:\Documents and Settings\Administrator\桌面\test.txt" 
    Content-Type: text/plain 
    my name is Rokey.Rokey。我的名字叫Rokey. 
    -----------------------------7d7ea23120550 Content-Disposition: form-data; 
    name="Submit" 上傳 -----------------------------7d7ea23120550--
    很明顯,這里只有
    my name is Rokey.Rokey。我的名字叫Rokey.

    對我有用,這個也正是我的文件里面的內容,其它的都是關于這些form的其它信息。對我這個程序是沒有用的。如果這里寫下去的話,還要我去分析那些是數據,哪些是form的參數。好,到現在為止,我已經打消了自己寫的念頭了。我想,那些組件都可以把上傳文件封裝得那么好,能不能利用那些庫,抽出文件的IO流,讓我操作呢?

    于是,就開始對的API看,看到里面有這么一段。

    public class MultipartParser
    extends java.lang.Object
    A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. This class uses a "pull" model where the reading of incoming files and parameters is controlled by the client code, which allows incoming files to be stored into any OutputStream. If you wish to use an API which resembles HttpServletRequest, use the "push" model MultipartRequest instead. It's an easy-to-use wrapper around this class.

    This class can receive arbitrarily large files (up to an artificial limit you can set), and fairly efficiently too. It cannot handle nested data (multipart content within multipart content). It can now with the latest release handle internationalized content (such as non Latin-1 filenames).

    It also optionally includes enhanced buffering and Content-Length limitation. Buffering is only required if your servlet container is poorly implemented (many are, including Tomcat 3.2), but it is generally recommended because it will make a slow servlet container a lot faster, and will only make a fast servlet container a little slower. Content-Length limiting is usually only required if you find that your servlet is hanging trying to read the input stram from the POST, and it is similarly recommended because it only has a minimal impact on performance.

    而且里面的API已經封裝程我想象得到的情況了。于是,我就覺得這樣我就可以完成我的功能了。于是,就寫了以下代碼:

    MultipartParser mp = new MultipartParser(request, 10 * 1024 * 1024);
    Part part;
    while ((part = mp.readNextPart()) != null) {
          if (part.isParam()) {
              // it's a parameter part
              ParamPart paramPart = (ParamPart) part;
              //out.println("param: name=" + name + "; value=" + value);
          } else if (part.isFile()) {
              FilePart filePart = (FilePart) part;
              InputStream is = filePart.getInputStream();
              InputStreamReader isr = new InputStreamReader(is);
    
              int ch;
              while ((ch = isr.read()) != -1) {
    
                  out.print((char) ch);
              }
    
              System.out.flush();
              isr.close();
              is.close();
          }
    }
                   

    出去結果如下:

    my name is Rokey.Rokey。
    我的名字叫Rokey.
    到現在,已經可以把這個流封裝成一個文件流,送給Excel的組件去處理了。
    主站蜘蛛池模板: 在线观看亚洲免费| 国产精品免费AV片在线观看| 亚洲日韩精品国产一区二区三区| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 亚洲最大av资源站无码av网址| 亚洲Aⅴ无码一区二区二三区软件| 成人免费区一区二区三区| 99re6在线视频精品免费| XXX2高清在线观看免费视频| 日本免费精品一区二区三区 | 国产美女做a免费视频软件| 97无码免费人妻超级碰碰碰碰| 久久一区二区免费播放| 国内永久免费crm系统z在线| 免费无码又爽又刺激网站| 99视频免费播放| 黄色成人免费网站| 全免费一级午夜毛片| 国产成人免费高清在线观看 | 羞羞漫画在线成人漫画阅读免费| 亚洲蜜芽在线精品一区| 亚洲一区二区三区四区视频 | 国产在线观看免费不卡| 又大又黄又粗又爽的免费视频 | 亚洲AV色欲色欲WWW| 青青草国产免费国产是公开 | 精品国产日韩亚洲一区| 国产aⅴ无码专区亚洲av麻豆 | 91嫩草国产在线观看免费| 日韩电影免费在线观看视频 | 男性gay黄免费网站| 中国一级全黄的免费观看| 最近免费中文字幕MV在线视频3 | 亚洲性一级理论片在线观看| 亚洲乱码中文论理电影| 色偷偷亚洲男人天堂| 中文字幕无线码中文字幕免费| 在线观看亚洲精品专区| 亚洲丁香色婷婷综合欲色啪| 亚洲美女精品视频| 爱情岛论坛亚洲品质自拍视频网站|