| <FORMMETHOD="POST" ACTION="*.jsp" ENCTYPE="multipart/form-data"> <INPUT TYPE="FILE" NAME="FILE1" SIZE="50"><BR> <INPUT TYPE="SUBMIT" VALUE="Upload"> </FORM> |
| public class testUpload { public testUpload(){……} public final void initialize(&&keyword=Servlet&Submit=+%CB%D1%CB%F7+">ServletConfig config) throws &&keyword=Servlet&Submit=+%CB%D1%CB%F7+">ServletException { m_application = config.get&&keyword=Servlet&Submit=+%CB%D1%CB%F7+">ServletContext(); } public void upload() throws testUploadException, IOException, &&keyword=Servlet&Submit=+%CB%D1%CB%F7+">ServletException {………} private void getDataSection(){………} private void getDataHeader(){………} public int save (String destPathName) throws SmartUploadException, IOException, &&keyword=Servlet&Submit=+%CB%D1%CB%F7+">ServletException {………} …… } |
| //文件分隔符 -----------------------------7d226137250336 //文件信息头 Content-Disposition: form-data; name="FILE1"; filename="C:\Documents and Settings\Administrator.TIMBER-4O6B0ZZ0\My Documents\tt.sql" Content-Type: text/plain //源文件内容 create table info( content image null); //下一个文件的分隔符 -----------------------------7d226137250336 Content-Disposition: form-data; name="FILE2"; filename="" Content-Type: application/octet-stream -----------------------------7d226137250336 |
| m_totalBytes=1024;totalRead=0; for(; totalRead < m_totalBytes; totalRead += readBytes) try { m_request.getInputStream(); readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead); }catch(Exception e){ throw new SmartUploadException("Unable to upload.");} |
这里采用了循环中多字节读取方法,以上循环不断地读取数据直到数组填满为止。如果一个文件可以完全得到,则文件的所有字节也就可以全部得到。但是因为网络速度通常比CPU慢得多,所以程序很容易在所有的数据到来之前就清空网络缓冲区。实际上,多字节读取方法在试图从暂时为空但是开放的网络缓存区读取数据时,该方法会返回0,这表示没有数据存在但网络流没有关闭。这种情况下,单字节方法将阻止运行程序的执行,所以多字节的行为优于单字节read()方法的行为。接下来将分析字节数组m_binArray。首先找到分隔符;使用getDataHeader()方法返回文件信息头的值,从中确定源文件的完整路径名、源文件的扩展名和源文件文件内容格式;使用getDataSection()方法返回文件的内容数据,并记录文件数据在字节数组中的起止位置。然后生成一个File类实例,并将文件的完整路径名、源文件的扩展名、源文件文件内容格式和文件的内容数据的起止位置放到File类实例的&&属性中。找到下一个分隔符,继续重复上述过程,直至分析完毕。