﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-FOG</title><link>http://www.blogjava.net/FOG/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 28 Apr 2026 18:59:09 GMT</lastBuildDate><pubDate>Tue, 28 Apr 2026 18:59:09 GMT</pubDate><ttl>60</ttl><item><title> commons-fileupload  简述</title><link>http://www.blogjava.net/FOG/archive/2009/12/03/304611.html</link><dc:creator>FOG</dc:creator><author>FOG</author><pubDate>Thu, 03 Dec 2009 05:46:00 GMT</pubDate><guid>http://www.blogjava.net/FOG/archive/2009/12/03/304611.html</guid><wfw:comment>http://www.blogjava.net/FOG/comments/304611.html</wfw:comment><comments>http://www.blogjava.net/FOG/archive/2009/12/03/304611.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/FOG/comments/commentRss/304611.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/FOG/services/trackbacks/304611.html</trackback:ping><description><![CDATA[&nbsp;
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Java Web</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">开发人员可以使用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件来接收浏览器上传的文件，该组件由多个类共同组成，但是，对于使用该组件来编写文件上传功能的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Java Web</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">开发人员来说，只需要了解和使用其中的三个类：</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">、</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">和</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileUploadException</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。这三个类全部位于</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">org.apache.commons.fileupload</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">包中。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&nbsp;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">查看</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文档</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">在准备实验环境时获得的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">commons-fileupload-1.0.zip</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件的解压缩目录中可以看到一个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">docs</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">的子目录，其中包含了</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件中的各个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的帮助文档，从这个文档中可以了解到各个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的使用帮助信息。打开文件上传组件</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">帮助文档中的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">index.html</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">页面，在左侧分栏窗口页面中列出了文件上传组件中的各个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的名称，在右侧分栏窗口页面的底部列出了一段示例代码，如图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.2</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">所示。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.2</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">读者不需要逐个去阅读图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.2</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中列出的各个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的帮助文档，而应该以图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.2</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中的示例代码为线索，以其中所使用到的类为入口点，按图索骥地进行阅读，对于示例代码中调用到的各个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">API</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的方法则应重点掌握。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana"><br />
<br />
<strong><span style="font-family: Verdana">1. DiskFileUpload</span></strong></span><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类</span></strong></p>
<p style="background: white; line-height: 150%"><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类是</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">文件上传组件的核心类，应用程序开发人员通过这个类来与</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">文件上传组件进行交互。但现在</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">建议使用</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">ServletFileUpload</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类</span></strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，两个类的方法类似。下面介绍</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类中的几个常用的重要方法。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.1</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">．</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setSizeMax</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setSizeMax</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于设置请求消息实体内容的最大允许大小，以防止客户端故意通过上传特大的文件来塞满服务器端的存储空间，单位为字节。其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp; public void setSizeMax</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">（</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">long sizeMax</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">）</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">如果请求消息中的实体内容的大小超过了</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setSizeMax</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的设置值，该方法将会抛出</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileUploadException</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">异常。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.2</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">．</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setSizeThreshold</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件在解析和处理上传数据中的每个字段内容时，需要临时保存解析出的数据。因为</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Java</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">虚拟机默认可以使用的内存空间是有限的（笔者测试不大于</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">100M</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">），超出限制时将会发生</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;java.lang.OutOfMemoryError&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">错误，如果上传的文件很大，例如上传</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">800M</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">的文件，在内存中将无法保存该文件内容，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件将用临时文件来保存这些数据；但如果上传的文件很小，例如上传</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">600</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">个字节的文件，显然将其直接保存在内存中更加有效。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setSizeThreshold</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于设置是否使用临时文件保存解析出的数据的那个临界值，该方法传入的参数的单位是字节。其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public void setSizeThreshold(int sizeThreshold)</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3. setRepositoryPath</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setRepositoryPath</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于设置</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setSizeThreshold</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法中提到的临时文件的存放目录，这里要求使用绝对路径。其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public void setRepositoryPath(String repositoryPath)</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">如果不设置存放路径，那么临时文件将被储存在</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">"java.io.tmpdir"</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">这个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">JVM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">环境属性所指定的目录中，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">tomcat 5.5.9</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">将这个属性设置为了</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;&lt;tomcat</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">安装目录</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&gt;/temp/&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">目录。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.4. parseRequest</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">parseRequest </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法是</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的重要方法，它是对</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">HTTP</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">请求消息进行解析的入口方法，如果请求消息中的实体内容的类型不是</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;multipart/form-data&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，该方法将抛出</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileUploadException</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">异常。</span><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">parseRequest </span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">方法解析出</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">表单中的每个字段的数据，并将它们分别包装成独立的</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">对象，然后将这些</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">对象加入进一个</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">List</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类型的集合对象中返回。</span></strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">parseRequest </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public List parseRequest(HttpServletRequest req)</span></strong></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">parseRequest </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法还有一个重载方法，该方法集中处理上述所有方法的功能，其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">parseRequest(HttpServletRequest req,int sizeThreshold,long sizeMax,</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String path)</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">这两个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">parseRequest</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法都会抛出</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileUploadException</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">异常。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.5. isMultipartContent</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">isMultipartContent</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法方法用于判断请求消息中的内容是否是</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;multipart/form-data&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类型，是则返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">true</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，否则返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">false</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">isMultipartContent</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法是一个静态方法，不用创建</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类的实例对象即可被调用，其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public static final boolean isMultipartContent(HttpServletRequest req)</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.6. setHeaderEncoding</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">由于浏览器在提交</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">表单时，会将普通表单中填写的文本内容传递给服务器，对于文件上传字段，除了传递原始的文件内容外，还要传递其文件路径名等信息，如后面的图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">所示。不管</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">表单采用的是</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;application/x-www-form-urlencoded&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">编码，还是</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;multipart/form-data&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">编码，它们仅仅是将各个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">表单字段元素内容组织到一起的一种格式，而这些内容又是由某种字符集编码来表示的。关于浏览器采用何种字符集来编码</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">表单字段中的内容，请参看笔者编著的《深入体验</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">java Web</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">开发内幕</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">——</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">核心基础》一书中的第</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">6.9.2</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">的讲解，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;multipart/form-data&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类型的表单为表单字段内容选择字符集编码的原理和方式与</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;application/x-www-form-urlencoded&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类型的表单是相同的。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">表单中填写的文本内容和文件上传字段中的文件路径名在内存中就是它们的某种字符集编码的字节数组形式，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件在读取这些内容时，必须知道它们所采用的字符集编码，才能将它们转换成正确的字符文本返回。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">对于浏览器上传给</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">WEB</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">服务器的各个表单字段的描述头内容，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件都需要将它们转换成字符串形式返回，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setHeaderEncoding </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于设置转换时所使用的字符集编码，其原理与笔者编著的《深入体验</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">java Web</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">开发内幕</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">——</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">核心基础》一书中的第</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">6.9.4</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">节讲解的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">ServletRequest.setCharacterEncoding</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法相同。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setHeaderEncoding </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public void setHeaderEncoding(String encoding)</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">其中，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">encoding</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">参数用于指定将各个表单字段的描述头内容转换成字符串时所使用的字符集编码。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">注意：如果读者在使用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件时遇到了中文字符的乱码问题，一般都是没有正确调用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">setHeaderEncoding</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的原因。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana"><br />
<br />
<strong><span style="font-family: Verdana">2. FileItem</span></strong></span><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类</span></strong></p>
<p style="background: white; line-height: 150%"><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类用来封装单个表单字段元素的数据，一个表单字段元素对应一个</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">对象，通过调用</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">对象的方法可以获得相关表单字段元素的数据。</span></strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">是一个接口，在应用程序中使用的实际上是该接口一个实现类，该实现类的名称并不重要，程序可以采用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">接口类型来对它进行引用和访问，为了便于讲解，这里将</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">实现类称之为</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类还实现了</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Serializable</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">接口，以支持序列化操作。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">对于</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;multipart/form-data&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类型的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FORM</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">表单，浏览器上传的实体内容中的每个表单字段元素的数据之间用字段分隔界线进行分割，两个分隔界线间的内容称为一个分区，每个分区中的内容可以被看作两部分，一部分是对表单字段元素进行描述的描述头，另外一部是表单字段元素的主体内容，如图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">所示。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana"> 1.3</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">主体部分有两种可能性，要么是用户填写的表单内容，要么是文件内容。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类对象实际上就是对图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中的一个分区的数据进行封装的对象，它内部用了两个成员变量来分别存储描述头和主体内容，其中保存主体内容的变量是一个输出流类型的对象。当主体内容的大小小于</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload.setSizeThreshold</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法设置的临界值大小时，这个流对象关联到一片内存，主体内容将会被保存在内存中。当主体内容的数据超过</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload.setSizeThreshold</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法设置的临界值大小时，这个流对象关联到硬盘上的一个临时文件，主体内容将被保存到该临时文件中。临时文件的存储目录由</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">DiskFileUpload.setRepositoryPath</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法设置，临时文件名的格式为</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;upload_00000005</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">（八位或八位以上的数字）</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">.tmp&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">这种形式，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类内部提供了维护临时文件名中的数值不重复的机制，以保证了临时文件名的唯一性。当应用程序将主体内容保存到一个指定的文件中时，或者在</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">对象被垃圾回收器回收时，或者</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Java</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">虚拟机结束时，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件都会尝试删除临时文件，以尽量保证临时文件能被及时清除。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">下面介绍</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类中的几个常用的方法：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.1. isFormField</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">isFormField</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于判断</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类对象封装的数据是否属于一个普通表单字段，还是属于一个文件表单字段，如果是普通表单字段则返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">true</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，否则返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">false</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。该方法的完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public boolean isFormField()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.2. getName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于获得文件上传字段中的文件名，对于图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中的第三个分区所示的描述头，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法返回的结果为字符串</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;C:"bg.gif&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。如果</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类对象对应的是普通表单字段，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法将返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">null</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。即使用户没有通过网页表单中的文件字段传递任何文件，但只要设置了文件表单字段的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">name</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">属性，浏览器也会将文件字段的信息传递给服务器，只是文件名和文件内容部分都为空，但这个表单字段仍然对应一个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">对象，此时，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法返回结果为空字符串</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">""</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，读者在调用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件时要注意考虑这个情况。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public String getName()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">注意：如果用户使用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Windows</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">系统上传文件，浏览器将传递该文件的完整路径，如果用户使用</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Linux</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">或者</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Unix</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">系统上传文件，浏览器将只传递该文件的名称部分。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">．</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getFieldName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getFieldName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于返回表单字段元素的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">name</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">属性值，也就是返回图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中的各个描述头部分中的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">name</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">属性值，例如</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;name=p1&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中的</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;p1&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getFieldName</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public String getFieldName()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.4. write</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">write</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">方法用于将</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">对象中保存的主体内容保存到某个指定的文件中。</span></strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">如果</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">对象中的主体内容是保存在某个临时文件中，该方法顺利完成后，临时文件有可能会被清除。该方法也可将普通表单字段内容写入到一个文件中，但它主要用途是将上传的文件内容保存在本地文件系统中。其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public void write(File file)</span></strong></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.5</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">．</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getString</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&nbsp;&nbsp;&nbsp; getString</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于将</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">对象中保存的主体内容作为一个字符串返回，它有两个重载的定义形式：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public java.lang.String getString()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public java.lang.String getString(java.lang.String encoding)</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp; throws java.io.UnsupportedEncodingException</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">前者使用缺省的字符集编码将主体内容转换成字符串，后者使用参数指定的字符集编码将主体内容转换成字符串。如果在读取普通表单字段元素的内容时出现了中文乱码现象，请调用第二个</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getString</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法，并为之传递正确的字符集编码名称。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.6. getContentType</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getContentType </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用于获得上传文件的类型，对于图</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">1.3</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">中的第三个分区所示的描述头，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getContentType</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法返回的结果为字符串</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;image/gif&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，即</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">&#8220;Content-Type&#8221;</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">字段的值部分。如果</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类对象对应的是普通表单字段，该方法将返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">null</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">getContentType </span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法的完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public String getContentType()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.7. isInMemory</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">isInMemory</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用来判断</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类对象封装的主体内容是存储在内存中，还是存储在临时文件中，如果存储在内存中则返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">true</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">，否则返回</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">false</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public boolean isInMemory()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">2.8. delete</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">delete</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法用来清空</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileItem</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类对象中存放的主体内容，如果主体内容被保存在临时文件中，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">delete</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">方法将删除该临时文件。尽管</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">组件使用了多种方式来尽量及时清理临时文件，但系统出现异常时，仍有可能造成有的临时文件被永久保存在了硬盘中。在有些情况下，可以调用这个方法来及时删除临时文件。其完整语法定义如下：</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">public void delete()</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana"><br />
<br />
<strong><span style="font-family: Verdana">3. FileUploadException</span></strong></span><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">类</span></strong></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">在文件上传过程中，可能发生各种各样的异常，例如网络中断、数据丢失等等。为了对不同异常进行合适的处理，</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">文件上传组件还开发了四个异常类，其中</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileUploadException</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">是其他异常类的父类，其他几个类只是被间接调用的底层类，对于</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">Apache</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">组件调用人员来说，只需<strong><span style="font-family: 宋体">对</span></strong></span><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">FileUploadException</span></strong><strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: 宋体">异常类进行捕获和处理即可</span></strong><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">。</span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana"><br />
<br />
<strong><span style="font-family: Verdana">4. ServletRequestContext</span></strong></span></p>
<p style="background: white; line-height: 150%"><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">ServletRequestContext</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">类提供访问</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">request</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">的方法。实现</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%; font-family: Verdana">RequestContext</span><span style="font-size: 10pt; color: #4b4b4b; line-height: 150%">接口。</span></p>
<img src ="http://www.blogjava.net/FOG/aggbug/304611.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/FOG/" target="_blank">FOG</a> 2009-12-03 13:46 <a href="http://www.blogjava.net/FOG/archive/2009/12/03/304611.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>prototype.js简介</title><link>http://www.blogjava.net/FOG/archive/2009/11/30/304182.html</link><dc:creator>FOG</dc:creator><author>FOG</author><pubDate>Sun, 29 Nov 2009 16:14:00 GMT</pubDate><guid>http://www.blogjava.net/FOG/archive/2009/11/30/304182.html</guid><wfw:comment>http://www.blogjava.net/FOG/comments/304182.html</wfw:comment><comments>http://www.blogjava.net/FOG/archive/2009/11/30/304182.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.blogjava.net/FOG/comments/commentRss/304182.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/FOG/services/trackbacks/304182.html</trackback:ping><description><![CDATA[<div class="ns_content">
<p><a href="http://prototype.conio.net/">prototype.js</a>是一个很强大的Javascript函数库,它可以让你很轻松的使用一些特效,实现AJAX的功能.虽然prototype.js是为了Ruby On Rails开发的,它的纯Javascript的性质也使得它很容易用在其他的网络程序中.可惜的是,Prototype.js还没有强大的文档解释,尽管它的代码非常有条理,但是也给初学者造成了一定的麻烦.作者在README里说:</p>
<p><em>Prototype is embarrassingly lacking in documentation. (The source code should be fairly easy to comprehend; I&#8217;m committed to using a clean style with meaningful identifiers. But I know that only goes so far.) </em></p>
<pre>基本用法:以Element Class为例,prototype给每个主要的分类都分成了一个Class,使用起来很方便,要产生特定的效果的话只要用new Class.function(&lt;argument&gt;)就可以了.比如:</pre>
<pre>&lt;DIV id="div1"&gt;&lt;a href="#" onclick="new Element.toggle(''div2'')"&gt;Click Me&lt;/a&gt;&lt;/DIV&gt;<br />
&lt;DIV id="div2"&gt;Hello!&lt;/DIV&gt;</pre>
<p>当点击Click Me的时候,div2就会交替隐藏或显示.注意,你可以给toggle加上无限个parameter,比如Element.toggle(''div2'',''div3'',''div4'')...</p>
<p>下面是每个重要的类和函数的解释:</p>
<p>
<table height="1194" cellspacing="0" cellpadding="0" width="943" border="1">
    <caption>prototype 1.2.0 的函数简介 </caption>
    <tbody>
        <tr>
            <td width="208">函数名</td>
            <td width="385">解释</td>
            <td width="340">举例</td>
        </tr>
        <tr>
            <td>Element.toggle</td>
            <td>交替隐藏或显示</td>
            <td>"Element.toggle(''div1'',''div2'')</td>
        </tr>
        <tr>
            <td>Element.hide</td>
            <td>隐藏</td>
            <td>"Element.hide(''div1'',''div2'')</td>
        </tr>
        <tr>
            <td>Element.show</td>
            <td>显示</td>
            <td>"Element.show(''div1'',''div2'')</td>
        </tr>
        <tr>
            <td>Element.remove</td>
            <td>删除</td>
            <td>"Element.remove(''div1'',''div2'')</td>
        </tr>
        <tr>
            <td>Element.getHeight</td>
            <td>取得高度</td>
            <td>"Element.getHeight(''div1'')</td>
        </tr>
        <tr>
            <td>Toggle.display</td>
            <td>和Element.toggle相同</td>
            <td>"Toggle.display(''div1'',''div2'')</td>
        </tr>
        <tr>
            <td>Insertion.Before</td>
            <td>在DIV前插入文字</td>
            <td>"Insertion.Before(''div1'',''my content'')</td>
        </tr>
        <tr>
            <td>Insertion.After</td>
            <td>在DIV后插入文字</td>
            <td>"Insertion.After(''div1'',''my content'')</td>
        </tr>
        <tr>
            <td>Insertion.Top</td>
            <td>在DIV里最前插入文字</td>
            <td>"Insertion.Top(''div1'',''this is a text'')</td>
        </tr>
        <tr>
            <td>Insertion.Bottom</td>
            <td>在DIV里最后插入文字</td>
            <td>"Insertion.Bottom(''div1'',''this is a text'')</td>
        </tr>
        <tr>
            <td>PeriodicalExecuter</td>
            <td>以给定频率调用一段JavaScript</td>
            <td>"PeridicalExecutor(test, 1)"这里test是Javascript的函数,1是频率(1秒).</td>
        </tr>
        <tr>
            <td>$</td>
            <td>取得一个DIV, 相当于getElementById()</td>
            <td>$(''div1'')</td>
        </tr>
        <tr>
            <td>Field.clear</td>
            <td>清空一个输入框</td>
            <td>"Field.clear(''textfield1'')</td>
        </tr>
        <tr>
            <td>Field.focus</td>
            <td>把 焦点集中在输入框上</td>
            <td>"Field.focus(''select1'')</td>
        </tr>
        <tr>
            <td>Field.present</td>
            <td>判断内容是否为空</td>
            <td>"alert(Field.present(''textfield1''))"</td>
        </tr>
        <tr>
            <td>Field.select</td>
            <td>选择输入框的内容</td>
            <td>"Field.select(''textfield1'')"</td>
        </tr>
        <tr>
            <td>Field.activate</td>
            <td>把 焦点集中在输入框上并选择输入框的内容</td>
            <td>"Field.activate(''textfield1'')"</td>
        </tr>
        <tr>
            <td>Form.serialize</td>
            <td>把表格内容转化成string</td>
            <td></td>
        </tr>
        <tr>
            <td>Form.getElements</td>
            <td>取得表格内容为数组形式</td>
            <td></td>
        </tr>
        <tr>
            <td>Form.disable</td>
            <td>disable表格所有内容</td>
            <td>Form.disable(''form1'') (这个好象不work)</td>
        </tr>
        <tr>
            <td>Form.focusFirstElement</td>
            <td>把焦点集中在表格第一个元素上</td>
            <td>Form.focusFirstElement(''form1'')</td>
        </tr>
        <tr>
            <td>Form.reset</td>
            <td>Reset表格</td>
            <td>Form.reset(''form1'')</td>
        </tr>
        <tr>
            <td>Form.Element.getValue</td>
            <td>取得表格输入框的值</td>
            <td>Form.Element.getValue(''text1'')</td>
        </tr>
        <tr>
            <td>Form.Element.serialize</td>
            <td>&nbsp;&nbsp; 把表格中输入框内容转化成string</td>
            <td>Form.Element.serialize(''text1'')</td>
        </tr>
        <tr>
            <td>$F</td>
            <td>等同于Form.Element.getValue()</td>
            <td>$F(''text1'')</td>
        </tr>
        <tr>
            <td>Effect.Highlight</td>
            <td>高亮特效.</td>
            <td>Effect.Highlight(''text1'')</td>
        </tr>
        <tr>
            <td>Effect.Fade</td>
            <td>褪色特效</td>
            <td></td>
        </tr>
        <tr>
            <td>Effect.Scale</td>
            <td>放大缩小(百分比）</td>
            <td>
            <p>Effect.Scale(''text1'', 200)<br />
            这里200 = 200%, 即两倍</p>
            </td>
        </tr>
        <tr>
            <td>Effect.Squish</td>
            <td>消失特效．文字缩小后消失</td>
            <td>Effect.Squish(''text1'')</td>
        </tr>
        <tr>
            <td>Effect.Puff</td>
            <td>消失特效．文字放大后消失</td>
            <td>Effect.Puff(''text1'')</td>
        </tr>
        <tr>
            <td>Effect.Appear</td>
            <td>出现特效</td>
            <td></td>
        </tr>
        <tr>
            <td>Effect.ContentZoom</td>
            <td>ZOOM特效．</td>
            <td></td>
        </tr>
        <tr>
            <td>Ajax.Request</td>
            <td>传送Ajax请求给服务器</td>
            <td>Ajax.Request(''http://server/s.php'')</td>
        </tr>
        <tr>
            <td>Ajax.Updater</td>
            <td>传送Ajax请求给服务器并用答复的结果更新指定的Container</td>
            <td>Ajax.Updater(''text1'',''http://server/s.php'')</td>
        </tr>
    </tbody>
</table>
</p>
<p>&nbsp;</p>
<p>Ajax的函数实际上还有一个可选参数,就是options.在未指明的情况下,Ajax使用的是''POST''发送请求,而且是异步执行,如果想要改用''GET''和同步,就可以用Ajax.Request(''http://server/s.php'',''get'','''',''a=1&amp;b=2'')来执行.</p>
<p>在Rails中Ajax的函数被封装成Ruby的函数,所以不必直接采用Ajax.Request,Ajax.Updater.但是知道它是怎么工作的也很有用.</p>
</div>
 <img src ="http://www.blogjava.net/FOG/aggbug/304182.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/FOG/" target="_blank">FOG</a> 2009-11-30 00:14 <a href="http://www.blogjava.net/FOG/archive/2009/11/30/304182.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jQuery Form插件详解</title><link>http://www.blogjava.net/FOG/archive/2009/11/26/303787.html</link><dc:creator>FOG</dc:creator><author>FOG</author><pubDate>Thu, 26 Nov 2009 08:50:00 GMT</pubDate><guid>http://www.blogjava.net/FOG/archive/2009/11/26/303787.html</guid><wfw:comment>http://www.blogjava.net/FOG/comments/303787.html</wfw:comment><comments>http://www.blogjava.net/FOG/archive/2009/11/26/303787.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/FOG/comments/commentRss/303787.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/FOG/services/trackbacks/303787.html</trackback:ping><description><![CDATA[<h5 class="posthead">&nbsp;</h5>
<div class="postText">
<p>&nbsp;</p>
<p>&lt;script src="js/jquery.form.js" type="text/javascript"&gt;&lt;/script&gt;</p>
<p>Jquery Form Plugin是jquery最重要的插件之一，它利用AJAX技术在不引起页面刷新的情况下POST表单。主要有两个方法：ajaxform和 ajaxsubmit。它会自动收集表单元素内容，决定如何管理提交进程。这两个方法支持多重选择。我想AJAX方式的表单应用再没有比这个插件更简单易用的了。</p>
<p>先在页面里设置一个普通的表单：<br />
Html代码 复制代码</p>
<p>&nbsp;&nbsp; 1. &lt;form id=&#8221;myForm&#8221; action=&#8221;comment.php&#8221; method=&#8221;post&#8221;&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 姓名: &lt;input type=&#8221;text&#8221; name=&#8221;name&#8221; id=&#8221;name&#8221;/&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 评论: &lt;textarea name=&#8221;comment&#8221; id=&#8221;comment&#8221;&gt;&lt;/textarea&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type=&#8221;submit&#8221; value=&#8221;提交评论&#8221; /&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 5. &lt;/form&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 6. [/html]&nbsp;&nbsp; </p>
<p>&lt;form id=&#8221;myForm&#8221; action=&#8221;comment.php&#8221; method=&#8221;post&#8221;&gt; <br />
&nbsp;&nbsp;&nbsp; 姓名: &lt;input type=&#8221;text&#8221; name=&#8221;name&#8221; id=&#8221;name&#8221;/&gt; <br />
&nbsp;&nbsp;&nbsp; 评论: &lt;textarea name=&#8221;comment&#8221; id=&#8221;comment&#8221;&gt;&lt;/textarea&gt; <br />
&nbsp;&nbsp;&nbsp; &lt;input type=&#8221;submit&#8221; value=&#8221;提交评论&#8221; /&gt; <br />
&lt;/form&gt; <br />
[/html]</p>
<p><br />
加载jquery库和插件脚本库。然后简单地写一段初始化脚本，这段脚本将在DOM加载完成时执行：<br />
Html代码 复制代码</p>
<p>&nbsp;&nbsp; 1. &lt;head&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;script type=&#8221;text/javascript&#8221; src=&#8221;jquery.js&#8221;&gt;&lt;/script&gt;&nbsp;&nbsp; //加载jquery&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;script type=&#8221;text/javascript&#8221; src=&#8221;form.js&#8221;&gt;&lt;/script&gt;&nbsp;&nbsp;&nbsp;&nbsp; //加载插件&nbsp;&nbsp; <br />
&nbsp;&nbsp; 4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;script type=&#8221;text/javascript&#8221;&gt;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 5.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // DOM加载完毕后执行&nbsp;&nbsp; <br />
&nbsp;&nbsp; 6.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(document).ready(function() {&nbsp;&nbsp; <br />
&nbsp;&nbsp; 7.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 绑定&#8217;myForm&#8217;并定义一个简单的回调函数&nbsp;&nbsp; <br />
&nbsp;&nbsp; 8.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $('#myForm').ajaxForm(function() {&nbsp;&nbsp; <br />
&nbsp;&nbsp; 9.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&#8221;评论提交完成!&#8221;);&nbsp;&nbsp; <br />
&nbsp; 10.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });&nbsp;&nbsp; <br />
&nbsp; 11.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });&nbsp;&nbsp; <br />
&nbsp; 12.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/script&gt;&nbsp;&nbsp; <br />
&nbsp; 13. &lt;/head&gt;&nbsp;&nbsp; <br />
&nbsp; 14. [/html]&nbsp; </p>
<p>&lt;head&gt; <br />
&nbsp;&nbsp;&nbsp; &lt;script type=&#8221;text/javascript&#8221; src=&#8221;jquery.js&#8221;&gt;&lt;/script&gt;&nbsp; //加载jquery <br />
&nbsp;&nbsp;&nbsp; &lt;script type=&#8221;text/javascript&#8221; src=&#8221;form.js&#8221;&gt;&lt;/script&gt;&nbsp;&nbsp;&nbsp; //加载插件 <br />
&nbsp;&nbsp;&nbsp; &lt;script type=&#8221;text/javascript&#8221;&gt; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // DOM加载完毕后执行 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(document).ready(function() { <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 绑定&#8217;myForm&#8217;并定义一个简单的回调函数 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $('#myForm').ajaxForm(function() { <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&#8221;评论提交完成!&#8221;); <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); <br />
&nbsp;&nbsp;&nbsp; &lt;/script&gt; <br />
&lt;/head&gt; <br />
[/html]</p>
<p>OK，就是这么简单。当点击&#8220;提交评论&#8221;按钮后，表单的数据就会POST到comment.php脚本，并且返回&#8220;评论提交完成&#8221;信息（如果提交成功）。页面并没有刷新，非常AJAX&#8230;&#8230;</p>
<p>让我们来看看这个插件还有什么强大的功能。</p>
<p>ajaxForm</p>
<p>1. 该方法须预先绑定表单，所以它一般在$(document).ready(function() { &#8230;}里定义。它能让表单在不刷新页面的情况下POST到目标。可以为该方法提供一个参数，参数一般是一个回调函数用于返回信息给用户。当然如果不提供参数也行，只是表单提在无声无息地提交后无法得到确认了。</p>
<p>2. 增加所有需要的事件监听器，为AJAX提交表单做好准备。ajaxForm不能提交表单。在document的ready函数中，使用ajaxForm来为 AJAX提交表单进行准备。ajaxForm接受0个或1个参数。这个单个的参数既可以是一个回调函数，也可以是一个Options对象。<br />
可链接（Chainable）：可以。</p>
<p>实例：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. $('#myFormId').ajaxForm();&nbsp; </p>
<p>$('#myFormId').ajaxForm();</p>
<p>ajaxSubmit</p>
<p>1. 该方法是以响应事件来通用AJAX方式提交表单。比如点击某个按钮或改变了某个下拉框的值，在触发事件函数里可以设置该方法。如：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. $(&#8221;#clickme&#8221;).click(function(){&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2.&nbsp;&nbsp;&nbsp; $(&#8221;#myForm&#8221;).ajaxSubmit();&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3. });&nbsp; </p>
<p>$(&#8221;#clickme&#8221;).click(function(){<br />
&nbsp; $(&#8221;#myForm&#8221;).ajaxSubmit();<br />
});</p>
<p>或者这样：<br />
Html代码 复制代码</p>
<p>&nbsp;&nbsp; 1. &lt;select id=&#8221;mySelect&#8221; onchange=&#8221;$(&#8216;#myForm&#8217;).ajaxSubmit();&#8221;&gt;&nbsp; </p>
<p>&lt;select id=&#8221;mySelect&#8221; onchange=&#8221;$(&#8216;#myForm&#8217;).ajaxSubmit();&#8221;&gt;</p>
<p>2. 马上由AJAX来提交表单。大多数情况下，都是调用ajaxSubmit来对用户提交表单进行响应。ajaxSubmit接受0个或1个参数。这个单个的参数既可以是一个回调函数，也可以是一个Options对象。<br />
可链接（Chainable）：可以。</p>
<p>实例：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. // 绑定表单提交事件处理器&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2. $('#myFormId').submit(function() {&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3.&nbsp;&nbsp;&nbsp;&nbsp; // 提交表单&nbsp;&nbsp; <br />
&nbsp;&nbsp; 4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(this).ajaxSubmit();&nbsp;&nbsp; <br />
&nbsp;&nbsp; 5.&nbsp;&nbsp;&nbsp;&nbsp; // 为了防止普通浏览器进行表单提交和产生页面导航（防止页面刷新？）返回false&nbsp;&nbsp; <br />
&nbsp;&nbsp; 6.&nbsp;&nbsp;&nbsp;&nbsp; return false;&nbsp;&nbsp; <br />
&nbsp;&nbsp; 7.&nbsp;&nbsp;&nbsp;&nbsp; });&nbsp; </p>
<p>// 绑定表单提交事件处理器<br />
$('#myFormId').submit(function() {<br />
&nbsp;&nbsp;&nbsp; // 提交表单<br />
&nbsp;&nbsp;&nbsp; $(this).ajaxSubmit();<br />
&nbsp;&nbsp;&nbsp; // 为了防止普通浏览器进行表单提交和产生页面导航（防止页面刷新？）返回false<br />
&nbsp;&nbsp;&nbsp; return false;<br />
&nbsp;&nbsp; });</p>
<p>formSerialize</p>
<p>1. 该方法可以表单域的名称和值连接起来，形成一个字符串，字符串格式为name1=value1&amp;name2=value2&#8230;比如：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. var queryString=$(&#8221;#myForm&#8221;).formSerialize();&nbsp; </p>
<p>var queryString=$(&#8221;#myForm&#8221;).formSerialize();</p>
<p>变量queryString的值将为变成name=xxx&amp;comment=xxx。得到这个字符串后，可以用$.post来提交，</p>
<p>如：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. $.post(&#8221;comment.php&#8221;,queryString);&nbsp; </p>
<p>$.post(&#8221;comment.php&#8221;,queryString);</p>
<p>2.将表单串行化（或序列化）成一个查询字符串。这个方法将返回以下格式的字符串：name1=value1&amp;name2=value2。<br />
可链接（Chainable）：不能， 这个方法返回一个字符串。</p>
<p>实例：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. var queryString = $('#myFormId').formSerialize();&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2.&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3. // 现在可以使用$.get、$.post、$.ajax等来提交数据&nbsp;&nbsp; <br />
&nbsp;&nbsp; 4. $.post('myscript.php', queryString);&nbsp; </p>
<p>var queryString = $('#myFormId').formSerialize();</p>
<p>// 现在可以使用$.get、$.post、$.ajax等来提交数据<br />
$.post('myscript.php', queryString);</p>
<p>fieldSerialize</p>
<p>1. 和formSerialize一样，返回一个字符串，但返回的是表单指定元素或特定元素类型域的字符串。返回字符串格式也是一样的。如：var queryString = $(&#8217;#myForm :text&#8217;).fieldSerialize();//返回myForm表单内所有文本框的字符串值。</p>
<p>2.将表单的字段元素串行化（或序列化）成一个查询字符串。当只有部分表单字段需要进行串行化（或序列化）时，这个就方便了。这个方法将返回以下格式的字符串：name1=value1&amp;name2=value2。<br />
可链接（Chainable）：不能，这个方法返回一个字符串。</p>
<p>实例：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. var queryString = $('#myFormId .specialFields').fieldSerialize();&nbsp;&nbsp; </p>
<p>var queryString = $('#myFormId .specialFields').fieldSerialize(); </p>
<p>fieldValue</p>
<p>1.返回一个数组，记录表单元素的值。如果表单没有值则对应数组值为空。如：</p>
<p>Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. var data=$(&#8217;:text&#8217;).fieldValue();//返回所有表单中所有文本框类型的值。&nbsp; </p>
<p>var data=$(&#8217;:text&#8217;).fieldValue();//返回所有表单中所有文本框类型的值。</p>
<p>变量data为[&#8221;,&#8221;,&#8221;]这样的一个数组，数组元素对应表单元素的值。事实上我们可以这样来得到&#8220;姓名&#8221;文本框里的值：var data=$(&#8221;#name&#8221;).val();（这个元素要有ID属性）这样的作法不需要jquery form插件也可以得到值。但使用插件可以更方便地取得单个或多个表单中特定元素域的值。比如取得所有文本框或所有复选框的值。只要加个&#8220;:&#8221;就可以。这种方法也可以用到formSerialize或fieldSerialize方法上。<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. var data=$(&#8221;#myForm :text&#8221;).fieldValue();//取得在myForm表单里所有文本框的值。&nbsp; </p>
<p>var data=$(&#8221;#myForm :text&#8221;).fieldValue();//取得在myForm表单里所有文本框的值。</p>
<p>2.返回匹配插入数组中的表单元素值。从0.91版起，该方法将总是以数组的形式返回数据。如果元素值被判定可能无效，则数组为空，否则它将包含一个或多于一个的元素值。<br />
可链接（Chainable）：不能，该方法返回数组。</p>
<p>实例：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. // 取得密码输入值&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2. var value = $('#myFormId :password').fieldValue();&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3. alert('The password is: ' + value[0]);&nbsp;&nbsp; </p>
<p>// 取得密码输入值<br />
var value = $('#myFormId :password').fieldValue(); <br />
alert('The password is: ' + value[0]);</p>
<p>formToArray</p>
<p>同样也返回一个数组，这个数组是对象数组。对象有name和value两个属性，分别记录表单中的元素名称和元素值。如：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. var data=$(&#8221;#myForm&#8221;).formToArray();&nbsp; </p>
<p>var data=$(&#8221;#myForm&#8221;).formToArray();</p>
<p>变量data得到的是一个对象数组。data[0].name保存myForm表单中第一个表单元素的名称属性值，这里是&#8221;name&#8221;。data[0].value保存myForm表单中第一个表单元素的值，这里就是填了什么值就是什么。<br />
resetForm</p>
<p>1.该方法很简单啦，恢复表单初始状态。也就恢复到DOM加载完成时的表单状态。类似&#8220;重设表单&#8221;</p>
<p>2.清除表单元素。该方法将所有的文本（text）输入字段、密码（password）输入字段和文本区域（textarea）字段置空，清除任何select元素中的选定，以及将所有的单选（radio）按钮和多选（checkbox）按钮重置为非选定状态。<br />
可链接（Chainable）：可以。<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. $('#myFormId').clearForm();&nbsp; </p>
<p>$('#myFormId').clearForm();</p>
<p>clearFields</p>
<p>1.清除表单域元素。可以清除特定类型的域元素，比如清除所有文本框的，或所有复选框的域。</p>
<p>2.清除字段元素。只有部分表单元素需要清除时才方便使用。<br />
可链接（Chainable）：可以。<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. $('#myFormId .specialFields').clearFields();&nbsp;&nbsp; </p>
<p>$('#myFormId .specialFields').clearFields(); </p>
<p>Options对象</p>
<p>ajaxForm和ajaxSubmit都支持众多的选项参数，这些选项参数可以使用一个Options对象来提供。Options只是一个JavaScript对象，它包含了如下一些属性与值的集合：</p>
<p>target</p>
<p>指明页面中由服务器响应进行更新的元素。元素的值可能被指定为一个jQuery选择器字符串，一个jQuery对象，或者一个DOM元素。<br />
默认值：null。</p>
<p>url</p>
<p>指定提交表单数据的URL。<br />
默认值：表单的action属性值</p>
<p>type</p>
<p>指定提交表单数据的方法（method）：&#8220;GET&#8221;或&#8220;POST&#8221;。<br />
默认值：表单的method属性值（如果没有找到默认为&#8220;GET&#8221;）。</p>
<p>beforeSubmit</p>
<p>表 单提交前被调用的回调函数。&#8220;beforeSubmit&#8221;回调函数作为一个钩子（hook），被提供来运行预提交逻辑或者校验表单数据。如果 &#8220;beforeSubmit&#8221;回调函数返回false，那么表单将不被提交。&#8220;beforeSubmit&#8221;回调函数带三个调用参数：数组形式的表单数据， jQuery表单对象，以及传入ajaxForm/ajaxSubmit中的Options对象。表单数组接受以下方式的数据：</p>
<p>[ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]</p>
<p><br />
默认值：null</p>
<p>success</p>
<p>表单成功提交后调用的回调函数。如果提供&#8220;success&#8221;回调函数，当从服务器返回响应后它被调用。然后由dataType选项值决定传回responseText还是responseXML的值。<br />
默认值：null</p>
<p>dataType</p>
<p>期望返回的数据类型。null、&#8220;xml&#8221;、&#8220;script&#8221;或者&#8220;json&#8221;其中之一。dataType提供一种方法，它规定了怎样处理服务器的响应。这个被直接地反映到jQuery.httpData方法中去。下面的值被支持：</p>
<p>'xml'：如果dataType == 'xml'，将把服务器响应作为XML来对待。同时，如果&#8220;success&#8221;回调方法被指定， 将传回responseXML值。</p>
<p>'json'：如果dataType == 'json'， 服务器响应将被求值，并传递到&#8220;success&#8221;回调方法，如果它被指定的话。</p>
<p>'script'：如果dataType == 'script'， 服务器响应将求值成纯文本。</p>
<p><br />
默认值：null（服务器返回responseText值）</p>
<p>semantic</p>
<p>Boolean flag indicating whether data must be submitted in strict semantic order (slower). Note that the normal form serialization is done in semantic order with the exception of input elements of type="image". You should only set the semantic option to true if your server has strict semantic requirements and your form contains an input element of type="image".<br />
布 尔标志，表示数据是否必须严格按照语义顺序（slower？）来进行提交。注意：一般来说，表单已经按照语义顺序来进行了串行化（或序列化），除了 type="image"的input元素。如果你的服务器有严格的语义要求，以及表单中包含有一个type="image"的input元素，就应该将 semantic设置为true。（译注：这一段由于无法理解，翻译出来可能语不达意，但请达人指正。）<br />
默认值：false</p>
<p>resetForm</p>
<p>布尔标志，表示如果表单提交成功是否进行重置。<br />
Default value: null</p>
<p>clearForm</p>
<p>布尔标志，表示如果表单提交成功是否清除表单数据。<br />
默认值：null</p>
<p>实例：<br />
Js代码 复制代码</p>
<p>&nbsp;&nbsp; 1. // 准备好Options对象&nbsp;&nbsp; <br />
&nbsp;&nbsp; 2. var options = {&nbsp;&nbsp; <br />
&nbsp;&nbsp; 3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; target:&nbsp;&nbsp;&nbsp;&nbsp; '#divToUpdate',&nbsp;&nbsp; <br />
&nbsp;&nbsp; 4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'comment.php',&nbsp;&nbsp; <br />
&nbsp;&nbsp; 5.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; success: function() {&nbsp;&nbsp; <br />
&nbsp;&nbsp; 6.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert('Thanks for your comment!');&nbsp;&nbsp; <br />
&nbsp;&nbsp; 7.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } };&nbsp;&nbsp; <br />
&nbsp;&nbsp; 8.&nbsp;&nbsp; <br />
&nbsp;&nbsp; 9.&nbsp;&nbsp;&nbsp; // 将options传给ajaxForm&nbsp;&nbsp; <br />
&nbsp; 10. $('#myForm').ajaxForm(options);&nbsp; </p>
<p>// 准备好Options对象<br />
var options = {<br />
&nbsp;&nbsp;&nbsp; target:&nbsp;&nbsp;&nbsp;&nbsp; '#divToUpdate',<br />
&nbsp;&nbsp;&nbsp; url:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'comment.php',<br />
&nbsp;&nbsp;&nbsp; success: function() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert('Thanks for your comment!');<br />
&nbsp;&nbsp;&nbsp; } };</p>
<p>&nbsp;&nbsp; // 将options传给ajaxForm<br />
$('#myForm').ajaxForm(options);</p>
<p>注意：Options对象还可以用来将值传递给jQuery的$.ajax方法。如果你熟悉$.ajax所支持的options，你可以利用它们来将Options对象传递给ajaxForm和ajaxSubmit。</p>
</div>
<img src ="http://www.blogjava.net/FOG/aggbug/303787.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/FOG/" target="_blank">FOG</a> 2009-11-26 16:50 <a href="http://www.blogjava.net/FOG/archive/2009/11/26/303787.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQLSERVER DLL的基本操作和实例</title><link>http://www.blogjava.net/FOG/archive/2009/11/24/303501.html</link><dc:creator>FOG</dc:creator><author>FOG</author><pubDate>Tue, 24 Nov 2009 09:54:00 GMT</pubDate><guid>http://www.blogjava.net/FOG/archive/2009/11/24/303501.html</guid><wfw:comment>http://www.blogjava.net/FOG/comments/303501.html</wfw:comment><comments>http://www.blogjava.net/FOG/archive/2009/11/24/303501.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/FOG/comments/commentRss/303501.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/FOG/services/trackbacks/303501.html</trackback:ping><description><![CDATA[<p><br />
<span style="color: #0000ff">/**<br />
&nbsp; *删除数据库中已经存在的表<br />
&nbsp;**/<br />
if exists(select * from sys.objects where name='tb_grade')<br />
drop table tb_grade--删除tb_grade表<br />
go<br />
if exists(select * from sys.objects where name='tb_student')<br />
drop table tb_student--删除tb_grade表<br />
go</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp; *创建数据表<br />
&nbsp;**/<br />
create table tb_student--创建tb_student<br />
(<br />
&nbsp;&nbsp; student_id int identity(1,1),--学生编号(主键,自动增长)<br />
&nbsp;&nbsp; student_name nvarchar(30) not null,--学生姓名(不能为空)&nbsp;&nbsp; <br />
&nbsp;&nbsp; student_sex char(10) not null,--学生性别(不能为空)<br />
&nbsp;&nbsp; student_age int default(18),--学生年龄(默认为18)<br />
&nbsp;&nbsp; constraint pk_student_id primary key (student_id)<br />
)<br />
go<br />
create table tb_grade--创建tb_grade<br />
(<br />
&nbsp; grade_id int identity(1,1),--成绩编号(主键，自动增长1)<br />
&nbsp; student_id int,--外键(引用学生表student_id)<br />
&nbsp; english float,--英语成绩<br />
&nbsp; math float,--数学成绩<br />
&nbsp; constraint pk_grade_id primary key (grade_id),--为表tb_student创建主键<br />
&nbsp; constraint fk_student_id foreign key(student_id) references tb_student(student_id)--创建外键关系<br />
)<br />
&nbsp;<br />
select * from tb_student<br />
select * from&nbsp; tb_grade</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp; *创建视图<br />
&nbsp;**/</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp;*创建视图的语法<br />
&nbsp;*<br />
&nbsp;<span style="color: #800000">CREATE VIEW&nbsp; view_name<br />
&nbsp;AS<br />
&nbsp;select_statement<br />
</span>*<br />
*<br />
*/</span></p>
<p><span style="color: #0000ff">if exists (select * from sys.objects where name='vw_student')<br />
drop view vw_student --删除已经存在的vw_student视图<br />
go<br />
create view vw_student--创建视图vw_student<br />
as<br />
select * from tb_student inner join tb_grade on tb_student.student_id=tb_grade.student_id<br />
go</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp; *创建索引<br />
&nbsp;**/</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp;*创建索引的语法<br />
&nbsp;*<br />
<span style="color: #800000">CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name<br />
&nbsp;&nbsp;&nbsp; ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) <br />
[ WITH &lt; index_option &gt; [ ,...n] ] <br />
[ ON filegroup ]</span></span></p>
<p><span style="color: #0000ff"><span style="color: #800000">&lt; index_option &gt; ::= <br />
&nbsp;&nbsp;&nbsp; { PAD_INDEX |<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILLFACTOR = fillfactor |<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IGNORE_DUP_KEY |<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DROP_EXISTING |<br />
&nbsp;&nbsp;&nbsp; STATISTICS_NORECOMPUTE |<br />
&nbsp;&nbsp;&nbsp; SORT_IN_TEMPDB&nbsp; <br />
}<br />
</span>*<br />
*<br />
*/</span></p>
<p><span style="color: #0000ff">if exists (select * from sys.indexes where name='index_student_id')<br />
drop index index_student_id on tb_student --删除tb_student中的索引student_id<br />
--创建索引<br />
create index index_student_id on tb_student(student_id)<br />
go</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp; *创建存储过程<br />
&nbsp;**/</span></p>
<p><span style="color: #0000ff">/**<br />
&nbsp;*<br />
&nbsp;*创建存储过程的语法<br />
&nbsp;*<br />
<span style="color: #800000">CREATE PROC [ EDURE ] procedure_name [ ; number ]<br />
&nbsp;&nbsp;&nbsp; [ { @parameter data_type }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ VARYING ] [ = default ] [ OUTPUT ]<br />
&nbsp;&nbsp;&nbsp; ] [ ,...n ] </span></span></p>
<p><span style="color: #0000ff"><span style="color: #800000">[ WITH<br />
&nbsp;&nbsp;&nbsp; { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ] </span></span></p>
<p><span style="color: #0000ff"><span style="color: #800000">[ FOR REPLICATION ] </span></span></p>
<p><span style="color: #0000ff"><span style="color: #800000">AS sql_statement [ ...n ] <br />
</span>*<br />
*/<br />
--不带参数的存储过程<br />
if exists (select * from&nbsp; sys.objects where name='find_student')<br />
drop procedure find_student<br />
go<br />
create procedure find_student<br />
as<br />
select * from tb_student<br />
go<br />
---调用存储过程<br />
exec find_student<br />
go<br />
--带输入参数的存储过程<br />
if exists (select * from&nbsp; sys.objects where name='add_student_grade')<br />
drop procedure add_student_grade<br />
go<br />
create procedure add_student_grade<br />
--定义输入参数<br />
@studentName nvarchar(30),<br />
@studentSex char(10),<br />
@studentAge int=18,<br />
@gradeEnglish float,<br />
@gradeMath float <br />
as<br />
&nbsp;&nbsp;&nbsp; declare @studentId int<br />
&nbsp;&nbsp;&nbsp; declare @n int<br />
&nbsp;&nbsp;&nbsp; set @n=0<br />
&nbsp;&nbsp;&nbsp; begin transaction tran_add --开启事务<br />
&nbsp;&nbsp;&nbsp; insert into tb_student(student_name,student_sex,student_age) values(@studentName,@studentSex,@studentAge)<br />
&nbsp;&nbsp;&nbsp; set @n=@@error&nbsp;&nbsp;&nbsp; <br />
&nbsp;select @studentId=max(student_id) from tb_student<br />
&nbsp;set @n=@@error&nbsp; <br />
&nbsp;&nbsp;&nbsp; insert into tb_grade (student_id,english,math) values(@studentId,@gradeEnglish,@gradeMath)<br />
&nbsp;set @n=@@error&nbsp; <br />
&nbsp;&nbsp;&nbsp; if(@n&lt;&gt;0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rollback transaction tran_add --回滚事务<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br />
&nbsp;&nbsp;&nbsp; else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin <br />
&nbsp;&nbsp;&nbsp;&nbsp; commit transaction tran_add&nbsp; --提交事务<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br />
go<br />
---调用存储过程<br />
exec add_student_grade '张三','男',20,80,88<br />
go<br />
--带输出参数的存储过程<br />
if exists (select * from&nbsp; sys.objects where name='getCount')<br />
drop procedure getCount<br />
go<br />
create procedure getCount<br />
@n int output <br />
as<br />
select @n=count(*) from tb_student<br />
go<br />
--调用带输出参数的存储过程<br />
declare @n int<br />
execute getCount @n output<br />
select @n<br />
&nbsp;&nbsp; <br />
/**<br />
&nbsp;*创建触发器<br />
&nbsp;**/<br />
/**<br />
&nbsp;*<br />
&nbsp;*创建触发器的语法<br />
&nbsp;*<br />
&nbsp;*<br />
<span style="color: #800000">CREATE TRIGGER trigger_name <br />
ON { table | view } <br />
[ WITH ENCRYPTION ] <br />
{<br />
&nbsp;&nbsp;&nbsp; { { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ WITH APPEND ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ NOT FOR REPLICATION ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AS<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ { IF UPDATE ( column )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ { AND | OR } UPDATE ( column ) ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ ...n ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | IF ( COLUMNS_UPDATED ( ) { bitwise_operator } updated_bitmask )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { comparison_operator } column_bitmask [ ...n ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } ] <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql_statement [ ...n ] <br />
&nbsp;&nbsp;&nbsp; } <br />
} <br />
</span>*<br />
*<br />
**/</span></p>
<p><span style="color: #0000ff">IF EXISTS (SELECT name FROM sysobjects<br />
WHERE name = 'reminder' AND type = 'TR')<br />
DROP TRIGGER reminder --删除触发器reminder<br />
GO<br />
--创建触发器reminder(如果对表tb_student进行添加和更新信息时出发)<br />
CREATE TRIGGER reminder<br />
ON tb_student<br />
FOR INSERT, UPDATE<br />
AS <br />
RAISERROR (50009, 16, 10)<br />
GO<br />
---创建DELETE触发器<br />
IF EXISTS (SELECT name FROM sysobjects<br />
WHERE name = 'sendemail' AND type = 'TR')<br />
DROP TRIGGER sendemail--删除触发器sendemail<br />
GO<br />
--创建触发器<br />
CREATE TRIGGER sendemail<br />
ON tb_grade<br />
FOR DELETE<br />
AS<br />
EXEC master..xp_sendmail'&nbsp;MaBin',<br />
'Don''t forget to print a report for the distributors.'<br />
GO</span></p>
 <img src ="http://www.blogjava.net/FOG/aggbug/303501.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/FOG/" target="_blank">FOG</a> 2009-11-24 17:54 <a href="http://www.blogjava.net/FOG/archive/2009/11/24/303501.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>