⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1010.html

📁 著名的linux英雄站点的文档打包
💻 HTML
📖 第 1 页 / 共 3 页
字号:
    {   m_application = config.getServletContext();    }<br>
	public void upload()    throws testUploadException, IOException, ServletException<br>
	{………}<br>
private void getDataSection(){………}<br>
private void getDataHeader(){………}<br>
public int save (String destPathName)<br>
throws SmartUploadException, IOException, ServletException<br>
{………}<br>
	……<br>
}<br>
通过initialize()方法初始化Servlet的运行环境。使用upload()方法获得输入流,并分析上传文件的格式,并将各个上传文件的属性赋给多个File类实例处理,这些File类实例由Files类管理。File类根据各文件的属性调用它的save ()方法将多个文件依次输出服务器端的目标文件中。其中upload()方法是关键,用于分析http1.1协议传送文件的格式。经过测试,我们得出传输流文件的格式,这对理解upload()方法很有用。例如,上传我的文档	t.txt文件。格式如下:<br>
//文件分隔符<br>
-----------------------------7d226137250336 <br>
//文件信息头<br>
Content-Disposition: form-data; name="FILE1"; filename="C:Documents and SettingsAdministrator.TIMBER-4O6B0ZZ0My Documents	t.sql"<br>
Content-Type: text/plain<br>
//源文件内容 <br>
create table info(<br>
content image null);<br>
//下一个文件的分隔符<br>
-----------------------------7d226137250336<br>
Content-Disposition: form-data; name="FILE2"; filename=""<br>
Content-Type: application/octet-stream<br>
-----------------------------7d226137250336<br>
    从以上文件我们可以看出,HTTP协议在上传多个文件时,是将文件全部放到输入流并以一定的分隔符来区分的。实际上upload()方法就是要分析上面的文件,确定分隔符的内容、各个文件的内容格式、文件的完整路径名称、及其文件的实际数据的始末位置。这里需要说明的一点是分隔符是随机的,它是传输流文件的第一个回车符之前的所有字符。<br>
	Upload()方法的实现流程是:首先将输入流文件输出到字节数组m_binArray中,通过下面的代码实现。<br>
m_totalBytes=1024;totalRead=0;<br>
for(; totalRead &lt; m_totalBytes; totalRead += readBytes)<br>
try<br>
{   m_request.getInputStream();<br>
    readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);<br>
}catch(Exception e){  throw new SmartUploadException("Unable to upload.");}<br>
这里采用了循环中多字节读取方法,以上循环不断地读取数据直到数组填满为止。如果一个文件可以完全得到,则文件的所有字节也就可以全部得到。但是因为网络速度通常比CPU慢得多,所以程序很容易在所有的数据到来之前就清空网络缓冲区。实际上,多字节读取方法在试图从暂时为空但是开放的网络缓存区读取数据时,该方法会返回0,这表示没有数据存在但网络流没有关闭。这种情况下,单字节方法将阻止运行程序的执行,所以多字节的行为优于单字节read()方法的行为。接下来将分析字节数组m_binArray。首先找到分隔符;使用getDataHeader()方法返回文件信息头的值,从中确定源文件的完整路径名、源文件的扩展名和源文件文件内容格式;使用getDataSection()方法返回文件的内容数据,并记录文件数据在字节数组中的起止位置。然后生成一个File类实例,并将文件的完整路径名、源文件的扩展名、源文件文件内容格式和文件的内容数据的起止位置放到File类实例的属性中。找到下一个分隔符,继续重复上述过程,直至分析完毕。<br>
2   采用FTP协议实现多个文件的上传<br>
FTP协议是Internet上用来传送文件的协议,规定了Internet上文件互相传送的标准。在java中实现这一功能是借助FtpClient类完成的。具体实现过程:首先与FTP服务器建立连接;初始化文件的传输方式,包括ASCII和BINARY两种方式;将文件输出到文件输入流FileInputStream中;FileInputStream中的数据读入字节数组中;字节数组中的数据写入输出流TelnetOutputStream(利用write方法将数据写入到一个网络链接上)。这样和源文件同名的一个文件就复制到了服务器端。本例的JavaBean中通过connectServer()、upload()和closeConnect()三个方法完成文件上传过程。主要实现如下:<br>
public class ftpUpload<br>
{   String filename;String filename1;FtpClient ftpClient;<br>
	public void connectServer(string server,string user,string password,string path)<br>
	{<br>
	  //server:FTP服务器的IP地址;user:登录FTP服务器的用户名<br>
	  //password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径<br>
		try{ ftpClient=new FtpClient();<br>
			ftpClient.openServer(server);<br>
			ftpClient.login(user, password);<br>
			System.out.println("login success!");<br>
		    if (path.length()!=0) ftpClient.cd(path);<br>
			ftpClient.binary(); }catch (IOException ex) {System.out.println(ex);}<br>
	}<br>
	public void closeConnect()<br>
	{try{  ftpClient.closeServer();<br>
		}catch (IOException ex) {System.out.println(ex);}<br>
	}<br>
	public void upload() <br>
	{   filename1=findFileName(filename);<br>
//从filename中分析出文件的名称,作为目标文件的名称,具体方法实现未给出<br>
		try {<br>
			TelnetOutputStream os=ftpClient.put(filename1);<br>
			java.io.File file_in=new java.io.File(filename);<br>
			FileInputStream is=new FileInputStream(file_in);<br>
			byte[] bytes=new byte[1024];<br>
			int c;<br>
			while ((c=is.read(bytes))!=-1){  os.write(bytes,0,c);  }<br>
			is.close();  os.close();<br>
			} catch (IOException ex) {System.out.println(ex);}<br>
	  }<br>
	}<br>
connectServer()完成与FTP服务器建立连接的功能,使用FtpClient的openServer(string server)方法打开远程FTP服务器,然后使用FtpClient的login(user, password)方法登录服务器。登录远程FTP服务器有两种方式,一种是注册用户登录,另一种是以匿名方式登录。前者要求用户首先注册为服务器的客户,服务器会给客户一个登录账号和密码,依据账号和密码连结到服务器上。后者要求用户不用注册而使用特殊的用户名"annoymous"和"guest"有限制的访问远程主机的公开文件,现在许多系统要求用户将Email地址作为口令。出于安全的目的,大部分匿名FTP主机一般只允许远程用户下载文件,而不允许上传,这将依赖于FTP服务器的设置。用户可根据实际情况选择使用两种方式。登录完成后使用FtpClient的binary()方法初始化传输方式为字节方式。upload()完成文件的上传功能。创建源文件的文件输入流FileInputStream,将输入流写入到字节数组中,利用TelnetOutputStream的write方法将字节数组中的数据写入到一个网络链接上。由于TelnetOutputStream打开的是FTP服务器上的一个文件,所以数据写入到了目标文件中,这样就完成了文件上传。closeConnect()要求与服务器断开连接。<br>
以上只是单个文件上传的过程,如果是多个文件可以多次调用此上传过程。由以上两种方式我们可以看出采用FTP协议实现多个文件的上传比较简单,容易实现。利用FTP协议上传文件一般是编写的客户端的程序,服务器端的安全设置会比较复杂;而利用HTTP协议上传文件则是服务器端的应用程序,相对来说安全设置会比较简单。并且通过测试发现FTP上传方式在传输大文件时速度是HTTP上传方式的几十倍甚至几百倍,但在传输小于1M的文件时却比HTTP上传方式稍慢一些。所以说两种传输方式各有优势,请读者根据自身情况量力而行。如果有什么问题或者是需要其他部分的源码,请与我联系!<br>
<br>
 参考文献:<br>
廖若雪,《Jsp高级编程》,机械工业出版社,2001.3<br>
黄理、洪亮等,《Jsp高级编程》,北京希望电子出版社,2001.10<br>
Elliotte Rusty Harold著,刘东华、王巍、唐刚译,Java网络编程,中国电力出版社,2001.8<br>
Bruce Eckel著,京京工作室译,Java编程思想,机械工业出版社,1999.4<br>
http://www.jspsmart.com<br>
</FONT><br>
                                      </TD>
                                    </TR>
                                <TR>
                                <TD colSpan=2><FONT 
                                class=middlefont></FONT><BR>
                                        <FONT 
                                class=normalfont>全文结束</FONT> </TD>
                                    </TR>
                                <TR>
                                <TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2 
                                height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
                        <TD vAlign=top width="20%" 
                      background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2> 
                          <DIV align=center> 
                            <table class=tableoutline cellspacing=1 cellpadding=4 
                        width="100%" align=center border=0>
                              <tr class=firstalt> 
                                <td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
                                <font class=normalfont><b>所有分类</b></font></td>
                              </tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td>    </tr>  </table></td></tr>                            </table>
                          </DIV></TD></TR>
                    <TR vAlign=top>
                        <TD width="80%"> 
                          <DIV align=center><BR>
                          </DIV>
                        </TD></TR></TBODY></TABLE></TD></TR>
                </TABLE></TD></TR>
          </TABLE>
      <TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee 
        border=0><TBODY>
        <TR>
          <TD width="50%">
              <P><FONT class=middlefont>版权所有 &copy; 2004 <A 
            href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
                违者必究. </FONT></P>
            </TD>
          <TD width="50%">
              <DIV align=right><FONT class=middlefont>Powered by: <A 
            href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
            </TD></TR></TBODY></TABLE>
      <CENTER></CENTER></TD></TR>
    </TABLE></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -