1. 程式人生 > >JEE上傳檔案的IO流

JEE上傳檔案的IO流

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流,讓我操作呢?

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的元件去處理了。