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流,让我操作呢?
于是,就开始对O'Reilly的上传组件cos.jar的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的组件去处理了。