﻿<?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-alancxx</title><link>http://www.blogjava.net/alancxx/</link><description>------简单就是美</description><language>zh-cn</language><lastBuildDate>Tue, 28 Apr 2026 21:15:55 GMT</lastBuildDate><pubDate>Tue, 28 Apr 2026 21:15:55 GMT</pubDate><ttl>60</ttl><item><title>oracle jdbc 邪恶数字"65536" ---批量插入10万条记录引发的“血案”</title><link>http://www.blogjava.net/alancxx/articles/369106.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Tue, 31 Jan 2012 12:17:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/369106.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/369106.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/369106.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/369106.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/369106.html</trackback:ping><description><![CDATA[<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="font-weight: bold"><span>1.<span>&nbsp;&nbsp;&nbsp;</span></span></strong><strong style="font-weight: bold">故事背景：</strong></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">mysql数据迁移到<span>oracle，其中有个表名叫&#8220;bil_vip&#8221;，有10万条记录，迁移后检查发现oracle数据库只插入34464条记录，程序执行过程没有发现任何错误。修改数据库连接代码向mysql插入10万条记录，结果都插入OK。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">批量插入使用<span>spring&nbsp;<strong style="font-weight: bold">jdbctemplate.batchUpdate</strong>(sql, new BatchPreparedStatementSetter());方法，该方法的核心是PreparedStatement的<strong style="font-weight: bold">executeBatch</strong>方法。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="font-weight: bold"><span>2.<span>&nbsp;&nbsp;&nbsp;</span></span></strong><strong style="font-weight: bold">继续实验：</strong></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">新建一个<span>Test表，只有一个name字段做实验。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">抛弃<span>jdbctemplate，直接使用PreparedStatement做实验，结果和jdbcTemplate是一样的，实验证明总是会丢失65536条记录。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">oracle&nbsp;驱动的问题？换了最新<span>driver，还是不行。继续探索....</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="font-weight: bold"><span>3.<span>&nbsp;&nbsp;&nbsp;</span></span></strong><strong style="font-weight: bold">希望之光：</strong></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">早上发了个消息向大家征求思路，继中说了一句分批处理，点亮了我的思路。按照继中提供思路，每<span>1万条记录一批，分多批发送给Oracle。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">结果喜出望外，<span>10万条记录全部插入成功。但是"65536"这个数字是什么意思呢......<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">在网上搜索文章发现，这个<span>65536是一个bug。当PreparedStatement批量处理正好65536个记录时候，<span style="color: red">程序会挂死</span>。我试了一下真的挂死了，太吓人了，这坑够深的。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="font-weight: bold"><span>4.<span>&nbsp;&nbsp;&nbsp;</span></span></strong><strong style="font-weight: bold">解决方案：</strong></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">spring jdbctemplate还是很好用的，而且业务已经被我封装了，如果使用<span>PreparedStatement意味着多了一个处理分支，以后维护会很麻烦。</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">我新建一个<span>MyJdbcTemplate类，继承&nbsp;jdbctemplate类，并覆盖了batchUpdate方法。这下舒服了，系统又恢复了整洁。<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="font-weight: bold"><span>5.<span>&nbsp;&nbsp;&nbsp;</span></span></strong><strong style="font-weight: bold">&#9733;&nbsp;</strong><strong style="font-weight: bold">结论和收获：</strong></p>
<p style="padding-right: 0px; padding-left: 30px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-family: Wingdings"><span style="font-size: small">&#252;&nbsp;&nbsp;</span></span><span style="font-size: small">有事多思考，多请教身边同事。</span></p>
<p style="padding-right: 0px; padding-left: 30px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-family: Wingdings"><span style="font-size: small">&#252;&nbsp;&nbsp;</span></span><span style="font-size: small">坚持<span>Open-close原则<span style="color: red">(</span></span>Open for extension, Closed for modification)会系统更好的扩展，非常容易维护，关键是要坚持这个原则，如果我因为一个特殊分支使用了<span>PreparedStatement，这样势必破坏了这个原则，日后的维护必然会很麻烦。</span></span></p>
<p style="padding-right: 0px; padding-left: 30px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-family: Wingdings"><span style="font-size: small">&#252;&nbsp;&nbsp;</span></span><span style="font-size: small">基于<span>oracle数据如果使用jdbc批量，一定要分批发送数据oracle，否则正好发一个65536系统就挂死，大于65536数据就丢失，杯具呀......</span></span></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 0px; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><strong style="font-weight: bold"><span>6.<span>&nbsp;&nbsp;&nbsp;</span></span></strong><strong style="font-weight: bold">题目何以为&#8220;<span style="color: red">血案&#8221;：</span></strong></p>
<p style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0cm 0cm 0pt 21pt; word-spacing: 0px; font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; text-transform: none; color: rgb(0,0,0); text-indent: 21pt; padding-top: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-align: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span style="font-size: small">周二打球回家就想这个问题，打开笔记本调试，不知不觉搞到很晚，影响媳妇睡觉（媳妇早上<span>5:30上班），被痛骂一顿，赶紧上床睡觉，我媳妇气的不行，手痛砸了一下床板（我们的床撤掉了床垫，只有床架和木板），床一下子塌了，把媳妇吓坏了，把我腿弄伤了一块，唉，&#8220;<strong style="font-weight: bold"><span style="color: red">血案</span></strong>&#8221;呀。</span></span></p><img src ="http://www.blogjava.net/alancxx/aggbug/369106.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2012-01-31 20:17 <a href="http://www.blogjava.net/alancxx/articles/369106.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>打jar把第三方的jar也打进去</title><link>http://www.blogjava.net/alancxx/articles/369033.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Mon, 30 Jan 2012 08:58:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/369033.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/369033.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/369033.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/369033.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/369033.html</trackback:ping><description><![CDATA[<span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;之前在我的blog中写道,关于这个问题,以前是不知道怎么实现?</span><a title="关于java跨平台移值" style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(29,88,209); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); text-decoration: none; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" href="http://www.blogjava.net/soddabao/archive/2006/10/20/76304.html" target="_blank">地址</a><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">.后来自己在网上查了,有各种不同的说法,感觉都不怎么实用.</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp; 前几天在看&lt;&lt;Eclipse从入门到精通&gt;&gt;一书,看到一节关于eclipse打包的介绍,感觉有方法了.</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp; 大家都知道打java包,需要一个清单文件,MANIFEST.MF,内容无非就是</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" />
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; padding-bottom: 4px; word-spacing: 0px; font: 13px/21px Arial; text-transform: none; border-left: rgb(204,204,204) 1px solid; width: 564px; color: rgb(0,0,0); word-break: break-all; text-indent: 0px; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; white-space: normal; letter-spacing: normal; background-color: rgb(238,238,238); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-image: initial" src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align="top"  alt="" /><span style="color: rgb(0,0,0)">Manifest</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">Version:&nbsp;</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)"><br /><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-image: initial" src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align="top"  alt="" />Main</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">Class:&nbsp;awt.MyGuiImp</span></div><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp; 实际上,清单文件中还有一个重要的,就是Class-Path,大家一看就知道这个是用来干什么的了.不错,这个就是程序运行的支持库的路径.后来上网查了一下,MANIFEST.MF的具体解释是这样:</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 打包清单.它是打包的关键性文件,主要是设置执行入口类和支持库的路径,在运行java应用程序时要根据此文件中给出的信息来查找入口类和支持库.</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我们以前只注意了MANIFEST.MF中的入口类,也就是我们常说的主类(有main方法的类),却忽视了支持库.</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Class-Path:指定支持库的路径,"."指程序运行目录,即到处的jar包所在目录.程序运行时依据Class-Path项的设置路径来查找支持库.每一个支持库之间用空格格开.</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" /><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp; 后来我自己用到的jar,放一个文件夹lib,lib就放在和jar包一个文件夹下,清单文件这样写</span><br style="word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" />
<div style="border-right: rgb(204,204,204) 1px solid; padding-right: 5px; border-top: rgb(204,204,204) 1px solid; padding-left: 4px; padding-bottom: 4px; word-spacing: 0px; font: 13px/21px Arial; text-transform: none; border-left: rgb(204,204,204) 1px solid; width: 564px; color: rgb(0,0,0); word-break: break-all; text-indent: 0px; padding-top: 4px; border-bottom: rgb(204,204,204) 1px solid; white-space: normal; letter-spacing: normal; background-color: rgb(238,238,238); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-image: initial" src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align="top"  alt="" /><span style="color: rgb(0,0,0)">Manifest</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">Version:&nbsp;</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)"><br /><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-image: initial" src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align="top"  alt="" />Main</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">Class:&nbsp;awt.MyGuiImp<br /><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-image: initial" src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align="top"  alt="" />Class</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">Path:&nbsp;.</span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)">lib</span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)">jacob.jar&nbsp;.</span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)">lib</span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)">jdom.jar&nbsp;.</span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)">lib</span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)">log4j.jar</span></div><span style="display: inline! important; float: none; word-spacing: 0px; font: 14px/21px Arial; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: rgb(255,255,255); orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp; 移到别的计算机,运行jar,就不会出现任何问题了,前提是该计算机必须装jdk.</span><img src ="http://www.blogjava.net/alancxx/aggbug/369033.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2012-01-30 16:58 <a href="http://www.blogjava.net/alancxx/articles/369033.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux 部署记录</title><link>http://www.blogjava.net/alancxx/archive/2011/10/26/362096.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Wed, 26 Oct 2011 06:23:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/archive/2011/10/26/362096.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/362096.html</wfw:comment><comments>http://www.blogjava.net/alancxx/archive/2011/10/26/362096.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/362096.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/362096.html</trackback:ping><description><![CDATA[<p><font face="Verdana"><font face="Verdana"><span style="color: #003366">&nbsp;&nbsp;&nbsp; 1&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 2&nbsp; cd Server/</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 3&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 4&nbsp; rpm -Uvh setarch-2*</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 5&nbsp; rpm -Uvh make-3*</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 6&nbsp; rpm -Uvh glibc-2*</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 7&nbsp; rpm -Uvh libaio-0*</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 8&nbsp; rpm -Uvh compat-libstdc++-33-3*</span><br /><span style="color: #003366">&nbsp;&nbsp;&nbsp; 9&nbsp; rpm -Uvh compat-gcc-34-3*</span><br /><span style="color: #003366">&nbsp;&nbsp; 10&nbsp; rpm -Uvh compat-gcc-34-c++-3*</span><br /><span style="color: #003366">&nbsp;&nbsp; 11&nbsp; rpm -Uvh gcc-4*</span><br /><span style="color: #003366">&nbsp;&nbsp; 12&nbsp; rpm -Uvh libXp-1*</span><br /><span style="color: #003366">&nbsp;&nbsp; 13&nbsp; rpm -Uvh openmotif-2*</span><br /><span style="color: #003366">&nbsp;&nbsp; 14&nbsp; rpm -Uvh compat-db-4*</span><br /><span style="color: #003366">&nbsp;&nbsp; 15&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp;&nbsp; 16&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 17&nbsp; mkdir u01</span><br /><span style="color: #003366">&nbsp;&nbsp; 18&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 19&nbsp; ln -s /opt/u01/ /u01</span><br /><span style="color: #003366">&nbsp;&nbsp; 20&nbsp; cd /</span><br /><span style="color: #003366">&nbsp;&nbsp; 21&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 22&nbsp; cd u-1</span><br /><span style="color: #003366">&nbsp;&nbsp; 23&nbsp; cd u01</span><br /><span style="color: #003366">&nbsp;&nbsp; 24&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 25&nbsp; mkdir -p /u01/app/oracle/product/10.2.0/db_1</span><br /><span style="color: #003366">&nbsp;&nbsp; 26&nbsp; chown -R oracle.oinstall /u01</span><br /><span style="color: #003366">&nbsp;&nbsp; 27&nbsp; chown -R oracle.oinstall /opt/u01</span><br /><span style="color: #003366">&nbsp;&nbsp; 28&nbsp; cd /etc</span><br /><span style="color: #003366">&nbsp;&nbsp; 29&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 30&nbsp; vi redhat-release </span><br /><span style="color: #003366">&nbsp;&nbsp; 31&nbsp; cd /home/oracle/.bash_profile </span><br /><span style="color: #003366">&nbsp;&nbsp; 32&nbsp; vi /home/oracle/.bash_profile </span><br /><span style="color: #003366">&nbsp;&nbsp; 33&nbsp; su - oracle</span><br /><span style="color: #003366">&nbsp;&nbsp; 34&nbsp; /u01/app/oracle/product/10.2.0/db/root.sh</span><br /><span style="color: #003366">&nbsp;&nbsp; 35&nbsp; ifconfig</span><br /><span style="color: #003366">&nbsp;&nbsp; 36&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp;&nbsp; 37&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 38&nbsp; mkdir u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 39&nbsp; ln -s /opt/u02 /u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 40&nbsp; cd /</span><br /><span style="color: #003366">&nbsp;&nbsp; 41&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 42&nbsp; chown -R oracle:oinstall /opt/u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 43&nbsp; chown -R oracle:oinstall /u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 44&nbsp; su - oracle</span><br /><span style="color: #003366">&nbsp;&nbsp; 45&nbsp; fdisk -l</span><br /><span style="color: #003366">&nbsp;&nbsp; 46&nbsp; fdisk /dev/sdb </span><br /><span style="color: #003366">&nbsp;&nbsp; 47&nbsp; fdisk -l</span><br /><span style="color: #003366">&nbsp;&nbsp; 48&nbsp; partprobe </span><br /><span style="color: #003366">&nbsp;&nbsp; 49&nbsp; mkfs.ext3 /dev/sdb1</span><br /><span style="color: #003366">&nbsp;&nbsp; 50&nbsp; mkfs.ext2 /dev/sdb1</span><br /><span style="color: #003366">&nbsp;&nbsp; 51&nbsp; mke2fs /dev/sdb1</span><br /><span style="color: #003366">&nbsp;&nbsp; 52&nbsp; mkfs -t ext3 -j /dev/sdb1</span><br /><span style="color: #003366">&nbsp;&nbsp; 53&nbsp; mkfs -t ext2 -j /dev/sdb1</span><br /><span style="color: #003366">&nbsp;&nbsp; 54&nbsp; fdisk /dev/sdb</span><br /><span style="color: #003366">&nbsp;&nbsp; 55&nbsp; fdisk /dev/sdb</span><br /><span style="color: #003366">&nbsp;&nbsp; 56&nbsp; fdisk -l</span><br /><span style="color: #003366">&nbsp;&nbsp; 57&nbsp; mkfs.ext3 /dev/sdb1</span><br /><span style="color: #003366">&nbsp;&nbsp; 58&nbsp; mkdir /opt/newdis</span><br /><span style="color: #003366">&nbsp;&nbsp; 59&nbsp; mount /dev/sdb1 /opt/newdis/</span><br /><span style="color: #003366">&nbsp;&nbsp; 60&nbsp; vi /etc/fstab </span><br /><span style="color: #003366">&nbsp;&nbsp; 61&nbsp; touch test /opt/newdis/</span><br /><span style="color: #003366">&nbsp;&nbsp; 62&nbsp; cd /opt/newdis/</span><br /><span style="color: #003366">&nbsp;&nbsp; 63&nbsp; ls</span><br /><span style="color: #003366">&nbsp;&nbsp; 64&nbsp; ls</span><br /><span style="color: #003366">&nbsp;&nbsp; 65&nbsp; touch test</span><br /><span style="color: #003366">&nbsp;&nbsp; 66&nbsp; ls</span><br /><span style="color: #003366">&nbsp;&nbsp; 67&nbsp; reboot</span><br /><span style="color: #003366">&nbsp;&nbsp; 68&nbsp; df -lh</span><br /><span style="color: #003366">&nbsp;&nbsp; 69&nbsp; cd /opt/newdis/</span><br /><span style="color: #003366">&nbsp;&nbsp; 70&nbsp; ls</span><br /><span style="color: #003366">&nbsp;&nbsp; 71&nbsp; su - oracle</span><br /><span style="color: #003366">&nbsp;&nbsp; 72&nbsp; rm -rf u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 73&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 74&nbsp; cd /</span><br /><span style="color: #003366">&nbsp;&nbsp; 75&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 76&nbsp; rm -rf u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 77&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 78&nbsp; ln -s /opt/newdis/u02 /u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 79&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 80&nbsp; chown -R oracle:oinstall /u02</span><br /><span style="color: #003366">&nbsp;&nbsp; 81&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 82&nbsp; su - oracle</span><br /><span style="color: #003366">&nbsp;&nbsp; 83&nbsp; chown -R oracle:oinstall /opt/newdis/</span><br /><span style="color: #003366">&nbsp;&nbsp; 84&nbsp; su - oracle</span><br /><span style="color: #003366">&nbsp;&nbsp; 85&nbsp; mount 192.168.6.138:/u02/dmp /u02/dmp/</span><br /><span style="color: #003366">&nbsp;&nbsp; 86&nbsp; cd /u02/dmp/</span><br /><span style="color: #003366">&nbsp;&nbsp; 87&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 88&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 89&nbsp; ll</span><br /><span style="color: #003366">&nbsp;&nbsp; 90&nbsp; lsnrctl status</span><br /><span style="color: #003366">&nbsp;&nbsp; 91&nbsp; su - oracle</span><br /><span style="color: #003366">&nbsp;&nbsp; 92&nbsp; service syslog status</span><br /><span style="color: #003366">&nbsp;&nbsp; 93&nbsp; netstat -tlnp |grep syslog</span><br /><span style="color: #003366">&nbsp;&nbsp; 94&nbsp; netstat -an |grep syslog</span><br /><span style="color: #003366">&nbsp;&nbsp; 95&nbsp; service syslog status</span><br /><span style="color: #003366">&nbsp;&nbsp; 96&nbsp; clean</span><br /><span style="color: #003366">&nbsp;&nbsp; 97&nbsp; clear</span><br /><span style="color: #003366">&nbsp;&nbsp; 98&nbsp; netstat -an |grep syslog</span><br /><span style="color: #003366">&nbsp;&nbsp; 99&nbsp; clear</span><br /><span style="color: #003366">&nbsp; 100&nbsp; service syslog status</span><br /><span style="color: #003366">&nbsp; 101&nbsp; service iptables status</span><br /><span style="color: #003366">&nbsp; 102&nbsp; last</span><br /><span style="color: #003366">&nbsp; 103&nbsp; who</span><br /><span style="color: #003366">&nbsp; 104&nbsp; service iptables status</span><br /><span style="color: #003366">&nbsp; 105&nbsp; service iptables start</span><br /><span style="color: #003366">&nbsp; 106&nbsp; service iptables status</span><br /><span style="color: #003366">&nbsp; 107&nbsp; cd /root/</span><br /><span style="color: #003366">&nbsp; 108&nbsp; mkdir .ssh</span><br /><span style="color: #003366">&nbsp; 109&nbsp; cd .ssh/</span><br /><span style="color: #003366">&nbsp; 110&nbsp; rz</span><br /><span style="color: #003366">&nbsp; 111&nbsp; cp /root/Identity ./id_rsa</span><br /><span style="color: #003366">&nbsp; 112&nbsp; ssh-keygen </span><br /><span style="color: #003366">&nbsp; 113&nbsp; sftp <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#97;&#108;&#97;&#110;&#64;&#49;&#57;&#50;&#46;&#49;&#54;&#56;&#46;&#53;&#48;&#46;&#52;&#50;">alan@192.168.50.42</a></span><br /><span style="color: #003366">&nbsp; 114&nbsp; cd /root/.ssh/</span><br /><span style="color: #003366">&nbsp; 115&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 116&nbsp; ssh-keygen </span><br /><span style="color: #003366">&nbsp; 117&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 118&nbsp; vi id_rsa.pub </span><br /><span style="color: #003366">&nbsp; 119&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 120&nbsp; rm -rf *</span><br /><span style="color: #003366">&nbsp; 121&nbsp; cp /root/Identity ./id_rsa</span><br /><span style="color: #003366">&nbsp; 122&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 123&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 124&nbsp; sftp <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#97;&#108;&#97;&#110;&#64;&#49;&#57;&#50;&#46;&#49;&#54;&#56;&#46;&#53;&#48;&#46;&#52;&#48;">alan@192.168.50.40</a></span><br /><span style="color: #003366">&nbsp; 125&nbsp; sftp alan@192.168.50.42</span><br /><span style="color: #003366">&nbsp; 126&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 127&nbsp; rm -rf known_hosts </span><br /><span style="color: #003366">&nbsp; 128&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 129&nbsp; sftp root@192.168.50.42</span><br /><span style="color: #003366">&nbsp; 130&nbsp; ssh-keygen </span><br /><span style="color: #003366">&nbsp; 131&nbsp; sftp root@192.168.50.42</span><br /><span style="color: #003366">&nbsp; 132&nbsp; ssh-keygen </span><br /><span style="color: #003366">&nbsp; 133&nbsp; sftp root@192.168.50.42</span><br /><span style="color: #003366">&nbsp; 134&nbsp; ssh-keygen </span><br /><span style="color: #003366">&nbsp; 135&nbsp; sftp root@192.168.50.42</span><br /><span style="color: #003366">&nbsp; 136&nbsp; ifconfig</span><br /><span style="color: #003366">&nbsp; 137&nbsp; init 0</span><br /><span style="color: #003366">&nbsp; 138&nbsp; ntpdate 192.168.191.240</span><br /><span style="color: #003366">&nbsp; 139&nbsp; echo "ntpdate 192.168.191.240" &gt;&gt; /etc/rc.local</span><br /><span style="color: #003366">&nbsp; 140&nbsp; echo "0 12 * * * ntpdate 192.168.191.240" &gt;&gt; /var/spool/cron/root</span><br /><span style="color: #003366">&nbsp; 141&nbsp; at 18:01 </span><br /><span style="color: #003366">&nbsp; 142&nbsp; last</span><br /><span style="color: #003366">&nbsp; 143&nbsp; last | less</span><br /><span style="color: #003366">&nbsp; 144&nbsp; shutdown -h now</span><br /><span style="color: #003366">&nbsp; 145&nbsp; exit</span><br /><span style="color: #003366">&nbsp; 146&nbsp; mv ~/master_key ~/.ssh/authorized_keys</span><br /><span style="color: #003366">&nbsp; 147&nbsp; chmod 600 ~/.ssh/authorized_keys</span><br /><span style="color: #003366">&nbsp; 148&nbsp; gzip -d hadoop-0.21.0.tar.gz </span><br /><span style="color: #003366">&nbsp; 149&nbsp; tar -xf hadoop-0.21.0.tar </span><br /><span style="color: #003366">&nbsp; 150&nbsp; java</span><br /><span style="color: #003366">&nbsp; 151&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 152&nbsp; ll /opt/</span><br /><span style="color: #003366">&nbsp; 153&nbsp; ll /usr/</span><br /><span style="color: #003366">&nbsp; 154&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 155&nbsp; ./jdk-6u5-linux-x64.bin</span><br /><span style="color: #003366">&nbsp; 156&nbsp; chmod +w jdk-6u5-linux-x64.bin </span><br /><span style="color: #003366">&nbsp; 157&nbsp; ./jdk-6u5-linux-x64.bin</span><br /><span style="color: #003366">&nbsp; 158&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 159&nbsp; chmod +x jdk-6u5-linux-x64.bin </span><br /><span style="color: #003366">&nbsp; 160&nbsp; ./jdk-6u5-linux-x64.bin</span><br /><span style="color: #003366">&nbsp; 161&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 162&nbsp; rm jdk-6u5-linux-x64.bin </span><br /><span style="color: #003366">&nbsp; 163&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 164&nbsp; java</span><br /><span style="color: #003366">&nbsp; 165&nbsp; sudo gedit /etc/profile </span><br /><span style="color: #003366">&nbsp; 166&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 167&nbsp; java</span><br /><span style="color: #003366">&nbsp; 168&nbsp; export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC Java Home CLASSPATH JRE_HOME</span><br /><span style="color: #003366">&nbsp; 169&nbsp; java</span><br /><span style="color: #003366">&nbsp; 170&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 171&nbsp; export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC Java Home CLASSPATH JRE_HOME</span><br /><span style="color: #003366">&nbsp; 172&nbsp; java</span><br /><span style="color: #003366">&nbsp; 173&nbsp; export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC Java Home CLASSPATH JRE_HOME JAVA_HOME</span><br /><span style="color: #003366">&nbsp; 174&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 175&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 176&nbsp; java</span><br /><span style="color: #003366">&nbsp; 177&nbsp; reboot</span><br /><span style="color: #003366">&nbsp; 178&nbsp; echo PATH</span><br /><span style="color: #003366">&nbsp; 179&nbsp; export</span><br /><span style="color: #003366">&nbsp; 180&nbsp; echo $PATH</span><br /><span style="color: #003366">&nbsp; 181&nbsp; mv ~/master_key ~/.ssh/authorized_keys</span><br /><span style="color: #003366">&nbsp; 182&nbsp; chmod 600 ~/.ssh/authorized_keys</span><br /><span style="color: #003366">&nbsp; 183&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 184&nbsp; vi /etc/bashrc </span><br /><span style="color: #003366">&nbsp; 185&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 186&nbsp; less /root/hadoop-0.21.0/bin/../logs/hadoop-root-namenode-tibcotest.alan.com.out</span><br /><span style="color: #003366">&nbsp; 187&nbsp; ll hadoop-0.21.0/logs/hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 188&nbsp; less hadoop-0.21.0/logs/hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 189&nbsp; rm -rf hadoop-0.21.0/logs/*</span><br /><span style="color: #003366">&nbsp; 190&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 191&nbsp; cd hadoop-0.21.0/logs</span><br /><span style="color: #003366">&nbsp; 192&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 193&nbsp; less hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 194&nbsp; less hadoop-root-tasktracker-localhost.localdomain.log </span><br /><span style="color: #003366">&nbsp; 195&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 196&nbsp; less hadoop-root-tasktracker-localhost.localdomain.log </span><br /><span style="color: #003366">&nbsp; 197&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 198&nbsp; vi conf/mapred-site.xml </span><br /><span style="color: #003366">&nbsp; 199&nbsp; vi conf/mapred-site.xml </span><br /><span style="color: #003366">&nbsp; 200&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 201&nbsp; less hadoop-root-tasktracker-localhost.localdomain.log </span><br /><span style="color: #003366">&nbsp; 202&nbsp; cd logs</span><br /><span style="color: #003366">&nbsp; 203&nbsp; less hadoop-root-tasktracker-localhost.localdomain.log </span><br /><span style="color: #003366">&nbsp; 204&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 205&nbsp; vi conf/core-site.xml </span><br /><span style="color: #003366">&nbsp; 206&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 207&nbsp; vi conf/mapred-site.xml </span><br /><span style="color: #003366">&nbsp; 208&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 209&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 210&nbsp; ll /tmp/hadoop-root/dfs/data/current/finalized/</span><br /><span style="color: #003366">&nbsp; 211&nbsp; vi ll /tmp/hadoop-root/dfs/data/current/finalized/blk_-6267353868788565174</span><br /><span style="color: #003366">&nbsp; 212&nbsp; vi ll /tmp/hadoop-root/dfs/data/current/finalized/blk_-6267353868788565174_1102.meta </span><br /><span style="color: #003366">&nbsp; 213&nbsp; less /tmp/hadoop-root/dfs/data/current/finalized/blk_-6267353868788565174_1102.meta </span><br /><span style="color: #003366">&nbsp; 214&nbsp; less /tmp/hadoop-root/dfs/data/current/finalized/blk_-6267353868788565174</span><br /><span style="color: #003366">&nbsp; 215&nbsp; cd hadoop-0.21.0</span><br /><span style="color: #003366">&nbsp; 216&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 217&nbsp; vi conf/hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 218&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp; 219&nbsp; mkdir hadoop</span><br /><span style="color: #003366">&nbsp; 220&nbsp; mkdir hadoop/data1</span><br /><span style="color: #003366">&nbsp; 221&nbsp; mkdir hadoop/data2</span><br /><span style="color: #003366">&nbsp; 222&nbsp; ll hadoop/data1/</span><br /><span style="color: #003366">&nbsp; 223&nbsp; ll hadoop/data1/</span><br /><span style="color: #003366">&nbsp; 224&nbsp; cd /root/hadoop-0.21.0/conf/</span><br /><span style="color: #003366">&nbsp; 225&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 226&nbsp; vi hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 227&nbsp; tail -fn500 /root/hadoop-0.21.0/bin/../logs/hadoop-root-tasktracker-localhost.localdomain.out</span><br /><span style="color: #003366">&nbsp; 228&nbsp; cd ../logs</span><br /><span style="color: #003366">&nbsp; 229&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 230&nbsp; tail -fn500 hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 231&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 232&nbsp; vi ../conf/hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 233&nbsp; tail -fn500 hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 234&nbsp; vi ../conf/hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 235&nbsp; tail -fn500 hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 236&nbsp; ll /opt/hadoop/data2</span><br /><span style="color: #003366">&nbsp; 237&nbsp; ll /opt/hadoop/data1</span><br /><span style="color: #003366">&nbsp; 238&nbsp; rm rm -rf /opt/hadoop/data1/*</span><br /><span style="color: #003366">&nbsp; 239&nbsp; rm rm -rf /opt/hadoop/data2/*</span><br /><span style="color: #003366">&nbsp; 240&nbsp; tail -fn500 hadoop-root-datanode-localhost.localdomain.log</span><br /><span style="color: #003366">&nbsp; 241&nbsp; vi ../conf/hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 242&nbsp; mkdir /opt/hadoop/data3</span><br /><span style="color: #003366">&nbsp; 243&nbsp; cd /root/hadoop-0.21.0</span><br /><span style="color: #003366">&nbsp; 244&nbsp; cd bin</span><br /><span style="color: #003366">&nbsp; 245&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 246&nbsp; less start-dfs.sh </span><br /><span style="color: #003366">&nbsp; 247&nbsp; cd hadoop-0.21.0/conf</span><br /><span style="color: #003366">&nbsp; 248&nbsp; vi excludes</span><br /><span style="color: #003366">&nbsp; 249&nbsp; vi hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 250&nbsp; vi excludes </span><br /><span style="color: #003366">&nbsp; 251&nbsp; rm -rf /opt/hadoop/data/*</span><br /><span style="color: #003366">&nbsp; 252&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 253&nbsp; cd hadoop-0.21.0/bin/</span><br /><span style="color: #003366">&nbsp; 254&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 255&nbsp; ./hadoop-daemon.sh start datanode</span><br /><span style="color: #003366">&nbsp; 256&nbsp; ./hadoop </span><br /><span style="color: #003366">&nbsp; 257&nbsp; sh start-dfs.sh </span><br /><span style="color: #003366">&nbsp; 258&nbsp; vi start-dfs.sh </span><br /><span style="color: #003366">&nbsp; 259&nbsp; vi stop-dfs.sh </span><br /><span style="color: #003366">&nbsp; 260&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 261&nbsp; vi hadoop-daemon.sh</span><br /><span style="color: #003366">&nbsp; 262&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 263&nbsp; sh hadoop-daemon.sh start datanode</span><br /><span style="color: #003366">&nbsp; 264&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 265&nbsp; sh hadoop-daemon.sh start datanode</span><br /><span style="color: #003366">&nbsp; 266&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 267&nbsp; sh hadoop-daemon.sh stop datanode</span><br /><span style="color: #003366">&nbsp; 268&nbsp; rm rm -rf /opt/hadoop/data1/*</span><br /><span style="color: #003366">&nbsp; 269&nbsp; rm -rf /opt/hadoop/data2/*</span><br /><span style="color: #003366">&nbsp; 270&nbsp; ll /opt/hadoop/</span><br /><span style="color: #003366">&nbsp; 271&nbsp; ll /opt/hadoop/data3</span><br /><span style="color: #003366">&nbsp; 272&nbsp; rm -rf /opt/hadoop/data3/*</span><br /><span style="color: #003366">&nbsp; 273&nbsp; chown -fR hadoop /opt/hadoop</span><br /><span style="color: #003366">&nbsp; 274&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 275&nbsp; ll /opt</span><br /><span style="color: #003366">&nbsp; 276&nbsp; vi /etc/bashrc </span><br /><span style="color: #003366">&nbsp; 277&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 278&nbsp; vi /etc/bashrc </span><br /><span style="color: #003366">&nbsp; 279&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 280&nbsp; cd /root</span><br /><span style="color: #003366">&nbsp; 281&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 282&nbsp; cp -rf jdk1.6.0_05 /opt/</span><br /><span style="color: #003366">&nbsp; 283&nbsp; cd /opt/</span><br /><span style="color: #003366">&nbsp; 284&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 285&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 286&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 287&nbsp; vi /etc/bashrc </span><br /><span style="color: #003366">&nbsp; 288&nbsp; exit</span><br /><span style="color: #003366">&nbsp; 289&nbsp; gzip -d hadoop-0.20.203.0rc1.tar.gz</span><br /><span style="color: #003366">&nbsp; 290&nbsp; tar -xvf hadoop-0.20.203.0rc1.tar</span><br /><span style="color: #003366">&nbsp; 291&nbsp; cd hadoop-0.20.203.0</span><br /><span style="color: #003366">&nbsp; 292&nbsp; \cp -a ../hadoop-0.21.0/conf/core-site.xml conf/core-site.xml</span><br /><span style="color: #003366">&nbsp; 293&nbsp; \cp -a ../hadoop-0.21.0/conf/hdfs-site.xml conf/hdfs-site.xml</span><br /><span style="color: #003366">&nbsp; 294&nbsp; \cp -a ../hadoop-0.21.0/conf/mapred-site.xml conf/mapred-site.xml</span><br /><span style="color: #003366">&nbsp; 295&nbsp; \cp -a ../hadoop-0.21.0/conf/masters conf/masters</span><br /><span style="color: #003366">&nbsp; 296&nbsp; \cp -a ../hadoop-0.21.0/conf/slaves conf/slaves </span><br /><span style="color: #003366">&nbsp; 297&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 298&nbsp; kill -9 3588</span><br /><span style="color: #003366">&nbsp; 299&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 300&nbsp; cp -rf hadoop-0.20.203.0 /opt/hadoop</span><br /><span style="color: #003366">&nbsp; 301&nbsp; useradd hadoop</span><br /><span style="color: #003366">&nbsp; 302&nbsp; passwd hadoop</span><br /><span style="color: #003366">&nbsp; 303&nbsp; mkdir ~/.ssh</span><br /><span style="color: #003366">&nbsp; 304&nbsp; chmod 700 ~/.ssh</span><br /><span style="color: #003366">&nbsp; 305&nbsp; mv ~/authorized_keys ~/.ssh/</span><br /><span style="color: #003366">&nbsp; 306&nbsp; chmod 600 ~/.ssh/authorized_keys</span><br /><span style="color: #003366">&nbsp; 307&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 308&nbsp; java</span><br /><span style="color: #003366">&nbsp; 309&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 310&nbsp; less /etc/profile</span><br /><span style="color: #003366">&nbsp; 311&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 312&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 313&nbsp; cp zookeeper-3.3.3.tar.gz /opt/hadoop/</span><br /><span style="color: #003366">&nbsp; 314&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 315&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 316&nbsp; cd</span><br /><span style="color: #003366">&nbsp; 317&nbsp; cp hbase-0.90.3.tar.gz /opt/hadoop/</span><br /><span style="color: #003366">&nbsp; 318&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 319&nbsp; \cp -a hbase-site.xml hdfs-site.xml hbase-env.sh regionservers /opt/hadoop/hbase/conf/</span><br /><span style="color: #003366">&nbsp; 320&nbsp; chown -fR hadoop /opt/hadoop/</span><br /><span style="color: #003366">&nbsp; 321&nbsp; df</span><br /><span style="color: #003366">&nbsp; 322&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp; 323&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 324&nbsp; rm -rf full.dmp </span><br /><span style="color: #003366">&nbsp; 325&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 326&nbsp; ps -ef |grep 13026</span><br /><span style="color: #003366">&nbsp; 327&nbsp; ps -ef |grep 13026</span><br /><span style="color: #003366">&nbsp; 328&nbsp; cp /opt/hadoop/zookeeper-3.3.3/conf/zoo.cfg /opt/jdk1.6.0_05/lib</span><br /><span style="color: #003366">&nbsp; 329&nbsp; zookeeper-3.3.3/bin/zkServer.sh stop</span><br /><span style="color: #003366">&nbsp; 330&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 331&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 332&nbsp; cp hbase-*.xml /opt/hadoop/hbase/conf/</span><br /><span style="color: #003366">&nbsp; 333&nbsp; rm -rf /opt/hadoop/hbase/conf/hbase-default.xml</span><br /><span style="color: #003366">&nbsp; 334&nbsp; cp hbase-*.xml /opt/hadoop/hbase/conf/</span><br /><span style="color: #003366">&nbsp; 335&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 336&nbsp; less /etc/bashrc </span><br /><span style="color: #003366">&nbsp; 337&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 338&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 339&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 340&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 341&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 342&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 343&nbsp; vi etc/hosts</span><br /><span style="color: #003366">&nbsp; 344&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 345&nbsp; vi /opt/hadoop/hbase/conf/hbase-site.xml </span><br /><span style="color: #003366">&nbsp; 346&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 347&nbsp; kill -9 23754</span><br /><span style="color: #003366">&nbsp; 348&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 349&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 350&nbsp; kill -9 23987</span><br /><span style="color: #003366">&nbsp; 351&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 352&nbsp; kill -9 24355</span><br /><span style="color: #003366">&nbsp; 353&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 354&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 355&nbsp; kill -9 24682 24585</span><br /><span style="color: #003366">&nbsp; 356&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 357&nbsp; kill -9 24887 24984</span><br /><span style="color: #003366">&nbsp; 358&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 359&nbsp; kill -9 HQuorumPeer HRegionServer</span><br /><span style="color: #003366">&nbsp; 360&nbsp; kill -9 25286 25185</span><br /><span style="color: #003366">&nbsp; 361&nbsp; vi /opt/hadoop/hbase/conf/hbase-site.xml </span><br /><span style="color: #003366">&nbsp; 362&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 363&nbsp; kill -9 25582 25485</span><br /><span style="color: #003366">&nbsp; 364&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 365&nbsp; vi /etc/sysctl.conf</span><br /><span style="color: #003366">&nbsp; 366&nbsp; reboot</span><br /><span style="color: #003366">&nbsp; 367&nbsp; cat /proc/sys/net/ipv6/conf/all/disable_ipv6</span><br /><span style="color: #003366">&nbsp; 368&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 369&nbsp; vi /etc/bashrc </span><br /><span style="color: #003366">&nbsp; 370&nbsp; vi /etc/hosts</span><br /><span style="color: #003366">&nbsp; 371&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 372&nbsp; kill -9 5014 5111</span><br /><span style="color: #003366">&nbsp; 373&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 374&nbsp; less /opt/hadoop/hadoop-0.20.203.0/conf/hdfs-site.xml </span><br /><span style="color: #003366">&nbsp; 375&nbsp; less /opt/hadoop/hadoop-0.20.203.0/conf/core-site.xml </span><br /><span style="color: #003366">&nbsp; 376&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 377&nbsp; kill -9 12414 12317</span><br /><span style="color: #003366">&nbsp; 378&nbsp; vi /opt/hadoop/hadoop-0.20.203.0/conf/core-site.xml </span><br /><span style="color: #003366">&nbsp; 379&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 380&nbsp; kill -9 19312</span><br /><span style="color: #003366">&nbsp; 381&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 382&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 383&nbsp; kill -9 19563 19472</span><br /><span style="color: #003366">&nbsp; 384&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 385&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 386&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 387&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 388&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 389&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 390&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 391&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 392&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 393&nbsp; vi /etc/profile</span><br /><span style="color: #003366">&nbsp; 394&nbsp; exit</span><br /><span style="color: #003366">&nbsp; 395&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 396&nbsp; kill -9 17930</span><br /><span style="color: #003366">&nbsp; 397&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 398&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 399&nbsp; kill -9 20659</span><br /><span style="color: #003366">&nbsp; 400&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 401&nbsp; jps</span><br /><span style="color: #003366">&nbsp; 402&nbsp; kill -9 29834 29935</span><br /><span style="color: #003366">&nbsp; 403&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 404&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 405&nbsp; su hadoop</span><br /><span style="color: #003366">&nbsp; 406&nbsp; which java</span><br /><span style="color: #003366">&nbsp; 407&nbsp; mkdir /opt/oracle</span><br /><span style="color: #003366">&nbsp; 408&nbsp; mv apache-tomcat-6.0.30_V1.4.zip /opt/oracle/</span><br /><span style="color: #003366">&nbsp; 409&nbsp; cd /opt/oracle/</span><br /><span style="color: #003366">&nbsp; 410&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 411&nbsp; unzip apache-tomcat-6.0.30_V1.4.zip </span><br /><span style="color: #003366">&nbsp; 412&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 413&nbsp; cd tomcatTemplate/</span><br /><span style="color: #003366">&nbsp; 414&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 415&nbsp; cd bin</span><br /><span style="color: #003366">&nbsp; 416&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 417&nbsp; chmod +x *.sh</span><br /><span style="color: #003366">&nbsp; 418&nbsp; ./createInstance.sh </span><br /><span style="color: #003366">&nbsp; 419&nbsp; ./createInstance.sh -h</span><br /><span style="color: #003366">&nbsp; 420&nbsp; ll ../..</span><br /><span style="color: #003366">&nbsp; 421&nbsp; cd ../../tomcat</span><br /><span style="color: #003366">&nbsp; 422&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 423&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 424&nbsp; rm -rf tomcat</span><br /><span style="color: #003366">&nbsp; 425&nbsp; cd tomcatTemplate/bin</span><br /><span style="color: #003366">&nbsp; 426&nbsp; cd ../../</span><br /><span style="color: #003366">&nbsp; 427&nbsp; cp tomcatTemplate/ tomact-sso</span><br /><span style="color: #003366">&nbsp; 428&nbsp; cp -rf tomcatTemplate/ tomact-sso</span><br /><span style="color: #003366">&nbsp; 429&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 430&nbsp; mv tomact-sso tomcat-sso</span><br /><span style="color: #003366">&nbsp; 431&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 432&nbsp; cd tomcat-sso</span><br /><span style="color: #003366">&nbsp; 433&nbsp; cd bin</span><br /><span style="color: #003366">&nbsp; 434&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 435&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 436&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 437&nbsp; rm -rf tomcat-sso</span><br /><span style="color: #003366">&nbsp; 438&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 439&nbsp; cd tomcatTemplate/bin</span><br /><span style="color: #003366">&nbsp; 440&nbsp; sh createInstance.sh tomcatServer47-0</span><br /><span style="color: #003366">&nbsp; 441&nbsp; cd ../..</span><br /><span style="color: #003366">&nbsp; 442&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 443&nbsp; cd tomcat</span><br /><span style="color: #003366">&nbsp; 444&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 445&nbsp; cd tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 446&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 447&nbsp; less conf/catalina.properties </span><br /><span style="color: #003366">&nbsp; 448&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 449&nbsp; cd bin</span><br /><span style="color: #003366">&nbsp; 450&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 451&nbsp; cd tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 452&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 453&nbsp; cd bin</span><br /><span style="color: #003366">&nbsp; 454&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 455&nbsp; vi resource.properties </span><br /><span style="color: #003366">&nbsp; 456&nbsp; ./createResource.sh </span><br /><span style="color: #003366">&nbsp; 457&nbsp; cd</span><br /><span style="color: #003366">&nbsp; 458&nbsp; mv if-sso-token-server.war /opt/oracle/tomcat/tomcatServer47-0/webapps/</span><br /><span style="color: #003366">&nbsp; 459&nbsp; cd&nbsp; /opt/oracle/tomcat/tomcatServer47-0/bin</span><br /><span style="color: #003366">&nbsp; 460&nbsp; ./catalina.sh start</span><br /><span style="color: #003366">&nbsp; 461&nbsp; tail -fn100 ../logs/catalina.out </span><br /><span style="color: #003366">&nbsp; 462&nbsp; tail -fn100 ../logs/localhost.2011-09-07.log </span><br /><span style="color: #003366">&nbsp; 463&nbsp; ./catalina.sh stop</span><br /><span style="color: #003366">&nbsp; 464&nbsp; ./catalina.sh start</span><br /><span style="color: #003366">&nbsp; 465&nbsp; ./catalina.sh stop</span><br /><span style="color: #003366">&nbsp; 466&nbsp; ./catalina.sh start</span><br /><span style="color: #003366">&nbsp; 467&nbsp; ./catalina.sh stop</span><br /><span style="color: #003366">&nbsp; 468&nbsp; ./catalina.sh start</span><br /><span style="color: #003366">&nbsp; 469&nbsp; mv if-sso-token-server.war /opt/oracle/tomcat/tomcatServer47-0/webapps/</span><br /><span style="color: #003366">&nbsp; 470&nbsp; rm -rf /opt/oracle/tomcat/tomcatServer47-0/webapps/if-sso-token-server</span><br /><span style="color: #003366">&nbsp; 471&nbsp; less /nfs/envconfig/pmd/jms.properties </span><br /><span style="color: #003366">&nbsp; 472&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 473&nbsp; mkdir /nfs</span><br /><span style="color: #003366">&nbsp; 474&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 475&nbsp; cd /nfs</span><br /><span style="color: #003366">&nbsp; 476&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 477&nbsp; mkdir envconfig/pmd</span><br /><span style="color: #003366">&nbsp; 478&nbsp; mkdir envconfig</span><br /><span style="color: #003366">&nbsp; 479&nbsp; mkdir envconfig/pmd</span><br /><span style="color: #003366">&nbsp; 480&nbsp; vi /nfs/envconfig/pmd/jms.properties</span><br /><span style="color: #003366">&nbsp; 481&nbsp; vi /nfs/envconfig/pmd/jms.properties</span><br /><span style="color: #003366">&nbsp; 482&nbsp; vi /nfs/envconfig/pmd/orm.properties</span><br /><span style="color: #003366">&nbsp; 483&nbsp; tail -fn100 /opt/oracle/tomcat/tomcatServer47-0/logs/localhost.2011-09-07.log </span><br /><span style="color: #003366">&nbsp; 484&nbsp; tail -fn100 /opt/log/sso.token.server.log </span><br /><span style="color: #003366">&nbsp; 485&nbsp; ps -ef|grep tomcat</span><br /><span style="color: #003366">&nbsp; 486&nbsp; ps -ef|grep tomcat</span><br /><span style="color: #003366">&nbsp; 487&nbsp; cd /opt/oracle/tomcat/tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 488&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 489&nbsp; ll webapps/</span><br /><span style="color: #003366">&nbsp; 490&nbsp; cd /opt/oracle/</span><br /><span style="color: #003366">&nbsp; 491&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 492&nbsp; cd tomcat</span><br /><span style="color: #003366">&nbsp; 493&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 494&nbsp; cd tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 495&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 496&nbsp; cd conf</span><br /><span style="color: #003366">&nbsp; 497&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 498&nbsp; less context.xml </span><br /><span style="color: #003366">&nbsp; 499&nbsp; cat /etc/redhat-release </span><br /><span style="color: #003366">&nbsp; 500&nbsp; exit</span><br /><span style="color: #003366">&nbsp; 501&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 502&nbsp; cd opt</span><br /><span style="color: #003366">&nbsp; 503&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 504&nbsp; cd /opt/oracle/</span><br /><span style="color: #003366">&nbsp; 505&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 506&nbsp; cd tomcat</span><br /><span style="color: #003366">&nbsp; 507&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 508&nbsp; cd tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 509&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 510&nbsp; cd webapps/</span><br /><span style="color: #003366">&nbsp; 511&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 512&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 513&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 514&nbsp; cd conf</span><br /><span style="color: #003366">&nbsp; 515&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 516&nbsp; less context.xml </span><br /><span style="color: #003366">&nbsp; 517&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 518&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 519&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp; 520&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 521&nbsp; pwd</span><br /><span style="color: #003366">&nbsp; 522&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp; 523&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 524&nbsp; cd oracle/bea/</span><br /><span style="color: #003366">&nbsp; 525&nbsp; cd oracle/</span><br /><span style="color: #003366">&nbsp; 526&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 527&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 528&nbsp; cd bea</span><br /><span style="color: #003366">&nbsp; 529&nbsp; cd /opt</span><br /><span style="color: #003366">&nbsp; 530&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 531&nbsp; cd&nbsp; oracle/</span><br /><span style="color: #003366">&nbsp; 532&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 533&nbsp; cd tomcat</span><br /><span style="color: #003366">&nbsp; 534&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 535&nbsp; cd tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 536&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 537&nbsp; cd webapps/</span><br /><span style="color: #003366">&nbsp; 538&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 539&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 540&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 541&nbsp; cd conf</span><br /><span style="color: #003366">&nbsp; 542&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 543&nbsp; less context.xml </span><br /><span style="color: #003366">&nbsp; 544&nbsp; cd /opt/</span><br /><span style="color: #003366">&nbsp; 545&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 546&nbsp; cd oracle/</span><br /><span style="color: #003366">&nbsp; 547&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 548&nbsp; cd tomcat/tomcatServer47-0/</span><br /><span style="color: #003366">&nbsp; 549&nbsp; cd webapps/</span><br /><span style="color: #003366">&nbsp; 550&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 551&nbsp; ls ..</span><br /><span style="color: #003366">&nbsp; 552&nbsp; history</span><br /><span style="color: #003366">&nbsp; 553&nbsp; cd /opt/oracle/</span><br /><span style="color: #003366">&nbsp; 554&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 555&nbsp; cd tomcat</span><br /><span style="color: #003366">&nbsp; 556&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 557&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 558&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 559&nbsp; cd tomcatTemplate/</span><br /><span style="color: #003366">&nbsp; 560&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 561&nbsp; cd bin</span><br /><span style="color: #003366">&nbsp; 562&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 563&nbsp; sh createInstance.sh tomcatServer47-1</span><br /><span style="color: #003366">&nbsp; 564&nbsp; cd ../../</span><br /><span style="color: #003366">&nbsp; 565&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 566&nbsp; cd tomcat</span><br /><span style="color: #003366">&nbsp; 567&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 568&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 569&nbsp; cd ..</span><br /><span style="color: #003366">&nbsp; 570&nbsp; ls</span><br /><span style="color: #003366">&nbsp; 571&nbsp; ll</span><br /><span style="color: #003366">&nbsp; 572&nbsp; history</span></font></font></p><img src ="http://www.blogjava.net/alancxx/aggbug/362096.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-10-26 14:23 <a href="http://www.blogjava.net/alancxx/archive/2011/10/26/362096.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle 与 Mysql 常用命令对比</title><link>http://www.blogjava.net/alancxx/articles/350284.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Sun, 15 May 2011 11:05:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/350284.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/350284.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/350284.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/350284.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/350284.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 13px; color: rgb(75,75,75); line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">
<table cellspacing="1" cellpadding="1" border="1">
    <tbody>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word"></td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word"><strong>Oracle</strong></td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word"><strong>mysql</strong></td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">对比版本</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">Enterprise&nbsp;Oracle10g Release 10.2.0.1.0</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql 4.1.21-nt</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">默认安装目录</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">..\oracle\product\10.2.0</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">..\MySQL\MySQL Server 4.1</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">各种实用程序所在目录</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">..\oracle\product\10.2.0\db_1\BIN</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">..\MySQL\MySQL Server 4.1\bin</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">客户程序</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL*Plus</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">安装后系统默认用户（库）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">sys<br />
            system<br />
            scott</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql<br />
            test</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">显示所有用户（库）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL &gt;select * from all_users;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; show databases;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">退出命令</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; exit<br />
            SQL&gt; quit</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; exit<br />
            mysql&gt; quit</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">改变连接用户（库）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; conn 用户名/密码@主机字符串</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; use 库名</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">查询当前所有的表</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select * from tab;<br />
            SQL&gt; select * from cat;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; show tables;<br />
            F:MySQLbin&gt;mysqlshow 库名</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">显示当前连接用户（库）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; show user</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; connect</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">查看帮助</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; ?</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; help</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">显示表结构</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; desc 表名<br />
            SQL&gt; describe 表名</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; desc 表名;<br />
            mysql&gt; describe 表名;<br />
            mysql&gt; show columns from 表名;<br />
            F:MySQLbin&gt;mysqlshow 库名 表名</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">日期函数</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select sysdate from dual;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select now();<br />
            mysql&gt; select sysdate();<br />
            mysql&gt; select curdate();<br />
            mysql&gt; select current_date;<br />
            mysql&gt; select curtime();<br />
            mysql&gt; select current_time;<br />
            </td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">日期格式化</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select to_char(sysdate,'yyyy-mm-dd') from dual;<br />
            SQL&gt; select to_char(sysdate,'hh24-mi-ss') from dual;<br />
            </td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select date_format(now(),'%Y-%m-%d');<br />
            mysql&gt; select time_format(now(),'%H-%i-%S');<br />
            </td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">日期函数<br />
            （增加一个月）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select to_char(add_months(to_date('20000101','yyyymmdd'),1),'yyyy-mm-dd') from dual;<br />
            结果：2000-02-01<br />
            SQL&gt; select to_char(add_months(to_date('20000101','yyyymmdd'),5),'yyyy-mm-dd') from dual;<br />
            结果：2000-06-01<br />
            </td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select date_add('2000-01-01',interval 1 month);<br />
            结果：2000-02-01<br />
            mysql&gt; select date_add('2000-01-01',interval 5 month);<br />
            结果：2000-06-01<br />
            </td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">别名</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select 1 a from dual;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select 1 as a;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">字符串截取函数</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select substr('abcdefg',1,5) from dual;<br />
            SQL&gt; select substrb('abcdefg',1,5) from dual;<br />
            结果：abcde</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select substring('abcdefg',2,3);<br />
            结果：bcd<br />
            mysql&gt; select mid('abcdefg',2,3);<br />
            结果：bcd<br />
            mysql&gt; select substring('abcdefg',2);<br />
            结果：bcdefg<br />
            mysql&gt; select substring('abcdefg' from 2);<br />
            结果：bcdefg<br />
            另有SUBSTRING_INDEX(str,delim,count)函数<br />
            返回从字符串str的第count个出现的分隔符delim之后的子串。<br />
            如果count是正数，返回最后的分隔符到左边(从左边数) 的所有字符。<br />
            如果count是负数，返回最后的分隔符到右边的所有字符(从右边数)。<br />
            </td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">执行外部脚本命令</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL &gt;@f:\sql\a.sql</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">1：mysql&gt; source f:/sql/a.sql<br />
            2：F:MySQLbin&gt;mysql<span class="Apple-converted-space">&nbsp;</span><a>3：F:MySQLbin&gt;mysql 库名</a><a td=""></a></td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">改表名</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; rename a to b;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; alter table a rename b;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">执行命令</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">;&lt;回车&gt;<br />
            /<br />
            r<br />
            run</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">;&lt;回车&gt;<br />
            go<br />
            ego</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">distinct用法</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select distinct 列1 from 表1;<br />
            SQL&gt; select distinct 列1,列2 from 表1;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select distinct 列1 from 表1;<br />
            mysql&gt; select distinct 列1,列2 from 表1;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">注释</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">--<br />
            /*与*/</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">#<br />
            --<br />
            /*与*/</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">限制返回记录条数</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select * from 表名 where rownum&lt;5;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; select * from 表名 limit 5;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">新建用户（库）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; create user 用户名 identified by 密码;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; create database 库名;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">删用户（库）</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; drop user 用户名;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; drop database 库名;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">外连接</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">使用(+)</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">使用left join</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">查询索引</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL&gt; select index_name,table_name from user_indexes;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">mysql&gt; show index from 表名 [FROM 库名];</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">通配符</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">&#8220;%&#8221;</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">&#8220;%&#8221;和&#8220;_&#8221;</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SQL语法</td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SELECT selection_list 选择哪些列<br />
            FROM table_list 从何处选择行<br />
            WHERE primary_constraint 行必须满足什么条件<br />
            GROUP BY grouping_columns 怎样对结果分组<br />
            HAVING secondary_constraint 行必须满足的第二条件<br />
            ORDER BY sorting_columns 怎样对结果排序<br />
            </td>
            <td style="font-size: 12px; color: rgb(69,69,69); line-height: 14px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; word-wrap: break-word">SELECT selection_list 选择哪些列<br />
            FROM table_list 从何处选择行<br />
            WHERE primary_constraint 行必须满足什么条件<br />
            GROUP BY grouping_columns 怎样对结果分组<br />
            HAVING secondary_constraint 行必须满足的第二条件<br />
            ORDER BY sorting_columns 怎样对结果排序</td>
        </tr>
    </tbody>
</table>
</span></span><br class="Apple-interchange-newline" />
<img src ="http://www.blogjava.net/alancxx/aggbug/350284.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-05-15 19:05 <a href="http://www.blogjava.net/alancxx/articles/350284.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hadoop介绍</title><link>http://www.blogjava.net/alancxx/articles/350080.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Thu, 12 May 2011 03:14:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/350080.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/350080.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/350080.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/350080.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/350080.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(0,51,51); font-family: Georgia; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px"><span style="font-size: 14pt">这里先大致介绍一下Hadoop.<br />
&nbsp;&nbsp;&nbsp; 本文大部分内容都是从官网</span><a title="Hadoop" style="color: rgb(95,158,160); text-decoration: none" href="http://lucene.apache.org/hadoop"><span style="font-size: 14pt">Hadoop</span></a><span style="font-size: 14pt">上来的。其中有一篇</span><a title="介绍HDFS的pdf文档" style="color: rgb(95,158,160); text-decoration: none" href="http://lucene.apache.org/hadoop/hdfs_design.pdf"><span style="font-size: 14pt">介绍HDFS的pdf文档</span></a><span style="font-size: 14pt">，里面对Hadoop介绍的比较全面了。我的这一个系列的Hadoop学习笔记也是从</span><a title="这里" style="color: rgb(95,158,160); text-decoration: none" href="http://wiki.apache.org/lucene-hadoop/GettingStartedWithHadoop"><span style="font-size: 14pt">这里</span></a><span style="font-size: 14pt">一步一步进行下来的，同时又参考了网上的很多文章，对学习Hadoop中遇到的问题进行了归纳总结。<br />
&nbsp;&nbsp;&nbsp; 言归正传，先说一下Hadoop的来龙去脉。谈到Hadoop就不得不提到</span><a title="Lucene" style="color: rgb(95,158,160); text-decoration: none" href="http://lucene.apache.org/"><span style="font-size: 14pt">Lucene</span></a><span style="font-size: 14pt">和</span><a title="Nutch" style="color: rgb(95,158,160); text-decoration: none" href="http://lucene.apache.org/nutch/"><span style="font-size: 14pt">Nutch</span></a><span style="font-size: 14pt">。首先，<span style="color: rgb(0,0,255)">Lucene并不是一个应用程序，而是提供了一个纯Java的高性能全文索引引擎工具包</span>，它可以方便的嵌入到各种实际应用中实现全文搜索/索引功能。<span style="color: rgb(0,0,255)">Nutch是一个应用程序，是一个以Lucene为基础实现的搜索引擎应用</span>，Lucene 为Nutch提供了文本搜索和索引的API，Nutch不光有搜索的功能，还有数据抓取的功能。在nutch0.8.0版本之前，Hadoop还属于 Nutch的一部分，而从nutch0.8.0开始，将其中实现的NDFS和MapReduce剥离出来成立一个新的开源项目，这就是Hadoop，而 nutch0.8.0版本较之以前的Nutch在架构上有了根本性的变化，那就是完全构建在Hadoop的基础之上了。在Hadoop中实现了 Google的GFS和MapReduce算法，使Hadoop成为了一个分布式的计算平台。<br />
&nbsp;&nbsp;&nbsp;其实，Hadoop并不仅仅是一个用于存储的分布式文件系统，而是设计用来在由通用计算设备组成的大型集群上执行分布式应用的框架。<br />
<br />
&nbsp;&nbsp;&nbsp;<span style="color: rgb(0,0,255)">Hadoop包含两个部分：</span><br />
<br />
&nbsp;&nbsp;&nbsp;<strong>1、HDFS<br />
</strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;即Hadoop Distributed File System (Hadoop分布式文件系统)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDFS 具有高容错性，并且可以被部署在低价的硬件设备之上。HDFS很适合那些有大数据集的应用，并且提供了对数据读写的高吞吐率。HDFS是一个 master/slave的结构，就通常的部署来说，在master上只运行一个Namenode，而在每一个slave上运行一个Datanode。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDFS 支持传统的层次文件组织结构，同现有的一些文件系统在操作上很类似，比如你可以创建和删除一个文件，把一个文件从一个目录移到另一个目录，重命名等等操 作。Namenode管理着整个分布式文件系统，对文件系统的操作（如建立、删除文件和文件夹）都是通过Namenode来控制。&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;下面是HDFS的结构：</span><br />
<div align="center">
<div align="center"><img height="645" alt="" src="http://www.cnblogs.com/images/cnblogs_com/wayne1017/HDFSArch.JPG" width="861" border="0" /></div>
</div>
<br />
<span style="font-size: 14pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从上面的图中可以看 出，Namenode，Datanode，Client之间的通信都是建立在TCP/IP的基础之上的。当Client要执行一个写入的操作的时候，命令 不是马上就发送到Namenode，Client首先在本机上临时文件夹中缓存这些数据，当临时文件夹中的数据块达到了设定的Block的值（默认是 64M）时，Client便会通知Namenode，Namenode便响应Client的RPC请求，将文件名插入文件系统层次中并且在 Datanode中找到一块存放该数据的block，同时将该Datanode及对应的数据块信息告诉Client，Client便这些本地临时文件夹中 的数据块写入指定的数据节点。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDFS采取了副本策略，其目的是为了提高系统的可靠性，可用性。HDFS的副本放置策略是三个副本， 一个放在本节点上，一个放在同一机架中的另一个节点上，还有一个副本放在另一个不同的机架中的一个节点上。当前版本的hadoop0.12.0中还没有实 现，但是正在进行中，相信不久就可以出来了。<br />
<br />
&nbsp;&nbsp;&nbsp;<strong>2、MapReduce的实现<br />
</strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a title="MapReduce" style="color: rgb(95,158,160); text-decoration: none" href="http://labs.google.com/papers/mapreduce.html"><span style="font-size: 14pt">MapReduce</span></a><span style="font-size: 14pt">是Google 的一项重要技术，它是一个编程模型，用以进行大数据量的计算。对于大数据量的计算，通常采用的处理手法就是并行计算。至少现阶段而言，对许多开发人员来 说，并行计算还是一个比较遥远的东西。MapReduce就是一种简化并行计算的编程模型，它让那些没有多少并行计算经验的开发人员也可以开发并行应用。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MapReduce的名字源于这个模型中的两项核心操作：Map和 Reduce。也许熟悉Functional Programming（</span><a title="函数式编程" style="color: rgb(95,158,160); text-decoration: none" href="http://chn.blogbeta.com/232.html"><span style="font-size: 14pt">函数式编程</span></a><span style="font-size: 14pt">） 的人见到这两个词会倍感亲切。简单的说来，Map是把一组数据一对一的映射为另外的一组数据，其映射的规则由一个函数来指定，比如对[1, 2, 3, 4]进行乘2的映射就变成了[2, 4, 6, 8]。Reduce是对一组数据进行归约，这个归约的规则由一个函数指定，比如对[1, 2, 3, 4]进行求和的归约得到结果是10，而对它进行求积的归约结果是24。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关于MapReduce的内容，建议看看孟岩的这篇</span><a title="MapReduce:The free lunch is not over!" style="color: rgb(95,158,160); text-decoration: none" href="http://www.mengyan.org/blog/archives/2006/11/15/138.html"><span style="font-size: 14pt">MapReduce:The Free Lunch Is Not Over!</span></a><br />
<br />
<span style="font-size: 14pt">&nbsp;&nbsp;&nbsp;好了，作为这个系列的第一篇就写这么多了，我也是刚开始接触Hadoop，下一篇就是讲Hadoop的部署，谈谈我在部署Hadoop时遇到的问题，也给大家一个参考，少走点弯路。</span></span></span>
<img src ="http://www.blogjava.net/alancxx/aggbug/350080.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-05-12 11:14 <a href="http://www.blogjava.net/alancxx/articles/350080.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring中的ServletContext使用</title><link>http://www.blogjava.net/alancxx/articles/349896.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Tue, 10 May 2011 02:02:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/349896.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/349896.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/349896.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/349896.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/349896.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(61,32,14); line-height: 20px; font-family: Georgia; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">
<p style="line-height: normal">Spring 不但提供了一个功能全面的应用开发框架，本身还拥有众多可以在程序编写时直接使用的工具类，您不但可以在 Spring 应用中使用这些工具类，也可以在其它的应用中使用，这些工具类中的大部分是可以在脱离 Spring 框架时使用的。了解 Spring 中有哪些好用的工具类并在程序编写时适当使用，将有助于提高开发效率、增强代码质量。</p>
<p style="line-height: normal">在这个分为两部分的文章中，我们将从众多的 Spring 工具类中遴选出那些好用的工具类介绍给大家。第 1 部分将介绍与文件资源操作和 Web 相关的工具类。在 第 2 部分 中将介绍特殊字符转义和方法入参检测工具类。</p>
<p style="line-height: normal">文件资源操作</p>
<p style="line-height: normal">文件资源的操作是应用程序中常见的功能，如当上传一个文件后将其保存在特定目录下，从指定地址加载一个配置文件等等。我们一般使用 JDK 的 I/O 处理类完成这些操作，但对于一般的应用程序来说，JDK 的这些操作类所提供的方法过于底层，直接使用它们进行文件操作不但程序编写复杂而且容易产生错误。相比于 JDK 的 File，Spring 的 Resource 接口（资源概念的描述接口）抽象层面更高且涵盖面更广，Spring 提供了许多方便易用的资源操作工具类，它们大大降低资源操作的复杂度，同时具有更强的普适性。这些工具类不依赖于 Spring 容器，这意味着您可以在程序中象一般普通类一样使用它们。</p>
<p style="line-height: normal">加载文件资源</p>
<p style="line-height: normal">Spring 定义了一个 org.springframework.core.io.Resource 接口，Resource 接口是为了统一各种类型不同的资源而定义的，Spring 提供了若干 Resource 接口的实现类，这些实现类可以轻松地加载不同类型的底层资源，并提供了获取文件名、URL 地址以及资源内容的操作方法。</p>
<p style="line-height: normal">访问文件资源</p>
<p style="line-height: normal">假设有一个文件地位于 Web 应用的类路径下，您可以通过以下方式对这个文件资源进行访问：</p>
<p style="line-height: normal">通过 FileSystemResource 以文件系统绝对路径的方式进行访问；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
通过 ClassPathResource 以类路径的方式进行访问；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
通过 ServletContextResource 以相对于Web应用根目录的方式进行访问。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
相比于通过 JDK 的 File 类访问文件资源的方式，Spring 的 Resource 实现类无疑提供了更加灵活的操作方式，您可以根据情况选择适合的 Resource 实现类访问资源。下面，我们分别通过 FileSystemResource 和 ClassPathResource 访问同一个文件资源：</p>
<p style="line-height: normal"><br style="line-height: normal" />
清单 1. FileSourceExample<br style="line-height: normal" />
package com.baobaotao.io;<br style="line-height: normal" />
import java.io.IOException;<br style="line-height: normal" />
import java.io.InputStream;<br style="line-height: normal" />
import org.springframework.core.io.ClassPathResource;<br style="line-height: normal" />
import org.springframework.core.io.FileSystemResource;<br style="line-height: normal" />
import org.springframework.core.io.Resource;<br style="line-height: normal" />
public class FileSourceExample {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String filePath =<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "D:/masterSpring/chapter23/webapp/WEB-INF/classes/conf/file1.txt";<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ① 使用系统文件路径方式加载文件<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource res1 = new FileSystemResource(filePath);<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ② 使用类路径方式加载文件<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource res2 = new ClassPathResource("conf/file1.txt");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InputStream ins1 = res1.getInputStream();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InputStream ins2 = res2.getInputStream();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("res1:"+res1.getFilename());<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("res2:"+res2.getFilename());<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IOException e) {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
在获取资源后，您就可以通过 Resource 接口定义的多个方法访问文件的数据和其它的信息：如您可以通过 getFileName() 获取文件名，通过 getFile() 获取资源对应的 File 对象，通过 getInputStream() 直接获取文件的输入流。此外，您还可以通过 createRelative(String relativePath) 在资源相对地址上创建新的资源。</p>
<p style="line-height: normal">在 Web 应用中，您还可以通过 ServletContextResource 以相对于 Web 应用根目录的方式访问文件资源，如下所示：</p>
<p style="line-height: normal">&lt;%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%&gt;<br style="line-height: normal" />
&lt;jsp:directive.page import="<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; org.springframework.web.context.support.ServletContextResource"/&gt;<br style="line-height: normal" />
&lt;jsp:directive.page import="org.springframework.core.io.Resource"/&gt;<br style="line-height: normal" />
&lt;%<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; // ① 注意文件资源地址以相对于 Web 应用根路径的方式表示<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; Resource res3 = new ServletContextResource(application,<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "/WEB-INF/classes/conf/file1.txt");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; out.print(res3.getFilename());<br style="line-height: normal" />
%&gt;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
对于位于远程服务器（Web 服务器或 FTP 服务器）的文件资源，您则可以方便地通过 UrlResource 进行访问。</p>
<p style="line-height: normal">为了方便访问不同类型的资源，您必须使用相应的 Resource 实现类，是否可以在不显式使用 Resource 实现类的情况下，仅根据带特殊前缀的资源地址直接加载文件资源呢？Spring 提供了一个 ResourceUtils 工具类，它支持&#8220;classpath:&#8221;和&#8220;file:&#8221;的地址前缀，它能够从指定的地址加载文件资源，请看下面的例子：</p>
<p style="line-height: normal"><br style="line-height: normal" />
清单 2. ResourceUtilsExample<br style="line-height: normal" />
package com.baobaotao.io;<br style="line-height: normal" />
import java.io.File;<br style="line-height: normal" />
import org.springframework.util.ResourceUtils;<br style="line-height: normal" />
public class ResourceUtilsExample {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Throwable{<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File clsFile = ResourceUtils.getFile("classpath:conf/file1.txt");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(clsFile.isFile());</p>
<p style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String httpFilePath = "file:D:/masterSpring/chapter23/src/conf/file1.txt";<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File httpFile = ResourceUtils.getFile(httpFilePath);<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(httpFile.isFile());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
ResourceUtils 的 getFile(String resourceLocation) 方法支持带特殊前缀的资源地址，这样，我们就可以在不和 Resource 实现类打交道的情况下使用 Spring 文件资源加载的功能了。</p>
<p style="line-height: normal">本地化文件资源</p>
<p style="line-height: normal">本地化文件资源是一组通过本地化标识名进行特殊命名的文件，Spring 提供的 LocalizedResourceHelper 允许通过文件资源基名和本地化实体获取匹配的本地化文件资源并以 Resource 对象返回。假设在类路径的 i18n 目录下，拥有一组基名为 message 的本地化文件资源，我们通过以下实例演示获取对应中国大陆和美国的本地化文件资源：</p>
<p style="line-height: normal"><br style="line-height: normal" />
清单 3. LocaleResourceTest<br style="line-height: normal" />
package com.baobaotao.io;<br style="line-height: normal" />
import java.util.Locale;<br style="line-height: normal" />
import org.springframework.core.io.Resource;<br style="line-height: normal" />
import org.springframework.core.io.support.LocalizedResourceHelper;<br style="line-height: normal" />
public class LocaleResourceTest {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LocalizedResourceHelper lrHalper = new LocalizedResourceHelper();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ① 获取对应美国的本地化文件资源<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource msg_us = lrHalper.findLocalizedResource("i18n/message", ".properties",<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Locale.US);<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ② 获取对应中国大陆的本地化文件资源<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource msg_cn = lrHalper.findLocalizedResource("i18n/message", ".properties",<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Locale.CHINA);<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("fileName(us):"+msg_us.getFilename());<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("fileName(cn):"+msg_cn.getFilename());<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
虽然 JDK 的 java.util.ResourceBundle 类也可以通过相似的方式获取本地化文件资源，但是其返回的是 ResourceBundle 类型的对象。如果您决定统一使用 Spring 的 Resource 接表征文件资源，那么 LocalizedResourceHelper 就是获取文件资源的非常适合的帮助类了。</p>
<p style="line-height: normal">文件操作</p>
<p style="line-height: normal">在使用各种 Resource 接口的实现类加载文件资源后，经常需要对文件资源进行读取、拷贝、转存等不同类型的操作。您可以通过 Resource 接口所提供了方法完成这些功能，不过在大多数情况下，通过 Spring 为 Resource 所配备的工具类完成文件资源的操作将更加方便。</p>
<p style="line-height: normal">文件内容拷贝</p>
<p style="line-height: normal">第一个我们要认识的是 FileCopyUtils，它提供了许多一步式的静态操作方法，能够将文件内容拷贝到一个目标 byte[]、String 甚至一个输出流或输出文件中。下面的实例展示了 FileCopyUtils 具体使用方法：</p>
<p style="line-height: normal"><br style="line-height: normal" />
清单 4. FileCopyUtilsExample<br style="line-height: normal" />
package com.baobaotao.io;<br style="line-height: normal" />
import java.io.ByteArrayOutputStream;<br style="line-height: normal" />
import java.io.File;<br style="line-height: normal" />
import java.io.FileReader;<br style="line-height: normal" />
import java.io.OutputStream;<br style="line-height: normal" />
import org.springframework.core.io.ClassPathResource;<br style="line-height: normal" />
import org.springframework.core.io.Resource;<br style="line-height: normal" />
import org.springframework.util.FileCopyUtils;<br style="line-height: normal" />
public class FileCopyUtilsExample {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Throwable {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource res = new ClassPathResource("conf/file1.txt");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ① 将文件内容拷贝到一个 byte[] 中<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile());<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ② 将文件内容拷贝到一个 String 中<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String fileStr = FileCopyUtils.copyToString(new FileReader(res.getFile()));<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ③ 将文件内容拷贝到另一个目标文件<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileCopyUtils.copy(res.getFile(),<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new File(res.getFile().getParent()+ "/file2.txt"));</p>
<p style="line-height: normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ④ 将文件内容拷贝到一个输出流中<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OutputStream os = new ByteArrayOutputStream();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileCopyUtils.copy(res.getInputStream(), os);<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
往往我们都通过直接操作 InputStream 读取文件的内容，但是流操作的代码是比较底层的，代码的面向对象性并不强。通过 FileCopyUtils 读取和拷贝文件内容易于操作且相当直观。如在 ① 处，我们通过 FileCopyUtils 的 copyToByteArray(File in) 方法就可以直接将文件内容读到一个 byte[] 中；另一个可用的方法是 copyToByteArray(InputStream in)，它将输入流读取到一个 byte[] 中。</p>
<p style="line-height: normal">如果是文本文件，您可能希望将文件内容读取到 String 中，此时您可以使用 copyToString(Reader in) 方法，如 ② 所示。使用 FileReader 对 File 进行封装，或使用 InputStreamReader 对 InputStream 进行封装就可以了。</p>
<p style="line-height: normal">FileCopyUtils 还提供了多个将文件内容拷贝到各种目标对象中的方法，这些方法包括：</p>
<p style="line-height: normal">方法 说明<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static void copy(byte[] in, File out) 将 byte[] 拷贝到一个文件中<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static void copy(byte[] in, OutputStream out) 将 byte[] 拷贝到一个输出流中<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static int copy(File in, File out) 将文件拷贝到另一个文件中<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static int copy(InputStream in, OutputStream out) 将输入流拷贝到输出流中<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static int copy(Reader in, Writer out) 将 Reader 读取的内容拷贝到 Writer 指向目标输出中<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static void copy(String in, Writer out) 将字符串拷贝到一个 Writer 指向的目标中</p>
<p style="line-height: normal">在实例中，我们虽然使用 Resource 加载文件资源，但 FileCopyUtils 本身和 Resource 没有任何关系，您完全可以在基于 JDK I/O API 的程序中使用这个工具类。</p>
<p style="line-height: normal">属性文件操作</p>
<p style="line-height: normal">我们知道可以通过 java.util.Properties的load(InputStream inStream) 方法从一个输入流中加载属性资源。Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源，请看下面的例子：</p>
<p style="line-height: normal">package com.baobaotao.io;<br style="line-height: normal" />
import java.util.Properties;<br style="line-height: normal" />
import org.springframework.core.io.support.PropertiesLoaderUtils;<br style="line-height: normal" />
public class PropertiesLoaderUtilsExample {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Throwable {&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ① jdbc.properties 是位于类路径下的文件<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Properties props = PropertiesLoaderUtils.loadAllProperties("jdbc.properties");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(props.getProperty("jdbc.driverClassName"));<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
一般情况下，应用程序的属性文件都放置在类路径下，所以 PropertiesLoaderUtils 比之于 Properties#load(InputStream inStream) 方法显然具有更强的实用性。此外，PropertiesLoaderUtils 还可以直接从 Resource 对象中加载属性资源：</p>
<p style="line-height: normal">方法 说明<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static Properties loadProperties(Resource resource) 从 Resource 中加载属性<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
static void fillProperties(Properties props, Resource resource) 将 Resource 中的属性数据添加到一个已经存在的 Properties 对象中</p>
<p style="line-height: normal">特殊编码的资源</p>
<p style="line-height: normal">当您使用 Resource 实现类加载文件资源时，它默认采用操作系统的编码格式。如果文件资源采用了特殊的编码格式（如 UTF-8），则在读取资源内容时必须事先通过 EncodedResource 指定编码格式，否则将会产生中文乱码的问题。</p>
<p style="line-height: normal"><br style="line-height: normal" />
清单 5. EncodedResourceExample<br style="line-height: normal" />
package com.baobaotao.io;<br style="line-height: normal" />
import org.springframework.core.io.ClassPathResource;<br style="line-height: normal" />
import org.springframework.core.io.Resource;<br style="line-height: normal" />
import org.springframework.core.io.support.EncodedResource;<br style="line-height: normal" />
import org.springframework.util.FileCopyUtils;<br style="line-height: normal" />
public class EncodedResourceExample {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Throwable {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource res = new ClassPathResource("conf/file1.txt");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ① 指定文件资源对应的编码格式（UTF-8）<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EncodedResource encRes = new EncodedResource(res,"UTF-8");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ② 这样才能正确读取文件的内容，而不会出现乱码<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String content = FileCopyUtils.copyToString(encRes.getReader());<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(content);<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
EncodedResource 拥有一个 getResource() 方法获取 Resource，但该方法返回的是通过构造函数传入的原 Resource 对象，所以必须通过 EncodedResource#getReader() 获取应用编码后的 Reader 对象，然后再通过该 Reader 读取文件的内容。</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
回页首<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
</p>
<p style="line-height: normal"></p>
<p style="line-height: normal">Web 相关工具类</p>
<p style="line-height: normal">您几乎总是使用 Spring 框架开发 Web 的应用，Spring 为 Web 应用提供了很多有用的工具类，这些工具类可以给您的程序开发带来很多便利。在这节里，我们将逐一介绍这些工具类的使用方法。</p>
<p style="line-height: normal">操作 Servlet API 的工具类</p>
<p style="line-height: normal">当您在控制器、JSP 页面中想直接访问 Spring 容器时，您必须事先获取 WebApplicationContext 对象。Spring 容器在启动时将 WebApplicationContext 保存在 ServletContext的属性列表中，通过 WebApplicationContextUtils 工具类可以方便地获取 WebApplicationContext 对象。</p>
<p style="line-height: normal">WebApplicationContextUtils</p>
<p style="line-height: normal">当 Web 应用集成 Spring 容器后，代表 Spring 容器的 WebApplicationContext 对象将以 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键存放在 ServletContext 属性列表中。您当然可以直接通过以下语句获取 WebApplicationContext：</p>
<p style="line-height: normal">WebApplicationContext wac = (WebApplicationContext)servletContext.<br style="line-height: normal" />
getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
但通过位于 org.springframework.web.context.support 包中的 WebApplicationContextUtils 工具类获取 WebApplicationContext 更方便：</p>
<p style="line-height: normal">WebApplicationContext wac =WebApplicationContextUtils.<br style="line-height: normal" />
getWebApplicationContext(servletContext);</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
当 ServletContext 属性列表中不存在 WebApplicationContext 时，getWebApplicationContext() 方法不会抛出异常，它简单地返回 null。如果后续代码直接访问返回的结果将引发一个 NullPointerException 异常，而 WebApplicationContextUtils 另一个 getRequiredWebApplicationContext(ServletContext sc) 方法要求 ServletContext 属性列表中一定要包含一个有效的 WebApplicationContext 对象，否则马上抛出一个 IllegalStateException 异常。我们推荐使用后者，因为它能提前发现错误的时间，强制开发者搭建好必备的基础设施。</p>
<p style="line-height: normal">WebUtils</p>
<p style="line-height: normal">位于 org.springframework.web.util 包中的 WebUtils 是一个非常好用的工具类，它对很多 Servlet API 提供了易用的代理方法，降低了访问 Servlet API 的复杂度，可以将其看成是常用 Servlet API 方法的门面类。</p>
<p style="line-height: normal">下面这些方法为访问 HttpServletRequest 和 HttpSession 中的对象和属性带来了方便：</p>
<p style="line-height: normal">方法 说明<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
Cookie getCookie(HttpServletRequest request, String name) 获取 HttpServletRequest 中特定名字的 Cookie 对象。如果您需要创建 Cookie， Spring 也提供了一个方便的 CookieGenerator 工具类；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
Object getSessionAttribute(HttpServletRequest request, String name) 获取 HttpSession 特定属性名的对象，否则您必须通过request.getHttpSession.getAttribute(name) 完成相同的操作；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
Object getRequiredSessionAttribute(HttpServletRequest request, String name) 和上一个方法类似，只不过强制要求 HttpSession 中拥有指定的属性，否则抛出异常；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
String getSessionId(HttpServletRequest request) 获取 Session ID 的值；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
void exposeRequestAttributes(ServletRequest request, Map attributes) 将 Map 元素添加到 ServletRequest 的属性列表中，当请求被导向（forward）到下一个处理程序时，这些请求属性就可以被访问到了；</p>
<p style="line-height: normal">此外，WebUtils还提供了一些和ServletContext相关的方便方法：</p>
<p style="line-height: normal">方法 说明<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
String getRealPath(ServletContext servletContext, String path) 获取相对路径对应文件系统的真实文件路径；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
File getTempDir(ServletContext servletContext) 获取 ServletContex 对应的临时文件地址，它以 File 对象的形式返回。</p>
<p style="line-height: normal">下面的片断演示了使用 WebUtils 从 HttpSession 中获取属性对象的操作：</p>
<p style="line-height: normal">protected Object formBackingObject(HttpServletRequest request) throws Exception {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request,<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "userSession");<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; if (userSession != null) {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new AccountForm(this.petStore.getAccount(<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; userSession.getAccount().getUsername()));<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; } else {<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new AccountForm();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal" />
}</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
Spring 所提供的过滤器和监听器</p>
<p style="line-height: normal">Spring 为 Web 应用提供了几个过滤器和监听器，在适合的时间使用它们，可以解决一些常见的 Web 应用问题。</p>
<p style="line-height: normal">延迟加载过滤器</p>
<p style="line-height: normal">Hibernate 允许对关联对象、属性进行延迟加载，但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层，当 Web 层访问到那些需要延迟加载的数据时，由于加载领域对象的 Hibernate Session 已经关闭，这些导致延迟加载数据的访问异常。</p>
<p style="line-height: normal">Spring 为此专门提供了一个 OpenSessionInViewFilter 过滤器，它的主要功能是使每个请求过程绑定一个 Hibernate Session，即使最初的事务已经完成了，也可以在 Web 层进行延迟加载的操作。</p>
<p style="line-height: normal">OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中，它将自动被 Spring 的事务管理器探测到。所以 OpenSessionInViewFilter 适用于 Service 层使用HibernateTransactionManager 或 JtaTransactionManager 进行事务管理的环境，也可以用于非事务只读的数据操作中。</p>
<p style="line-height: normal">要启用这个过滤器，必须在 web.xml 中对此进行配置：</p>
<p style="line-height: normal">&#8230;<br style="line-height: normal" />
&lt;filter&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;filter-class&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; org.springframework.orm.hibernate3.support.OpenSessionInViewFilter<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/filter-class&gt;<br style="line-height: normal" />
&lt;/filter&gt;<br style="line-height: normal" />
&lt;filter-mapping&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;<br style="line-height: normal" />
&lt;/filter-mapping&gt;<br style="line-height: normal" />
&#8230;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal">上面的配置，我们假设使用 .html 的后缀作为 Web 框架的 URL 匹配模式，如果您使用 Struts 等 Web 框架，可以将其改为对应的&#8220;*.do&#8221;模型。<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
中文乱码过滤器</p>
<p style="line-height: normal">在您通过表单向服务器提交数据时，一个经典的问题就是中文乱码问题。虽然我们所有的 JSP 文件和页面编码格式都采用 UTF-8，但这个问题还是会出现。解决的办法很简单，我们只需要在 web.xml 中配置一个 Spring 的编码转换过滤器就可以了：</p>
<p style="line-height: normal">&lt;web-app&gt;<br style="line-height: normal" />
&lt;!---listener的配置--&gt;<br style="line-height: normal" />
&lt;filter&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;filter-class&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; org.springframework.web.filter.CharacterEncodingFilter ① Spring 编辑过滤器<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/filter-class&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;init-param&gt; ② 编码方式<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-name&gt;encoding&lt;/param-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-value&gt;UTF-8&lt;/param-value&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/init-param&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;init-param&gt; ③ 强制进行编码转换<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-name&gt;forceEncoding&lt;/param-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-value&gt;true&lt;/param-value&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/init-param&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/filter&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;filter-mapping&gt; ② 过滤器的匹配 URL<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/filter-mapping&gt;</p>
<p style="line-height: normal">&lt;!---servlet的配置--&gt;<br style="line-height: normal" />
&lt;/web-app&gt;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
这样所有以 .html 为后缀的 URL 请求的数据都会被转码为 UTF-8 编码格式，表单中文乱码的问题就可以解决了。</p>
<p style="line-height: normal">请求跟踪日志过滤器</p>
<p style="line-height: normal">除了以上两个常用的过滤器外，还有两个在程序调试时可能会用到的请求日志跟踪过滤器，它们会将请求的一些重要信息记录到日志中，方便程序的调试。这两个日志过滤器只有在日志级别为 DEBUG 时才会起作用：</p>
<p style="line-height: normal">方法 说明<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
org.springframework.web.filter.ServletContextRequestLoggingFilter 该过滤器将请求的 URI 记录到 Common 日志中（如通过 Log4J 指定的日志文件）；<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
org.springframework.web.filter.ServletContextRequestLoggingFilter 该过滤器将请求的 URI 记录到 ServletContext 日志中。</p>
<p style="line-height: normal">以下是日志过滤器记录的请求跟踪日志的片断：</p>
<p style="line-height: normal">(JspServlet.java:224) -&nbsp;&nbsp;&nbsp;&nbsp; JspEngine --&gt; /htmlTest.jsp<br style="line-height: normal" />
(JspServlet.java:225) - &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ServletPath: /htmlTest.jsp<br style="line-height: normal" />
(JspServlet.java:226) - &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PathInfo: null<br style="line-height: normal" />
(JspServlet.java:227) - &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RealPath: D:\masterSpring\chapter23\webapp\htmlTest.jsp<br style="line-height: normal" />
(JspServlet.java:228) - &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RequestURI: /baobaotao/htmlTest.jsp<br style="line-height: normal" />
&#8230;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
通过这个请求跟踪日志，程度调试者可以详细地查看到有哪些请求被调用，请求的参数是什么，请求是否正确返回等信息。虽然这两个请求跟踪日志过滤器一般在程序调试时使用，但是即使程序部署不将其从 web.xml 中移除也不会有大碍，因为只要将日志级别设置为 DEBUG 以上级别，它们就不会输出请求跟踪日志信息了。</p>
<p style="line-height: normal">转存 Web 应用根目录监听器和 Log4J 监听器</p>
<p style="line-height: normal">Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器，正确地使用它们可以完成一些特定需求的功能。比如某些第三方工具支持通过 ${key} 的方式引用系统参数（即可以通过 System.getProperty() 获取的属性），WebAppRootListener 可以将 Web 应用根目录添加到系统参数中，对应的属性名可以通过名为&#8220;webAppRootKey&#8221;的 Servlet 上下文参数指定，默认为&#8220;webapp.root&#8221;。下面是该监听器的具体的配置：</p>
<p style="line-height: normal"><br style="line-height: normal" />
清单 6. WebAppRootListener 监听器配置<br style="line-height: normal" />
&#8230;<br style="line-height: normal" />
&lt;context-param&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;param-name&gt;webAppRootKey&lt;/param-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;param-value&gt;baobaotao.root&lt;/param-value&gt; ① Web 应用根目录以该属性名添加到系统参数中<br style="line-height: normal" />
&lt;/context-param&gt;<br style="line-height: normal" />
&#8230;<br style="line-height: normal" />
② 负责将 Web 应用根目录以 webAppRootKey 上下文参数指定的属性名添加到系统参数中<br style="line-height: normal" />
&lt;listener&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;listener-class&gt;<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; org.springframework.web.util.WebAppRootListener<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/listener-class&gt;<br style="line-height: normal" />
&lt;/listener&gt;<br style="line-height: normal" />
&#8230;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
这样，您就可以在程序中通过 System.getProperty("baobaotao.root") 获取 Web 应用的根目录了。不过更常见的使用场景是在第三方工具的配置文件中通过${baobaotao.root} 引用 Web 应用的根目录。比如以下的 log4j.properties 配置文件就通过 ${baobaotao.root} 设置了日志文件的地址：</p>
<p style="line-height: normal">log4j.rootLogger=INFO,R<br style="line-height: normal" />
log4j.appender.R=org.apache.log4j.RollingFileAppender<br style="line-height: normal" />
log4j.appender.R.File=${baobaotao.root}/WEB-INF/logs/log4j.log ① 指定日志文件的地址<br style="line-height: normal" />
log4j.appender.R.MaxFileSize=100KB<br style="line-height: normal" />
log4j.appender.R.MaxBackupIndex=1<br style="line-height: normal" />
log4j.appender.R.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
另一个专门用于 Log4J 的监听器是 Log4jConfigListener。一般情况下，您必须将 Log4J 日志配置文件以 log4j.properties 为文件名并保存在类路径下。Log4jConfigListener 允许您通过 log4jConfigLocation Servlet 上下文参数显式指定 Log4J 配置文件的地址，如下所示：</p>
<p style="line-height: normal">① 指定 Log4J 配置文件的地址<br style="line-height: normal" />
&lt;context-param&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;param-value&gt;/WEB-INF/log4j.properties&lt;/param-value&gt;<br style="line-height: normal" />
&lt;/context-param&gt;<br style="line-height: normal" />
&#8230;<br style="line-height: normal" />
② 使用该监听器初始化 Log4J 日志引擎<br style="line-height: normal" />
&lt;listener&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;listener-class&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; org.springframework.web.util.Log4jConfigListener<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/listener-class&gt;<br style="line-height: normal" />
&lt;/listener&gt;<br style="line-height: normal" />
&#8230;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal">提示</p>
<p style="line-height: normal">一些Web应用服务器（如 Tomcat）不会为不同的Web应用使用独立的系统参数，也就是说，应用服务器上所有的 Web 应用都共享同一个系统参数对象。这时，您必须通过webAppRootKey 上下文参数为不同Web应用指定不同的属性名：如第一个 Web 应用使用 webapp1.root 而第二个 Web 应用使用 webapp2.root 等，这样才不会发生后者覆盖前者的问题。此外，WebAppRootListener 和 Log4jConfigListener 都只能应用在 Web 应用部署后 WAR 文件会解包的 Web 应用服务器上。一些 Web 应用服务器不会将Web 应用的 WAR 文件解包，整个 Web 应用以一个 WAR 包的方式存在（如 Weblogic），此时因为无法指定对应文件系统的 Web 应用根目录，使用这两个监听器将会发生问题。<br style="line-height: normal" />
<br style="line-height: normal" />
<br style="line-height: normal" />
Log4jConfigListener 监听器包括了 WebAppRootListener 的功能，也就是说，Log4jConfigListener 会自动完成将 Web 应用根目录以 webAppRootKey 上下文参数指定的属性名添加到系统参数中，所以当您使用 Log4jConfigListener 后，就没有必须再使用 WebAppRootListener了。</p>
<p style="line-height: normal">Introspector 缓存清除监听器</p>
<p style="line-height: normal">Spring 还提供了一个名为 org.springframework.web.util.IntrospectorCleanupListener 的监听器。它主要负责处理由 JavaBean Introspector 功能而引起的缓存泄露。IntrospectorCleanupListener 监听器在 Web 应用关闭的时会负责清除 JavaBean Introspector 的缓存，在 web.xml 中注册这个监听器可以保证在 Web 应用关闭的时候释放与其相关的 ClassLoader 的缓存和类引用。如果您使用了 JavaBean Introspector 分析应用中的类，Introspector 缓存会保留这些类的引用，结果在应用关闭的时候，这些类以及Web 应用相关的 ClassLoader 不能被垃圾回收。不幸的是，清除 Introspector 的唯一方式是刷新整个缓存，这是因为没法准确判断哪些是属于本 Web 应用的引用对象，哪些是属于其它 Web 应用的引用对象。所以删除被缓存的 Introspection 会导致将整个 JVM 所有应用的 Introspection 都删掉。需要注意的是，Spring 托管的 Bean 不需要使用这个监听器，因为 Spring 的 Introspection 所使用的缓存在分析完一个类之后会马上从 javaBean Introspector 缓存中清除掉，并将缓存保存在应用程序特定的 ClassLoader 中，所以它们一般不会导致内存资源泄露。但是一些类库和框架往往会产生这个问题。例如 Struts 和 Quartz 的 Introspector 的内存泄漏会导致整个的 Web 应用的 ClassLoader 不能进行垃圾回收。在 Web 应用关闭之后，您还会看到此应用的所有静态类引用，这个错误当然不是由这个类自身引起的。解决这个问题的方法很简单，您仅需在 web.xml 中配置 IntrospectorCleanupListener 监听器就可以了：</p>
<p style="line-height: normal">&lt;listener&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;listener-class&gt;<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; org.springframework.web.util.IntrospectorCleanupListener<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp; &lt;/listener-class&gt;<br style="line-height: normal" />
&lt;/listener&gt;</p>
<p style="line-height: normal"></p>
<p style="line-height: normal"></p>
<p style="line-height: normal"></p>
<p style="line-height: normal"><br style="line-height: normal" />
回页首<span class="Apple-converted-space">&nbsp;</span><br style="line-height: normal" />
</p>
<p style="line-height: normal"></p>
<p style="line-height: normal">小结</p>
<p style="line-height: normal">本文介绍了一些常用的 Spring 工具类，其中大部分 Spring 工具类不但可以在基于 Spring 的应用中使用，还可以在其它的应用中使用。使用 JDK 的文件操作类在访问类路径相关、Web 上下文相关的文件资源时，往往显得拖泥带水、拐弯抹角，Spring 的 Resource 实现类使这些工作变得轻松了许多。</p>
<p style="line-height: normal">在 Web 应用中，有时你希望直接访问 Spring 容器，获取容器中的 Bean，这时使用 WebApplicationContextUtils 工具类从 ServletContext 中获取 WebApplicationContext 是非常方便的。WebUtils 为访问 Servlet API 提供了一套便捷的代理方法，您可以通过 WebUtils 更好的访问 HttpSession 或 ServletContext 的信息。</p>
<p style="line-height: normal">Spring 提供了几个 Servlet 过滤器和监听器，其中 ServletContextRequestLoggingFilter 和 ServletContextRequestLoggingFilter 可以记录请求访问的跟踪日志，你可以在程序调试时使用它们获取请求调用的详细信息。WebAppRootListener 可以将 Web 应用的根目录以特定属性名添加到系统参数中，以便第三方工具类通过 ${key} 的方式进行访问。Log4jConfigListener 允许你指定 Log4J 日志配置文件的地址，且可以在配置文件中通过 ${key} 的方式引用 Web 应用根目录，如果你需要在 Web 应用相关的目录创建日志文件，使用 Log4jConfigListener 可以很容易地达到这一目标。</p>
<p style="line-height: normal">Web 应用的内存泄漏是最让开发者头疼的问题，虽然不正确的程序编写可能是这一问题的根源，也有可能是一些第三方框架的 JavaBean Introspector 缓存得不到清除而导致的，Spring 专门为解决这一问题配备了 IntrospectorCleanupListener 监听器，它只要简单在 web.xml 中声明该监听器就可以了。</p>
</span></span>
<img src ="http://www.blogjava.net/alancxx/aggbug/349896.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-05-10 10:02 <a href="http://www.blogjava.net/alancxx/articles/349896.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate Restrictions用法 MatchMode.ANYWHERE</title><link>http://www.blogjava.net/alancxx/articles/349490.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Wed, 04 May 2011 06:09:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/349490.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/349490.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/349490.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/349490.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/349490.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 14px; color: rgb(117,117,117); line-height: 21px; font-family: Arial; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">
<table class="FCK__ShowTableBorders" dir="ltr" style="table-layout: auto; line-height: normal; height: 319px" cellspacing="0" cellpadding="0" width="499" border="0">
    <tbody style="line-height: normal">
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">方法</font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">说明</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.eq</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">＝</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.allEq</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">利用Map来进行多个等于的限制</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.gt</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">＞</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.ge</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">＞＝</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.<span style="font-size: 14px! important; line-height: normal">lt</span></font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">＜</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.le</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">＜＝</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.between</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">BETWEEN</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">LIKE</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.in</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">in</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.and</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">and</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="24">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">or</font></p>
            </td>
        </tr>
        <tr style="line-height: normal">
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="220" height="47">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.sqlRestriction</font></font></p>
            </td>
            <td style="font-size: 14px! important; filter: none; visibility: visible! important; word-break: break-all; line-height: normal; zoom: 1! important; font-family: Arial; word-wrap: break-word" width="281" height="47">
            <p style="font-size: 14px! important; text-indent: 2em; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">用SQL限定查询</font></p>
            </td>
        </tr>
    </tbody>
</table>
<br style="line-height: normal" />
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4"></font></p>
<p style="font-size: 14px! important; line-height: normal"><strong style="line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><em style="color: red; line-height: normal">QBC常用限定方法</em></font></font></strong></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.eq</span><span class="Apple-converted-space">&nbsp;</span>--&gt; equal,等于.</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.allEq</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 参数为Map对象,使用key/value进行多个等于的比对,相当于多个<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.eq 的效果</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.gt</span><span class="Apple-converted-space">&nbsp;</span>--&gt; great-than &gt; 大于</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.ge</span><span class="Apple-converted-space">&nbsp;</span>--&gt; great-equal &gt;= 大于等于</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.<span style="font-size: 14px! important; line-height: normal">lt</span></span><span class="Apple-converted-space">&nbsp;</span>--&gt; less-than, &lt; 小于</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.le</span><span class="Apple-converted-space">&nbsp;</span>--&gt; less-equal &lt;= 小于等于</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.between</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 对应SQL的between子句</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 对应SQL的LIKE子句</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.in</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 对应SQL的in子句</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.and</span><span class="Apple-converted-space">&nbsp;</span>--&gt; and 关系</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or</span><span class="Apple-converted-space">&nbsp;</span>--&gt; or 关系</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.isNull</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 判断属性是否为空,为空则返回true</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.isNotNull</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 与isNull相反</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal"><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.sqlRestriction</span><span class="Apple-converted-space">&nbsp;</span>--&gt; SQL限定的查询</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal">Order.asc</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 根据传入的字段进行升序排序</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal">Order.desc</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 根据传入的字段进行降序排序</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal">MatchMode.EXACT</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 字符串精确匹配.相当于"like 'value'"</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal">MatchMode.ANYWHERE</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 字符串在中间匹配.相当于"like '%value%'"</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal">MatchMode.START</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 字符串在最前面的位置.相当于"like 'value%'"</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312"><font style="line-height: normal" size="4"><span style="font-size: 14px! important; color: red; line-height: normal">MatchMode.END</span><span class="Apple-converted-space">&nbsp;</span>--&gt; 字符串在最后面的位置.相当于"like '%value'"</font></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">例子<br style="line-height: normal" />
查询年龄在20-30岁之间的所有学生对象<br style="line-height: normal" />
List list = session.createCriteria(Student.class)<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.between("age",new Integer(20),new Integer(30)).list();<br style="line-height: normal" />
查询学生姓名在AAA,BBB,CCC之间的学生对象<br style="line-height: normal" />
String[] names = {"AAA","BBB","CCC"};<br style="line-height: normal" />
List list = session.createCriteria(Student.class)<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.in("name",names)).list();<br style="line-height: normal" />
查询年龄为空的学生对象<br style="line-height: normal" />
List list = session.createCriteria(Student.class)<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.isNull("age")).list();<br style="line-height: normal" />
查询年龄等于20或者年龄为空的学生对象<br style="line-height: normal" />
List list = session.createCriteria(Student.class)<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .add(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.eq("age",new Integer(20)),<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.isNull("age")).list();</font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">--------------------------------------------------------------------<br style="line-height: normal" />
<span style="font-size: 14px! important; color: red; line-height: normal"><strong style="line-height: normal">使用QBC实现动态查询</strong><span class="Apple-converted-space">&nbsp;</span></span><br style="line-height: normal" />
public List findStudents(String name,int age){</font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">Criteria criteria = session.createCriteria(Student.class);<br style="line-height: normal" />
if(name != null){<br style="line-height: normal" />
&nbsp;&nbsp; criteria.add(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.liek("name",name,MatchMode.ANYWHERE));<br style="line-height: normal" />
}<br style="line-height: normal" />
if(age != 0){<br style="line-height: normal" />
&nbsp;&nbsp; criteria.add(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.eq("age",new Integer(age)));<br style="line-height: normal" />
}<br style="line-height: normal" />
criteria.addOrder(Order.asc("name"));//根据名字升序排列<br style="line-height: normal" />
return criteria.list();<br style="line-height: normal" />
}</font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4"></font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">-----------------------------------------------------------------------------------</font></p>
<p style="font-size: 14px! important; line-height: normal"><font style="line-height: normal" face="楷体_GB2312" size="4">今天用了写<span style="font-size: 14px! important; line-height: normal">hibernate</span>高级查询时用了<span style="font-size: 14px! important; line-height: normal">Restrictions</span>(当然Expr<wbr style="line-height: normal">ession也是可以以的)这个类.感觉不错.<br style="line-height: normal" />
下面的代码写的不易读.其实核心就是一句<br style="line-height: normal" />
<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like(),<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like,........))<br style="line-height: normal" />
里面的or可以无限加的.还是比较好用<br style="line-height: normal" />
<br style="line-height: normal" />
Session session = getHibernateTemplate().getSessionFactory()<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .openSession();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Criteria criteria = session.createCriteria(Film.class);<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;Film&gt; list = criteria.add(<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like("description", key,MatchMode.ANYWHERE),<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like("name", key,MatchMode.ANYWHERE),<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like("direct", key,MatchMode.ANYWHERE),<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.or(<span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like("mainplay",key,MatchMode.ANYWHERE),<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span><span style="font-size: 14px! important; line-height: normal">Restrictions</span>.like("filearea", key,MatchMode.ANYWHERE)))))).list();<br style="line-height: normal" />
<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.close();<br style="line-height: normal" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return list;</font></p>
</span></span>
<img src ="http://www.blogjava.net/alancxx/aggbug/349490.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-05-04 14:09 <a href="http://www.blogjava.net/alancxx/articles/349490.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Fedora Core 4下的GPG加密解密及文件完整性验证</title><link>http://www.blogjava.net/alancxx/articles/348896.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Sat, 23 Apr 2011 15:02:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/348896.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/348896.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/348896.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/348896.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/348896.html</trackback:ping><description><![CDATA[<table style="table-layout: fixed">
        <tr>
            <td>
            <div class="cnt" id="blog_text">准备知识:<br />
            对称加密:<br />
            发送方: 010111 XOR 101100 = 111011&nbsp;&nbsp; (101100为加密密钥)<br />
            接收方: 111011 XOR 101100 = 010111<br />
            公钥加密:<br />
            发送方产生两把密钥:公钥(Public Key)&amp;私钥(Private Key),也就是密钥对(Key Pair)<br />
            明文-&gt;公钥加密-&gt;密文-&gt;私钥解密-&gt;明文<br />
            数字签名:<br />
            ....<br />
            <br />
            <br />
            -----------------------------发送方的操作-------------------------------------<br />
            sh# mkdir ~/.gnupg&nbsp;&nbsp; //默认是把密钥对存在此目录<br />
            sh# gpg --gen-key //生成公钥pubring.gpg,私钥secring.gpg,邮箱是很重要的<br />
            sh# gpg --list-keys //查看所有密钥,如果生成了多个密钥对的话就有多个了<br />
            sh# gpg --list-public-key //查看所有公钥,注意pub后面的*号字符串,pub xxxxx/******** 2007-07-10,它就是以下可能用到的KEYID<br />
            sh# gpg --list-secret-key //查看所有密钥<br />
            sh# gpg --list-sig //查看所有签名<br />
            sh# gpg --export-secret-keys -a -o secring.asc //导出私钥,作为备份<br />
            sh# gpg -a --export zjstandup@126.com &gt;test.gpg //导出公钥,邮箱名是第一步填的,对应于zjstandup@126.com的公匙信息就保存到了test.gpg文件中,此文件可以由邮箱发送或上传至公钥服务器供别人下载(如subkeys.pgp.net),有点像BT种子,哈哈<br />
            <br />
            /**此处将演示怎么把公钥上传至公钥服务器上,可以直接去网站上传,也可以像以下这样用命令上传<br />
            sh# gpg --send-keys KEYID //KEYID用以上那个代替,默认好像是传到了subkeys.pgp.net,文件名就不是test.gpg了,这些公钥服务器好像是全球同步更新!厉害阿!<br />
            <br />
            **/<br />
            <br />
            <br />
            sh# gedit test.txt //生成一个文本,里面输入点东西,用于以下测试<br />
            <br />
            将演示两种验证方式和一个数字签名<br />
            一种是公钥验证,大概是用于验证文件完整性,相当于:sh#md5sum ***;<br />
            另一种是私钥验证,控制只让那些知道密码的人能打开看<br />
            <br />
            sh# gpg --clearsign test.txt //使用私匙对数据进行签名,生成test.txt.asc,打开此文件还是可以看到内容的,只是多了一些东西,把这个发给其他人,这个演示公钥验证<br />
            sh# gpg --output test.txt.gpg -r zjstandup@126.com -as --encrypt test.txt //导入私钥,数据被加密成了test.txt.gpg,打开此文件看不到原信息的,把这个发给对方,这个演示私钥验证<br />
            sh# gpg --output test.sig --sign test.txt //这个演示数字签名<br />
            <br />
            <br />
            <br />
            ---------------------------接收方的操作------------------------------------------<br />
            /**下载公钥,由邮件接收或者从服务器上下载,此处演示从服务器下载<br />
            sh# gpg --search-keys zjstandup@126.com //搜索公钥,启动了全球搜索?!然后填入1就下载!<br />
            <br />
            **/<br />
            sh# gpg --import test.gpg //导入公钥至本机,test.gpg由发送方邮件传输过来或从公钥服务器下载过来的<br />
            <br />
            /**以下一段不太清楚,好像是防止公钥被篡改,验证公钥的完整性<br />
            sh# gpg --fingerprint zjstandup@126.com //指纹取样,防伪造的公钥,执行后可以看到密钥指纹,可以通过其他通讯方式咨询对方,是否是这个公钥(应该是上方让大家注意的pub后面的字符串)<br />
            sh# gpg --sign-key zjstandup@126.com<br />
            sh# gpg --check-sigs zjstandup@126.com<br />
            sh# gpg --edit-key zjstandup@126.com<br />
            sh# trust<br />
            sh# quit<br />
            **/<br />
            <br />
            1:公钥验证<br />
            sh# gpg --verify test.txt.asc //将有信息表明是完整的签字,假如改变了这个文件里面的信息再执行此命令,将出现不同的结果哦! <br />
            <br />
            2:私钥验证<br />
            sh# gpg --decrypt test.txt.gpg &gt; zjstandup.txt //执行,要输入正确的密码(在发送端生成密钥对时的那个)才能生成txt<br />
            <br />
            3;数字签名<br />
            sh# gpg --output sig.txt --decrypt test.sig //将显示完整的签字并还原内容生成sig.txt<br />
            <br />
            <br />
            sh# gpg --help &gt;&gt; manule.txt<br />
            sh# gedit manule.txt<br />
            <br />
            //将看到以下说明<br />
            gpg (GnuPG) 1.4.1<br />
            Copyright (C) 2005 Free Software Foundation, Inc.<br />
            This program comes with ABSOLUTELY NO WARRANTY.<br />
            This is free software, and you are welcome to redistribute it<br />
            under certain conditions. See the file COPYING for details.<br />
            <br />
            Home: ~/.gnupg<br />
            支持的算法：<br />
            公钥：RSA, RSA-E, RSA-S, ELG-E, DSA<br />
            对称加密：3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH<br />
            散列：MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512<br />
            压缩：不压缩, ZIP, ZLIB, BZIP2<br />
            <br />
            语法：gpg [选项] [文件名]<br />
            签字、检查、加密或解密<br />
            默认的操作依输入数据而定<br />
            <br />
            指令：<br />
            <br />
            -s, --sign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --clearsign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份明文签字<br />
            -b, --detach-sign&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份分离的签字<br />
            -e, --encrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 加密数据<br />
            -c, --symmetric&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 仅使用对称加密<br />
            -d, --decrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 解密数据(默认)<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --verify&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 验证签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --list-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --list-sigs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出密钥和签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --check-sigs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出并检查密钥签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --fingerprint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出密钥和指纹<br />
            -K, --list-secret-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出私钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --gen-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一副新的密钥对<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --delete-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从公钥钥匙环里删除密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --delete-secret-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从私钥钥匙环里删除密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --sign-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为某把密钥添加签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --lsign-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为某把密钥添加本地签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --edit-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 编辑某把密钥或为其添加签字<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --gen-revoke&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份吊销证书<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --export&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 导出密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --send-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 把密钥导出到某个公钥服务器上<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --recv-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从公钥服务器上导入密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --search-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在公钥服务器上搜寻密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --refresh-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从公钥服务器更新所有的本地密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --import&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 导入/合并密钥<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --card-status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 打印卡状态<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --card-edit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更改卡上的数据<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --change-pin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更改卡的 PIN<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --update-trustdb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更新信任度数据库<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --print-md 算法 [文件]&nbsp;&nbsp;&nbsp; 使用指定的散列算法打印报文散列值<br />
            <br />
            选项：<br />
            <br />
            -a, --armor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出经 ASCII 封装<br />
            -r, --recipient 某甲&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为收件者&#8220;某甲&#8221;加密<br />
            -u, --local-user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用这个用户标识来签字或解密<br />
            -z N&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设定压缩等级为 N (0 表示不压缩)<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --textmode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用标准的文本模式<br />
            -o, --output&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 指定输出文件<br />
            -v, --verbose&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 详细模式<br />
            -n, --dry-run&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不做任何改变<br />
            -i, --interactive&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 覆盖前先询问<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --openpgp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 行为严格遵循 OpenPGP 定义<br />
            &nbsp;&nbsp;&nbsp;&nbsp; --pgp2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成与 PGP 2.x 兼容的报文<br />
            <br />
            (请参考在线说明以获得所有命令和选项的完整清单)<br />
            <br />
            范例：<br />
            <br />
            -se -r Bob [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为 Bob 这个收件人签字及加密<br />
            --clearsign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做出明文签字<br />
            --detach-sign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做出分离式签字<br />
            --list-keys [某甲]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示密钥<br />
            --fingerprint [某甲]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示指纹<br />
            <br />
            请向 &lt;gnupg-bugs@gnu.org&gt; 报告程序缺陷。<br />
            请向 &lt;zuxyhere@eastday.com&gt; 反映简体中文翻译的问题。</div>
            </td>
        </tr>
</table><img src ="http://www.blogjava.net/alancxx/aggbug/348896.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-04-23 23:02 <a href="http://www.blogjava.net/alancxx/articles/348896.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GPG(pgp)加解密详述</title><link>http://www.blogjava.net/alancxx/articles/348895.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Sat, 23 Apr 2011 14:55:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/348895.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/348895.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/348895.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/348895.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/348895.html</trackback:ping><description><![CDATA[<h2><span style="color: #3366ff">一、介绍</span></h2>
<p>我们都知道，互联网是不安全的，但其上所使用的大部分应用，如Web、Email等一般都只提供明文传输方式（用https、smtps等例外）。所以，当我们需要传输重要文件时，应该对当中的信息加密。非对称密码系统是其中一种常见的加密手段。而在基于PGP方式加密的中文介绍少之又少，所以萌生了写一个完整教程的想法，当然本文部分资料是我搜遍网络整理出来的，并不能保证百分之百的原创 </p>
<p></p>
<blockquote>
<p>GnuPG 是一个用来进行非对称加密(PGP)的免费软件，简称GPG（是不是有的童鞋已经被PGP和GPG给搞昏了）。先说说什么是非对称加密。传统的加密手段往往是使用同一个密码进行加密和解密。例如你加密时用的密码是&#8220;abc&#8221;， 则解密时也要使用&#8220;abc&#8221;才行。这样就存在一个问题，你不能够把一段加密信息发送给你的朋友。试想，如果采用这种加密方式把信息发送给你的朋友时，你的 朋友必须要知道你的密码才能把你的信息解密出来。但你如何保证你的朋友是绝对可靠的呢？也就是说，如果你的朋友把你的密码告诉了别人，你的密码就不再安全 了。<br />
非对称加密采用的是另一种思想。它会给你产生两个密钥，一个称为&#8220;公钥&#8221;，另一个称为&#8220;私钥&#8221;。公钥是可以公开的，你尽管把它传给别 人；私钥你一定要保管好不让其他任何人知道。当某人得到你的公钥后，他就可以给你发送加密信息了。具体来说，他把他要发给你的信息用你的公钥加密后发给 你，加密的信息只能用你的私钥去解密。这样，因为世界上除了你以外没有别人知道你的私钥，所以即使别人看到发送给你的加密信息他也无法解密，甚至连发送者 本人也不行。因为他不知道你的私钥。简单说来，就是用公钥去加密；用对应的私钥去解密。想给谁发送加密信息，首先要得到他的公钥。<br />
支持非 对称加密的软件有多种，最著名的可能是美国的PGP了，不过它是个商业软件，价格不便宜。对于加密软件，我反对使用破解软件，因为如果信息需要加密的话， 肯定是非常重要的信息，破解软件无法保证加密的安全可靠。因此我建议使用免费开源的GnuPG软件进行信息的加密和解密。</p>
</blockquote>
<h2><span style="color: #3366ff">二、使用:</span></h2>
<p><span style="color: #ff6600"><strong>1.生成密钥对</strong></span><br />
要使用GnuPG加密，首先需要创建密钥对，执行：</p>
<div class="quote">
<blockquote>
<div class="quote-content"># gpg &#8211;gen-key<br />
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.<br />
This program comes with ABSOLUTELY NO WARRANTY.<br />
This is free software, and you are welcome to redistribute it<br />
under certain conditions. See the file COPYING for details.
<p>&nbsp;</p>
<p>请选择您要使用的密钥种类：<br />
(1) DSA 和 ElGamal (默认)<br />
(2) DSA (仅用于签名)<br />
(5) RSA (仅用于签名)<br />
您的选择？ 1&nbsp;&nbsp;<span style="color: #ff0000">&#8592;只有1可以用于加密，其他种类只能用于签名</span><br />
DSA 密钥对会有 1024 位。<br />
ELG-E 密钥长度应在 1024 位与 4096 位之间。<br />
您想要用多大的密钥尺寸？(2048)&nbsp;&nbsp;<span style="color: #ff0000">&#8592;选择密码的位数，位数越大，越安全，但速度越慢</span><br />
您所要求的密钥尺寸是 2048 位<br />
请设定这把密钥的有效期限。<br />
0 = 密钥永不过期<br />
&lt;n&gt;&nbsp;&nbsp;= 密钥在 n 天后过期<br />
&lt;n&gt;w = 密钥在 n 周后过期<br />
&lt;n&gt;m = 密钥在 n 月后过期<br />
&lt;n&gt;y = 密钥在 n 年后过期<br />
密钥的有效期限是？(0) 0&nbsp;&nbsp;<span style="color: #ff0000">&#8592;根据实际情况选择密钥期限</span><br />
密钥永远不会过期<br />
以上正确吗？(y/n)y&nbsp;&nbsp;<span style="color: #ff0000">&#8592;确认</span></p>
<p>您需要一个用户标识来辨识您的密钥；本软件会用真实姓名、注释和电子邮件地址组合<br />
成用户标识，如下所示：<br />
&#8220;Heinrich Heine (Der Dichter) &lt;heinrichh@duesseldorf.de&gt;&#8221;</p>
<p>真实姓名：Hyphen Wang&nbsp;&nbsp;<span style="color: #ff0000">&#8592;请填入真实姓名，后面会用到</span><br />
电子邮件地址：gpgencrypt@linuxfly.org&nbsp;&nbsp;<span style="color: #ff0000">&#8592;邮件作为标记之一，不能重复</span><br />
注释：Use for GPG Encrypt&nbsp;&nbsp;<span style="color: #ff0000">&#8592;仅是注释而已</span><br />
您选定了这个用户标识：<br />
&#8220;Hyphen Wang (Use for GPG Encrypt) &lt;gpgencrypt@linuxfly.org&gt;&#8221;</p>
<p>更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)？O&nbsp;&nbsp;<span style="color: #ff0000">&#8592;输入&#8220;O&#8221;确认</span><br />
您需要一个密码来保护您的私钥。&nbsp;&nbsp;<span style="color: #ff0000">&#8592;输入两次用于访问私钥的密码，紧记，不能公开或丢失</span></p>
<p>我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动<br />
鼠标、读写硬盘之类的)，这会让随机数字发生器有更好的机会获得足够的熵数。<br />
++++++++++&#8230;++++++++++..++++++++</p>
<p>随机字节不够多。请再做一些其他的琐事，以使操作系统能搜集到更多的熵数！<br />
(还需要274字节)&nbsp;&nbsp;<span style="color: #ff0000">&#8592;运行一些的程序，以便在内存中获得更多随机数</span><br />
我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动<br />
鼠标、读写硬盘之类的)，这会让随机数字发生器有更好的机会获得足够的熵数。<br />
+++++++++++++++++++++++++.+++++.+++++.++++++++++.+++&lt;+++++..+++++^^^<br />
gpg: 密钥 <strong><span style="color: #dc143c">A3942296</span></strong> 被标记为绝对信任&nbsp;&nbsp;<span style="color: #ff0000">&#8592;密钥ID</span><br />
公钥和私钥已经生成并经签名。</p>
<p>gpg: 正在检查信任度数据库<br />
gpg: 需要 3 份勉强信任和 1 份完全信任，PGP 信任模型<br />
gpg: 深度：0 有效性：&nbsp;&nbsp;2 已签名：&nbsp;&nbsp;0 信任度：0-，0q，0n，0m，0f，2u<br />
pub&nbsp;&nbsp; 1024D/A3942296 2008-12-19<br />
密钥指纹 = E95E 1F77 6C4E 33BD 740C&nbsp;&nbsp;19AB EEF9 A67E A394 2296<br />
uid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hyphen Wang (Use for GPG Encrypt) &lt;gpgencrypt@linuxfly.org&gt;<br />
sub&nbsp;&nbsp; 2048g/911E677B 2008-12-19</p>
</div>
</blockquote>
<div class="quote-content"><span style="color: #ff6600"><strong>2.密钥的回收</strong></span></div>
<div class="quote-content">当您的密钥对生成之后，您应该立即做一个公钥回收证书，如果您忘记了您的私钥的口令或者您的私钥丢失或者被盗窃，您可以发布这个证书来声明以前的公钥不再有效。生成回收证书的选项是&#8221;&#8211;gen-revoke&#8221;。</div>
<div class="quote-content">
<blockquote>
<p>gpg &#8211;output revoke.asc &#8211;gen-revoke <span style="color: #3366ff">mykeyID</span></p>
</blockquote>
<p>其中mykey 参数是可以表示的密钥标识，产生的回收证书放在revoke.asc文件里，一旦回收证书被发放，以前的证书就不能再被其他用户访问，因此以前的公钥也就失效了。</p>
<p><span style="color: #ff00ff">PS:如果一旦决定撤销已经上传的公钥，就需要将该密钥的回收证书上传至密钥服务器完成回收工作。</span></p>
<p><strong></strong></p>
<blockquote>
<p>gpg &#8211;keyserver <span style="color: #3366ff">Server Address</span> &#8211;send-keys <span style="color: #3366ff">mykeyID</span></p>
</blockquote>
<p><strong><span style="color: #ff6600">3.密钥的上传</span></strong></p>
<p>当上述工作完成以后，为了让尽可能多的人获取您的公钥，您可以将公钥邮寄出去，或者贴在自己的个人主页上，当然还有一种更好的方法就是上传到全球性的密钥服务器，其他用户可以通过您提供的公钥ID来搜索并获得您的公钥。</p>
<p>通过如下命令可以将你的key发布到服务器上：</p>
</div>
<blockquote>
<div class="quote-content">gpg &#8211;keyserver <span style="color: #3366ff">Server Address</span> &#8211;send-keys <span style="color: #3366ff">mykeyID</span></div>
</blockquote>
<div><span style="color: #ff00ff">PS:当然您也可以定义默认的服务器key server，一般安装好后的默认key server都是subkeys.pgp.net。你也可以通过修改.gnupg/gpg.conf中的keyserver信息来改变你的key server。</span></div>
</div>
<div><strong><span style="color: #ff6600">4.密钥的导出／导入</span></strong></div>
<div><strong><span style="color: #ff6600"><br />
</span></strong></div>
<div>我们通常需要导出公钥和私钥保存起来，当然公钥是可以满世界的泼洒，但是私钥请务必保存好，否则你的密钥对将会永久性的失去威力。</div>
<ul>
    <li>公钥的导出： </li>
</ul>
<div>
<blockquote>
<p>gpg -o <span style="color: #3366ff">keyfilename</span> &#8211;export <span style="color: #3366ff">mykeyID</span></p>
<p>如果没有mykeyID则是备份所有的公钥，<span style="color: #ff0000">-o</span>表示输出到文件keyfilename中，如果加上<span style="color: #ff0000">-a</span>的参数则输出文本格式( ASCII )的信息，否则输出的是二进制格式信息。</p>
</blockquote>
<ul>
    <li>私钥的导出： </li>
</ul>
<blockquote>
<p>gpg -o <span style="color: #3366ff">keyfilename </span>&#8211;export-secret-keys <span style="color: #3366ff">mykeyID</span></p>
<p>如果没有mykeyID则是备份所有的私钥，<span style="color: #ff0000">-o</span>表示输出到文件keyfilename中，如果加上<span style="color: #ff0000">-a</span>的参数则输出文本格式的信息，否则输出的是二进制格式信息。</p>
</blockquote>
<ul>
    <li>密钥的导入： </li>
</ul>
<blockquote>
<p>gpg &#8211;import <span style="color: #3366ff">filename</span></p>
</blockquote>
<p><span style="color: #ff00ff">PS:用户可以使用gpg &#8211;list-keys命令查看是否成功导入了密钥。</span><br />
<strong></strong></p>
<p><strong><span style="color: #ff6600">5.加密解密和数字签名</span></strong></p>
<p>通过上述的密钥生成以及公钥分发后，加密和解密数据变得非常容易，用户可以通过使用该功能来达到安全地在网络上传输自己的隐密数据的目的。</p>
<p>如果用户patterson要给用户liyang发送一个加密文件，则他可以使用liyang的公钥加密这个文件，并且这个文件也只有liyang使用自己的密钥才可以解密查看。下面给出加解密的步骤：</p>
<ul>
    <li>用户patterson使用liyang的公钥加密文件test，使用下面的指令： </li>
</ul>
<blockquote>
<p># gpg -e test</p>
<p>You did not specify a user ID. (you may use &#8220;-r&#8221;)</p>
<p>Enter the user ID. End with an empty line: liyang</p>
<p>Added 1024g/C50E455A 2006-01-02 &#8220;liyang (hello) &lt; liyang@sina.com&gt;&#8221;</p>
</blockquote>
<p>这样，就可以将gpg.conf文件加密成test.gpg，一般用户是无法阅读的</p>
<p><span style="color: #ff00ff">PS:当然你也可以直接指定使用哪个用户的公钥进行加密:</span></p>
<blockquote>
<p>gpg -e -r liyang test&nbsp; (-r 表示指定用户)</p>
<p>还可以加上参数 -a 来输出ASCII编码的文件test.asc(test.gpg是二进制编码的，不可用文本读)</p>
<p>gpg -ea -r liyang test</p>
</blockquote>
<ul>
    <li>用户liyang 使用自己的私钥来解密该文件，如下所示： </li>
</ul>
<blockquote>
<p># gpg -d test.gpg</p>
<p>You need a passphrase to unlock the secret key for</p>
<p>user: &#8220;liyang (hello) &lt; liyang@sina.com&gt;&#8221;</p>
<p>1024-bit ELG-E key, ID C50E455A, created 2006-01-02 (main key ID 378D11AF)</p>
<p>GnuPG提示用户，需要输入生成私钥使用的密码：</p>
<p>Enter passphrase:</p>
<p>gpg: encrypted with 1024-bit ELG-E key, ID C50E455A, created 2006-01-02</p>
<p>&#8220;liyang (hello) &lt; liyang@sina.com&gt;&#8221;</p>
<p>PS:无论加密解密，都可以加上<span style="color: #ff0000">-o</span>参数来指定加密和解密后的输出文件，例如</p>
<p><span style="color: #888888">#gpg <span style="color: #ff0000">-o </span>doc.gpg -er name doc<br />
其中name是选择谁的公钥加密，即谁是文件的接收者。<br />
doc为要加密的文件，即原文件<br />
doc.gpg为命令执行后生成的加密的文件，这里要先指定好文件名</span></p>
</blockquote>
<ul>
    <li>对文件进行签名 </li>
</ul>
<blockquote>
<p><span style="color: #00ccff">1、数字签名</span><br />
命令格式：<br />
#gpg -o doc.sig -s doc<br />
其中doc是原文件，doc.sig包含了原文件和签名，是二进制的。这个命令会要求你输入你的私钥的密码句。<br />
#gpg -o doc.sig -ser name doc<br />
既签名又加密</p>
<p><span style="color: #00ccff">2、文本签名</span><br />
#gpg -o doc.sig &#8211;clearsign doc<br />
这样产生的doc.sig同样包含原文件和签名，其中签名是文本的，而原文件不变。</p>
<p><span style="color: #00ccff">3、分离式签名</span><br />
#gpg -o doc.sig -ab doc<br />
doc.sig仅包括签名，分离式签名的意思是原文件和签名是分开的。<br />
b 表示分离式签名detach-sign</p>
<p><span style="color: #00ccff">4、验证签名</span><br />
#gpg &#8211;verify doc.sig [doc]<br />
验证之前必须导入文件作者的公钥，对于分离式签名，最后还要加上原文件，即后面的doc。</p>
</blockquote>
<ul>
    <li>密匙签名和用户信任(进阶功能) </li>
</ul>
<p>尽管在理论上讲，具备了公匙和私匙就可以实现安全的信息通讯，但是在实际应用中，还必须对公匙进行有效确认。因为，确实存在伪造公匙信息的可能。</p>
<p>由此，在GPG中引入了一个复杂的信任系统，以帮助我们区分哪些密匙是真的，哪些密匙是假的。这个信任系统是基于密匙的，主要包括密匙签名。</p>
<p>当收到熟人的公匙并且GPG告知不存在任何实体可信信息附加于这个公匙后，首要的事情就是对这个密匙进行&#8220;指纹采样&#8221;（fingerprint）。例如，我们对来自mike的公匙进行了导入操作，并且GPG告知我们不存在这个密匙的附加可信信息，这时候，我们首先要做的工作就是对这个新密匙进行&#8220;指纹采样 &#8221;，相关命令及执行情况如下：</p>
<blockquote>
<p>$ gpg &#8211;fingerprint mike@hairnet.orgpub 1024D/4F03BD39 2001-01-15 Mike Socks (I&#8217;m WIRED) Key fingerprint = B121 5431 8DE4 E3A8 4AA7 737D 20BE 0DB8 4F03 BD39sub 1024g/FDBB477D 2001-01-15$</p>
</blockquote>
<p>这样，就从密匙数据中生成了其指纹信息，并且应该是唯一的。然后，我们打电话给mike，确认两件事情。首先，他是否发送给我们了密匙；其次，他的公匙的指纹信息是什么。如果Mike确认了这两件事情，我们就可以确信这个密匙是合法的。接下来，我们对密匙进行签名操作，以表示这个密匙来自Mike而且我们对密匙的信任，相关命令及执行情况如下：</p>
<blockquote>
<p>$ gpg &#8211;sign-key mike@hairnet.orgpub 1024D/4F03BD39 created: 2001-01-15 expires: neversub 1024g/FDBB477D created: 2001-01-15 expires: never(1) Mike Socks (I&#8217;m WIRED) pub 1024D/4F03BD39 created: 2001-01-15 expires: neverFingerprint = B121 5431 8DE4 E3A8 4AA7 737D 20BE 0DB8 4F03 BD39Mike Socks (I&#8217;m WIRED) Are you really sure that you want to sign this keywith your key: Ima User (I&#8217;m just ME) Really sign? yYou need a passphrase to unlock the secret key foruser: Ima User (I&#8217;m just ME) 1024-bit DSA key, ID D9BAC463, created 2001-01-03Enter passphrase:$</p>
</blockquote>
<p>执行到此，使用我们的私匙完成了对Mike的公匙的签名操作，任何持有我们的公匙的人都可以查证签名确实属于我们自己。这个附加到Mike的公匙上的签名信息将随它环游Internet世界，我们使用个人信誉，也就是我们自己的私匙，保证了那个密匙确实属于Mike。这是一个多么感人的充满诚信的故事啊 <img class="wp-smiley" alt=":-)" src="http://www.alexgao.com/wp-includes/images/smilies/icon_smile.gif" /> 现实世界的人们是否应该从这严格的技术标准中反思些什么呢？</p>
<p>还是回到这里。获取附加于一个公匙上的签名信息列表的命令是：</p>
<blockquote>
<p>gpg &#8211;check-sigs mike@hairnet.org</p>
</blockquote>
<p>签名列表越长，密匙的可信度越大。其实，正是签名系统本身提供了密匙查证功能。假设我们接收到一个签名为Mike的密匙，通过Mike的公匙，我们验证出签名确实属于Mike，那么我们就信任了这个密匙。推而广之，我们就可以信任Mike签名的任何密匙。</p>
<p>为了更加稳妥，GPG还引入了另一个附加功能：可信级别（trust level）。使用它，我们可以为我们拥有的任何密匙的所有者指定可信级别。例如，即使我们知道Mike的公匙是可信的，但是事实上我们不能信任Mike在对其他密匙签名时的判断；我们会想，Mike也许只对少数密匙进行了签名，但却没有好好地检查一遍。</p>
<p>设置可信级别的命令及执行情况如下：</p>
<blockquote>
<p>$ gpg &#8211;edit-key mike@hairnet.orgpub 1024D/4F03BD39 created: 2001-01-15 expires: never trust: -/fsub 1024g/FDBB477D created: 2001-01-15 expires: never(1) Mike Socks (I&#8217;m WIRED) Command&gt; trust 1 = Don&#8217;t know 2 = I do NOT trust 3 = I trust marginally 4 = I trust fully s = please show me more information m = back to the main menuYour decision? 2Command&gt; quit$</p>
</blockquote>
<p>在命令编辑环境中执行trust，然后选择级别2（I do NOT trust），这样我们割断了任何信任链，使每个密匙都必须经过Mike的签名。</p>
<p><strong><span style="color: #ff6600">6.删除密钥</span></strong></p>
<p>从私钥钥匙环里删除密钥：</p>
<div class="quote">
<blockquote>
<div class="quote-content"># gpg &#8211;delete-secret-keys hyphenwang@redflag-linux.com<br />
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.<br />
This program comes with ABSOLUTELY NO WARRANTY.<br />
This is free software, and you are welcome to redistribute it<br />
under certain conditions. See the file COPYING for details.
<p>&nbsp;</p>
<p>sec&nbsp;&nbsp;1024D/A3942296 2008-12-19 Hyphen Wang (Use for GPG Encrypt) &lt;gpgencrypt@linuxfly.org&gt;</p>
<p>要从钥匙环里删除这把密钥吗？(y/N)y<br />
这是一把私钥！――真的要删除吗？(y/N)y</p>
</div>
</blockquote></div>
<p>必须先删除私钥，然后才能删除公钥。<br />
从公钥钥匙环里删除密钥：</p>
</div>
<div sizset="135" sizcache="1">
<div class="quote" sizset="135" sizcache="1">
<blockquote>
<div class="quote-content"># gpg &#8211;delete-keys hyphenwang@redflag-linux.com<br />
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.<br />
This program comes with ABSOLUTELY NO WARRANTY.<br />
This is free software, and you are welcome to redistribute it<br />
under certain conditions. See the file COPYING for details.
<p>&nbsp;</p>
<p>sec&nbsp;&nbsp;1024D/A3942296 2008-12-19 Hyphen Wang (Use for GPG Encrypt) &lt;gpgencrypt@linuxfly.org&gt;</p>
<p>要从钥匙环里删除这把密钥吗？(y/N)y</p>
</div>
</blockquote>
<h2 class="quote-content"><span style="color: #3366ff"><strong><strong>三.对称加密:</strong></strong></span></h2>
<p>当然GPG同样具备普通的对称加密功能，这时候就不需要密钥，直接用密码加密即可（注意，这里的密码不一定是你私钥的密码，您大可以随意设定）</p>
<blockquote>
<p>gpg -o doc.gpg -c doc</p>
</blockquote>
<p>－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－</p>
<h2 class="quote-content"><span style="color: #3366ff"><strong>四.GPG常用参数:</strong></span></h2>
<blockquote>
<div class="quote-content">语法：gpg [选项] [文件名]<br />
签字、检查、加密或解密<br />
默认的操作依输入数据而定</div>
</blockquote>
<p>指令：</p>
<blockquote>
<p>-s, &#8211;sign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份签字<br />
&#8211;clearsign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份明文签字<br />
-b, &#8211;detach-sign&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份分离的签字<br />
-e, &#8211;encrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 加密数据<br />
-c, &#8211;symmetric&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 仅使用对称加密<br />
-d, &#8211;decrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 解密数据(默认)<br />
&#8211;verify&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 验证签字<br />
&#8211;list-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出密钥<br />
&#8211;list-sigs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出密钥和签字<br />
&#8211;check-sigs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出并检查密钥签字<br />
&#8211;fingerprint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出密钥和指纹<br />
-K, &#8211;list-secret-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出私钥<br />
&#8211;gen-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一副新的密钥对<br />
&#8211;delete-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从公钥钥匙环里删除密钥<br />
&#8211;delete-secret-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从私钥钥匙环里删除密钥<br />
&#8211;sign-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为某把密钥添加签字<br />
&#8211;lsign-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为某把密钥添加本地签字<br />
&#8211;edit-key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 编辑某把密钥或为其添加签字<br />
&#8211;gen-revoke&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成一份吊销证书<br />
&#8211;export&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 导出密钥<br />
&#8211;send-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 把密钥导出到某个公钥服务器上<br />
&#8211;recv-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从公钥服务器上导入密钥<br />
&#8211;search-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在公钥服务器上搜寻密钥<br />
&#8211;refresh-keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从公钥服务器更新所有的本地密钥<br />
&#8211;import&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 导入/合并密钥<br />
&#8211;card-status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 打印卡状态<br />
&#8211;card-edit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更改卡上的数据<br />
&#8211;change-pin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更改卡的 PIN<br />
&#8211;update-trustdb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 更新信任度数据库<br />
&#8211;print-md 算法 [文件]&nbsp;&nbsp;&nbsp; 使用指定的散列算法打印报文散列值</p>
<p>选项：</p>
<p>-a, &#8211;armor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 输出经 ASCII 封装<br />
-r, &#8211;recipient 某甲&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为收件者&#8220;某甲&#8221;加密<br />
-u, &#8211;local-user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用这个用户标识来签字或解密<br />
-z N&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设定压缩等级为 N (0 表示不压缩)<br />
&#8211;textmode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用标准的文本模式<br />
-o, &#8211;output&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 指定输出文件<br />
-v, &#8211;verbose&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 详细模式<br />
-n, &#8211;dry-run&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不做任何改变<br />
-i, &#8211;interactive&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 覆盖前先询问<br />
&#8211;openpgp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 行为严格遵循 OpenPGP 定义<br />
&#8211;pgp2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生成与 PGP 2.x 兼容的报文</p>
</blockquote>
<p>(请参考在线说明以获得所有命令和选项的完整清单)</p>
<p>范例：</p>
<p>-se -r Bob [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为 Bob 这个收件人签字及加密<br />
&#8211;clearsign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做出明文签字<br />
&#8211;detach-sign [文件名]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 做出分离式签字<br />
&#8211;list-keys [某甲]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示密钥<br />
&#8211;fingerprint [某甲]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示指纹</p>
<p>———————————————————————————&#8211;</p>
</div>
</div>
 <img src ="http://www.blogjava.net/alancxx/aggbug/348895.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-04-23 22:55 <a href="http://www.blogjava.net/alancxx/articles/348895.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GPG 简单使用</title><link>http://www.blogjava.net/alancxx/articles/GPG.html</link><dc:creator>蜂鸟</dc:creator><author>蜂鸟</author><pubDate>Sat, 23 Apr 2011 14:39:00 GMT</pubDate><guid>http://www.blogjava.net/alancxx/articles/GPG.html</guid><wfw:comment>http://www.blogjava.net/alancxx/comments/348893.html</wfw:comment><comments>http://www.blogjava.net/alancxx/articles/GPG.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alancxx/comments/commentRss/348893.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alancxx/services/trackbacks/348893.html</trackback:ping><description><![CDATA[<div id="art" style="margin: 15px" width="100%">
<h2 class="title" style="color: #000080">The GNU Privacy Guard</h2>
<p>Private和public的钥匙是gpg加密和解密过程的主要部分,所以第一步就是创建为自己创建一对密匙.</p>
<div class="procedure">
<ol type="1">
    <li>
    <p><strong>生成私钥</strong></p>
    <table style="border-right: rgb(153,153,153) 1px solid; border-top: rgb(153,153,153) 1px solid; font-size: 12px; border-left: rgb(153,153,153) 1px solid; width: 80%; border-bottom: rgb(153,153,153) 1px solid" align="center">
        <tbody>
            <tr>
                <td><strong style="color: #ff0000">$gpg --gen-key</strong><br />
                </td>
            </tr>
        </tbody>
    </table>
    <p>你需要回答一些这个命令提出的问题</p>
    <ol type="a">
        <li>
        <p>私钥的种类和size，这里缺省的答案已经足够好了</p>
        <li>
        <p>私钥的有效期，我通常选择不会过期，呵呵</p>
        <li>
        <p>你的真实的姓名和e-mail地址，这些是用来从一大堆钥匙中找到你的钥匙的</p>
        <li>
        <p>关于你的钥匙的comment，可以为空，我一般填一个昵称</p>
        <li>
        <p>钥匙的密码. 千万别忘了，否则所有你加密过的文件都没用了</p>
        </li>
    </ol>
    <li>
    <p><strong>为你的私钥生成一个公钥(文本文件)，这是我的：</strong><a href="http://blogimg.chinaunix.net/blog/upfile2/080129235300.zip" target="_blank"><strong>aubrey.asc.zip</strong></a></p>
    <table style="border-right: rgb(153,153,153) 1px solid; border-top: rgb(153,153,153) 1px solid; font-size: 12px; border-left: rgb(153,153,153) 1px solid; width: 552px; border-bottom: rgb(153,153,153) 1px solid; height: 40px" align="center">
        <tbody>
            <tr>
                <td><strong><span style="color: #ff0000">$ gpg --armor --output public.key --export &lt;your email&gt;<br />
                </span></strong></td>
            </tr>
        </tbody>
    </table>
    <p>你可以分发这个文件了，给你的朋友，或者贴到你的个人网站上， or whatever.</p>
    <li><strong>为自己加密一个文件. 这里--recipient可以是你的全名，也可以是你的邮件地址<br />
    </strong>
    <table style="border-right: rgb(153,153,153) 1px solid; border-top: rgb(153,153,153) 1px solid; font-size: 12px; border-left: rgb(153,153,153) 1px solid; width: 547px; border-bottom: rgb(153,153,153) 1px solid; height: 38px" align="center">
        <tbody>
            <tr>
                <td>
                <pre class="programlisting"><strong><span style="color: #ff0000">#gpg --encrypt --recipient 'Your Name' foo.tx<span style="font-family: 'Courier New',Courier,'宋体'">t</span></span></strong></pre>
                </td>
            </tr>
        </tbody>
    </table>
    <li><strong><span style="font-family: monospace">解</span>密这个文件. 这里不加--output选项的话,解密的内容将被送到屏幕上</strong>
    <table style="border-right: rgb(153,153,153) 1px solid; border-top: rgb(153,153,153) 1px solid; font-size: 12px; border-left: rgb(153,153,153) 1px solid; width: 544px; border-bottom: rgb(153,153,153) 1px solid; height: 33px" align="center">
        <tbody>
            <tr>
                <td>
                <pre class="programlisting"><strong><span style="color: #ff0000">#gpg --output foo.txt --decrypt foo.txt.gpg</span></strong></pre>
                </td>
            </tr>
        </tbody>
    </table>
    <li><strong><span style="font-family: monospace">为</span>别人加密一个文件. 这里首先要import别人的公钥，然后加密。注意这里变化的只是--recipient选项</strong>
    <table style="border-right: rgb(153,153,153) 1px solid; border-top: rgb(153,153,153) 1px solid; font-size: 12px; border-left: rgb(153,153,153) 1px solid; width: 544px; border-bottom: rgb(153,153,153) 1px solid; height: 31px" align="center">
        <tbody>
            <tr>
                <td>
                <pre class="programlisting"><strong style="color: #ff0000"><span style="font-family: 'Courier New',Courier,'宋体'">#gpg </span>--import key.asc<br />
                #gpg --list-keys<br />
                #gpg --encrypt --recipient 'myfriend@his.isp.net' foo.txt</strong><br />
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <li><strong>解密一个从别人那里发来的文件. 这个和本机加密的文件解密没什么区别.<br />
    </strong>
    <table style="border-right: rgb(153,153,153) 1px solid; border-top: rgb(153,153,153) 1px solid; font-size: 12px; border-left: rgb(153,153,153) 1px solid; width: 544px; border-bottom: rgb(153,153,153) 1px solid; height: 38px" align="center">
        <tbody>
            <tr>
                <td>
                <pre class="programlisting"><strong><span style="color: #ff0000"><strong>#gpg --output foo.txt --decrypt foo.txt.gpg</strong></span></strong>          </pre>
                </td>
            </tr>
        </tbody>
    </table>
    </li>
</ol>
</div>
<br />
</div>
 <img src ="http://www.blogjava.net/alancxx/aggbug/348893.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alancxx/" target="_blank">蜂鸟</a> 2011-04-23 22:39 <a href="http://www.blogjava.net/alancxx/articles/GPG.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>