<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>亂碼 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/%E4%BA%82%E7%A2%BC/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 18 Apr 2023 08:16:56 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>亂碼 &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>簡單兩招，解決解壓縮 zip 檔的亂碼問題</title>
		<link>https://noter.tw/7532/%e7%b0%a1%e5%96%ae%e5%85%a9%e6%8b%9b%ef%bc%8c%e8%a7%a3%e6%b1%ba%e8%a7%a3%e5%a3%93%e7%b8%ae-zip-%e6%aa%94%e7%9a%84%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c/</link>
					<comments>https://noter.tw/7532/%e7%b0%a1%e5%96%ae%e5%85%a9%e6%8b%9b%ef%bc%8c%e8%a7%a3%e6%b1%ba%e8%a7%a3%e5%a3%93%e7%b8%ae-zip-%e6%aa%94%e7%9a%84%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sun, 07 Jun 2020 05:09:28 +0000</pubDate>
				<category><![CDATA[應用分享]]></category>
		<category><![CDATA[技巧分享]]></category>
		<category><![CDATA[記下來]]></category>
		<category><![CDATA[解壓縮]]></category>
		<category><![CDATA[錯誤]]></category>
		<category><![CDATA[7zip]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[特殊符號]]></category>
		<category><![CDATA[中文亂碼]]></category>
		<category><![CDATA[winrar]]></category>
		<category><![CDATA[zip]]></category>
		<category><![CDATA[教學]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[怎麼]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=7532</guid>

					<description><![CDATA[<p>小蛙有時候在網路下載壓縮檔 zip，用 WinRAR 解壓縮的時候會噴出一堆錯誤，這篇記錄下該怎麼處理這問題。(注意：這邊提到的是 .zip 檔，不是 .rar 喔) 雖然噴出錯誤，但解壓縮的動作還是&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/7532/%e7%b0%a1%e5%96%ae%e5%85%a9%e6%8b%9b%ef%bc%8c%e8%a7%a3%e6%b1%ba%e8%a7%a3%e5%a3%93%e7%b8%ae-zip-%e6%aa%94%e7%9a%84%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">簡單兩招，解決解壓縮 zip 檔的亂碼問題</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>小蛙有時候在網路下載壓縮檔 zip，用 WinRAR 解壓縮的時候會噴出一堆錯誤，這篇記錄下該怎麼處理這問題。(注意：這邊提到的是 .zip 檔，不是 .rar 喔)</p>



<span id="more-7532"></span>



<p>雖然噴出錯誤，但解壓縮的動作還是會繼續進行，不過有檔名問題的檔案解出來有的會消失，有的會變成亂碼無法辨識或開啟，上網查了一些解法，看到 WinRAR 有調整編碼的功能，但是小蛙自己電腦上的 WinRAR 找不到這個功能。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://lh3.googleusercontent.com/pw/ACtC-3cpVQn4WEkvG6F6VqPCyZdK5UFM6XROEenzERZ5GTrJpuKIQ0dkhkcwzeBIDnCKsJ_4Vj_S_yMYhUGaNkWoMMunN2lcJhgcJB74uY-ktZFw9BJAOu8wgwYSpmtFrAYJ3hvGTf1GrtvpcDyrAJwO1IbBlQ=w1305-h772-no?authuser=1" alt="ACtC 3cpVQn4WEkvG6F6VqPCyZdK5UFM6XROEenzERZ5GTrJpuKIQ0dkhkcwzeBIDnCKsJ 4Vj S yMYhUGaNkWoMMunN2lcJhgcJB74uY ktZFw9BJAOu8wgwYSpmtFrAYJ3hvGTf1GrtvpcDyrAJwO1IbBlQ=w1305 h772 no?authuser=1 簡單兩招，解決解壓縮 zip 檔的亂碼問題" title="簡單兩招，解決解壓縮 zip 檔的亂碼問題"></figure></div>



<p>突然想到小蛙家裡的電腦，沒有安裝 WinRAR，但是還是一樣可以把檔案解壓縮出來，於是做了一些測試，以下是小蛙自己實際使用成功的解決方法，不確定是否能適用於每一個人的狀況，但可以試試看。</p>



<h2 class="wp-block-heading">使用 7-zip</h2>



<p>小蛙家裡的電腦只有安裝 7-zip 沒有 WinRAR，記得同一個壓縮檔好像沒有遇到公司電腦遇到的問題，再次使用 7-zip 解壓縮同一個 zip 試試，結果真的可以正確解壓縮，完全沒有噴出任何錯誤！</p>



<h2 class="para wp-block-heading">使用 Windows 內建</h2>



<p>Windows 10 以上的系統，在 zip 壓縮檔上面點選滑鼠右鍵 -&gt; 解壓縮全部，接著選取要解壓縮到的目錄，即會進行解壓縮，過程也可以順利解完，不會出現上述錯誤。</p>



<p>或者也可以直接在 zip 壓縮檔上點選滑鼠左鍵兩下，會開出一個檔案總管視窗，裡面是壓縮檔的內容，把要解壓縮的檔案框選起來，或是 ctrl + a 選擇所有檔案，點選左鍵把這些檔案拖曳到要解壓縮的資料夾裡就可以囉！</p>



<p><strong>Windows 相關文章</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/10439/windows-%e8%a4%87%e8%a3%bd%e6%89%80%e6%9c%89%e6%aa%94%e6%a1%88%e5%90%8d%e7%a8%b1%ef%bc%8c%e4%bb%a5%e5%8f%8a-notepad-%e5%a4%9a%e6%ac%84%e9%81%b8%e5%8f%96%e5%8f%8a%e8%a4%87%e8%a3%bd%e5%8a%9f%e8%83%bd/" data-wpel-link="internal">Windows 複製所有檔案名稱，以及 Notepad++ 多欄選取及複製功能</a></li><li><a href="https://noter.tw/8636/windows-update-error/" data-wpel-link="internal">您的裝置缺少重要的安全性和品質的修正</a></li><li><a href="https://noter.tw/8573/printer/" data-wpel-link="internal">Windows 10 新增網路印表機</a></li><li><a href="https://noter.tw/8564/samba/" data-wpel-link="internal">重設 Windows 網路芳鄰 (Samba) 記憶密碼</a></li><li><a href="https://noter.tw/4539/windows-vpn-%e8%a8%ad%e5%ae%9a-%e4%bd%bf%e7%94%a8-softether-vpn/" data-wpel-link="internal">Windows VPN 設定</a></li><li><a href="https://noter.tw/2602/windows-%e8%a8%ad%e5%ae%9a-vpn-%e9%80%a3%e7%b7%9a/" data-wpel-link="internal">Windows 設定 VPN 連線</a></li><li><a href="https://noter.tw/8645/remove-trend-micro-security-agent/" data-wpel-link="internal">Trend Micro Security Agent 移除方法</a></li><li><a href="https://noter.tw/7532/%e7%b0%a1%e5%96%ae%e5%85%a9%e6%8b%9b%ef%bc%8c%e8%a7%a3%e6%b1%ba%e8%a7%a3%e5%a3%93%e7%b8%ae-zip-%e6%aa%94%e7%9a%84%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">簡單兩招，解決解壓縮 zip 檔的亂碼問題</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/7532/%e7%b0%a1%e5%96%ae%e5%85%a9%e6%8b%9b%ef%bc%8c%e8%a7%a3%e6%b1%ba%e8%a7%a3%e5%a3%93%e7%b8%ae-zip-%e6%aa%94%e7%9a%84%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">簡單兩招，解決解壓縮 zip 檔的亂碼問題</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/7532/%e7%b0%a1%e5%96%ae%e5%85%a9%e6%8b%9b%ef%bc%8c%e8%a7%a3%e6%b1%ba%e8%a7%a3%e5%a3%93%e7%b8%ae-zip-%e6%aa%94%e7%9a%84%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>PHP：UTF-8 跟 Big5 的糾結</title>
		<link>https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/</link>
					<comments>https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 17 Sep 2018 14:39:51 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[rawurlencode]]></category>
		<category><![CDATA[encodeURIComponent]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[big5]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[iconv]]></category>
		<category><![CDATA[亂碼]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=109</guid>

					<description><![CDATA[<p>今天在公司處理到一個編碼的問題，由於有許多歷史包袱都是以 big5 下去開發，短時間內沒有辦法把所有系統轉換成 utf-8 的情況下，就經常要面對編碼轉換的問題，這邊記錄一下今天遇到問題的解法。 有個&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/" data-wpel-link="internal">PHP：UTF-8 跟 Big5 的糾結</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>今天在公司處理到一個編碼的問題，由於有許多歷史包袱都是以 big5 下去開發，短時間內沒有辦法把所有系統轉換成 utf-8 的情況下，就經常要面對編碼轉換的問題，這邊記錄一下今天遇到問題的解法。</p>



<span id="more-109"></span>



<p>有個新的系統 B 必須要串接舊系統 A，B 是 utf-8 環境，A 是 big5 環境，目前設計是 B 透過 AJAX 來讀取 A 的資料，A 這邊吃的是經過 encodeURIComponent 處理過的資料，例如：「你好」 -&gt;&nbsp;「%A7%41%A6%6E」，前情提要到這邊。</p>



<p>小蛙的笨腦袋就開始想怎麼處理：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>該頁面拿到的資料都是 UTF-8，拿到之後用 iconv 轉 big5 先</li><li>轉成 big5 之後，再做 encodeURIComponent 就好了</li><li>easy ~</li></ul>



<p>整個串起來之後，正確的「你好」應該是「%A7%41%A6%6E」，小蛙卻得到「%EF%BF%BDA%EF%BF%BDn」，看起來好像差很大，於是想說可能是因為 php 要透過 javascript encodeURIComponent 來轉，雖然轉好 big5 但是又在 utf-8 的文本中印出導致的，於是開始往「一切都在 php 中轉好，javascript 直接拿到轉好值就丟出去」的方向進行，小蛙改變策略：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>在 php 中使用 iconv 把 UTF-8 轉成 big5</li><li>在 php 中使用 <a rel="noreferrer noopener nofollow external" href="https://blog.longwin.com.tw/2015/11/php-rawurlencode-urlencode-diff-2015/" target="_blank" data-wpel-link="external" class="wpel-icon-right">rawurlencode<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 轉換</li><li>把結果透過 javascript 去要資料</li></ul>



<p>嗯，看起來應該是無懈可擊！哦哦哦哦哦哦 ~ 高興的尖叫起來，隔壁同事都看了小蛙一眼，但回傳的結果怪怪的啊，看一下後台 log，「你好」變成了「%A7A%A6n」，額外測試一個「外部」卻變成了「%A5~%B3%A1」，花惹發？嗯 ~ 港節好像比第一次得到的更接近正解一點(?)，再來想想問題在哪，重新順一下流程好了，第一步跟第二步都做轉換的動作，其中一定有一個轉換失效了，要不第一個要不第二個(廢話)。</p>



<p>Google 了一下看到這篇「<a href="http://www.edbiji.com/doccenter/showdoc/4/nav/680.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">php 转码函数 你还在用iconv吗？<span class="wpel-icon wpel-image wpel-icon-6"></span></a>」，既然懷疑轉換出問題，那就把他換掉看看，事情絕對沒有憨人所想的那麼簡單，結果沒變！沒變 &#8230; 記得以前有一次使用 base64 來做過這件事，正當打算要來試的時候，超猛同事走了過來問說發生什麼事，小蛙大概跟他講解了一下，他只回了一句，那你把 iconv 轉的東西印出來看就知道了。</p>



<p>看來出現新的曙光，Google 查到這篇「<a href="https://stackoverflow.com/questions/14674834/php-convert-string-to-hex-and-hex-to-string" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PHP convert string to hex and hex to string<span class="wpel-icon wpel-image wpel-icon-6"></span></a>」，於是把 iconv 改成&nbsp;mb_convert_encoding，然後再透過該文章中的 function 來做轉換，就可以成功轉換成系統 A 需要的格式了！</p>



<p>可喜可樂！留個備份，記錄超強同事的神蹟！</p>



<h2 class="wp-block-heading">發現問題 (2018-10-16更新)</h2>



<p>上面的作法用了一段時間後發現有些字串會解析成不一樣的東西，例如：<code>帶卜辭</code>實際上收到卻變成<code>帶尸辭</code>、<code>恆春</code>變成<code>恆柢</code>、<code>小屯</code>變成<code>匕屯</code>。<br>以「帶卜辭」來當作轉換的例子<br>帶 =&gt; 「%B1a」<br>卜 =&gt; 「%A4R」<br>辭 =&gt; 「%C3%E3」</p>



<p>如果用上面的方法轉出來，轉出來會是全小寫，<br>帶 &lt;= 「%b1a」<br>尸 &lt;= 「%a4r」<br>辭 &lt;= 「%c3%e3」</p>



<p>下方是把它改成全大寫<br>婢 &lt;= 「%B1A」<br>卜 &lt;= 「%A4R」<br>辭 &lt;= 「%C3%E3」</p>



<p>但以上兩者都不對，因為原本的方法轉出來之後，就通通變成小寫，而大小寫卻對應到不一樣的字，造成這次的問題。</p>



<h2 class="wp-block-heading">使用 rawurlencode (2018-10-16更新)</h2>



<p>只要先把要轉換的文字，正確地轉到 big5 之後，透過 rawurlencode 來做編碼就可以編出正確的內容囉！</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">// 先把要的文字轉成 big5，再整個透過 rawurlencode 轉換
$txt = rawurlencode(mb_convert_encoding($txt, "big5", "utf-8"));</pre>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/" data-wpel-link="internal">PHP：UTF-8 跟 Big5 的糾結</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>tomcat 下載檔案卻直接亂碼開啟 at IE 10</title>
		<link>https://noter.tw/3055/tomcat-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%8d%bb%e7%9b%b4%e6%8e%a5%e4%ba%82%e7%a2%bc%e9%96%8b%e5%95%9f-at-ie-10/</link>
					<comments>https://noter.tw/3055/tomcat-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%8d%bb%e7%9b%b4%e6%8e%a5%e4%ba%82%e7%a2%bc%e9%96%8b%e5%95%9f-at-ie-10/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 02 Sep 2013 12:37:48 +0000</pubDate>
				<category><![CDATA[Web Server]]></category>
		<category><![CDATA[檔案格式錯誤]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[web.xml]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[mime-mapping]]></category>
		<category><![CDATA[mime-type]]></category>
		<category><![CDATA[msi]]></category>
		<category><![CDATA[下載檔案]]></category>
		<category><![CDATA[副檔名]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=3055</guid>

					<description><![CDATA[<p>上星期五接到一張 ticket 內容大概是網站上提供下載 .msi 的安裝檔案，在經過測試後大部分瀏覽器都可以正常下載檔案，唯獨漏掉測試的 IE 10 會出現問題，IE 10 會把 .msi 檔案當作&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3055/tomcat-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%8d%bb%e7%9b%b4%e6%8e%a5%e4%ba%82%e7%a2%bc%e9%96%8b%e5%95%9f-at-ie-10/" data-wpel-link="internal">tomcat 下載檔案卻直接亂碼開啟 at IE 10</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>上星期五接到一張 ticket 內容大概是網站上提供下載 .msi 的安裝檔案，在經過測試後大部分瀏覽器都可以正常下載檔案，唯獨漏掉測試的 IE 10 會出現問題，IE 10 會把 .msi 檔案當作文字檔一般開啟，在頁面上印出一堆亂碼，簡單記錄一下解決方法。<br />
<span id="more-3055"></span><br />
IE 一直以來都不怎麼令小蛙討喜，每次在 IE 不是跑版就是碰到一些奇奇怪怪的問題，相對的也可能習慣用 IE 的開發者來說會覺得 Chrome, Firefox 才是非正道，不管怎樣網站開發人員在相容性部份常常一個頭兩個大！小蛙這邊的系統是 tomcat 架設，在檔案下載 .msi 的部份，Chrome, Firefox 可以正常下載檔案，IE 8, IE 9 也都可以正常下載檔案，不知道為什麼到了 IE 10 就變成無法正常下載檔案了 &#8230;<br />
為了還原這張 ticket 的情況，特地將筆電中的 IE 9 更新到 IE 10，由於 Windows 7 的 IE 10 需要 SP 1 以上才可以安裝 &#8230; 又花了不少時間把 Windows 7 安裝 SP 1 再更新 IE 10，不知道為什麼每次遇到跟 IE 有關的問題都會滿肚子火(個人偏見)，前陣子看到朋友塗鴉牆上寫著「終於找到 IE 的用途了，是拿來下載 Chrome！」要不是有一些 IE only 的服務或是網站，小蛙心想 IE 被安裝在電腦上的比例會更低吧！<br />
離題了，這個問題是 IE 10 下載 tomcat 伺服器上的檔案時，會變成「直接開啟」的狀態，也就是跟下載 txt 檔相同，把 msi 檔案直接印在網頁上，本來小蛙懶得找原因，只想偷懶直接把 .msi 換成 .zip 檔案就可以下載了，後來想想有好幾個頁面要改，有點不甘心，決定還是 google 一下看看。<br />
可以透過簡單設定 tomcat 上的 /usr/local/tomcat/conf/web.xml&nbsp;設定檔來解決這個問題，路徑以 tomcat 安裝目錄為主，只要在 web.xml 中加入以下</p>
<pre class="brush: xml; gutter: true">&lt;mime-mapping&gt;
    &lt;extension&gt;msi&lt;/extension&gt;
    &lt;mime-type&gt;application/x-msi&lt;/mime-type&gt;
&lt;/mime-mapping&gt;</pre>
<p>重新啟動 tomcat 即可！打完收工！<br />
<strong>參考資料：</strong></p>
<ol>
<li>.msi downloading as text @ stackoverflow<br />
<address><a href="http://stackoverflow.com/questions/1079630/msi-downloading-as-text" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">http://stackoverflow.com/questions/1079630/msi-downloading-as-text<span class="wpel-icon wpel-image wpel-icon-6"></span></a></address>
</li>
</ol>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3055/tomcat-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%8d%bb%e7%9b%b4%e6%8e%a5%e4%ba%82%e7%a2%bc%e9%96%8b%e5%95%9f-at-ie-10/" data-wpel-link="internal">tomcat 下載檔案卻直接亂碼開啟 at IE 10</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/3055/tomcat-%e4%b8%8b%e8%bc%89%e6%aa%94%e6%a1%88%e5%8d%bb%e7%9b%b4%e6%8e%a5%e4%ba%82%e7%a2%bc%e9%96%8b%e5%95%9f-at-ie-10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>中文亂碼總整理 (Tomcat 5.5)</title>
		<link>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/</link>
					<comments>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 15:21:55 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[中文亂碼]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[charset]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2902</guid>

					<description><![CDATA[<p>之前舊的部落格發過幾篇解決 Tomcat 中文亂碼問題的文章，確實是解決了一些中文顯示的問題，但今天在 Struts2 時，又再度遇到中文亂碼的問題，所以想把幾個可能忽略的步驟記錄下來。 其實解決中文&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/" data-wpel-link="internal">中文亂碼總整理 (Tomcat 5.5)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>之前舊的部落格發過幾篇解決 Tomcat 中文亂碼問題的文章，確實是解決了一些中文顯示的問題，但今天在 Struts2 時，又再度遇到中文亂碼的問題，所以想把幾個可能忽略的步驟記錄下來。</p>



<span id="more-2902"></span>



<p>其實解決中文亂碼，只要把所有的編碼格式都調整成一致，比如說，編輯器預設儲存編碼為 Big5，卻把頁面設定成 <code>&lt;%@ page contentType="text/html; charset=utf-8" %></code>，這樣一來，在瀏覽器開啟的網頁，中文就會是亂碼。(反過來也會是亂碼)。唯有兩者設定都是相同編碼的時候，在網頁上才能正確顯示中文。</p>



<p>上面提到，把所有的編碼都設成 Big5，這麼一來，也符合了「所有編碼都設為相同」的原則，當然包括編輯器預設儲存編碼也是 Big5，如此網頁可以正確顯示中文字，但是 &#8230; 如果後端需要加上 MySQL 資料庫，如何在表單跟 MySQL 之間正確傳遞中文，可能又會是一個花時間的問題，經過幾番折騰之後，決定把所有的編碼都設定為 utf-8 比較方便。</p>


<div id="rank-math-howto" class="rank-math-block" >
<div class="rank-math-howto-description">

</div>

<div class="rank-math-steps ">
<div id="howto-step-643e51f888560" class="rank-math-step">
<h3 class="rank-math-step-title ">編輯器預設儲存編碼 => 設定成 <strong>UTF-8</strong></h3>
<div class="rank-math-step-content "><p>以 EditPlus3 為例，可從「工具」->「偏好項」->「檔案」->「預設編碼模式」改成 UTF-8，<br />注意之前可能存成 Big5 的，可以按另存新檔後，最下面編碼模式可以改變。</p>
</div>
</div>
<div id="howto-step-643e51f888562" class="rank-math-step">
<h3 class="rank-math-step-title ">每個 JSP 頁面 charset 設定成 UTF-8</h3>
<div class="rank-math-step-content "><p>e.g. &lt;%@ page contentType=&#8221;text/html; charset=utf-8&#8243; %></p>
</div>
</div>
<div id="howto-step-643e51f888563" class="rank-math-step">
<h3 class="rank-math-step-title ">MySQL my.ini 設定成</h3>
<div class="rank-math-step-content ">[client]default-character-set=utf8<br />[mysqld]default-character-set=utf8</p>
</div>
</div>
<div id="howto-step-643e51f888564" class="rank-math-step">
<h3 class="rank-math-step-title ">資料庫、資料表及欄位的 <strong>character-set</strong> 設為 utf8，<strong>collation</strong>(校對)設定為utf8_unicode_ci。</h3>
<div class="rank-math-step-content "></div>
</div>
<div id="howto-step-643e51f888565" class="rank-math-step">
<h3 class="rank-math-step-title "><strong>資料庫連結部份</strong>修改</h3>
<div class="rank-math-step-content "><p>Connection con=DriverManager.getConnection(&#8220;jdbc:mysql://localhost/user?useUnicode=true&amp;characterEncoding=utf8&#8221;, &#8220;帳號&#8221;, &#8220;密碼&#8221;);</p>
</div>
</div>
<div id="howto-step-643e51f888566" class="rank-math-step">
<h3 class="rank-math-step-title ">Servlet 部份同樣把 <strong>charset</strong> 設定成 UTF-8</h3>
<div class="rank-math-step-content "><p>e.g. request.setContentType(&#8220;text/html; charset=utf-8&#8221;);</p>
</div>
</div>
<div id="howto-step-643e51f888567" class="rank-math-step">
<h3 class="rank-math-step-title ">GET 亂碼解決</h3>
<div class="rank-math-step-content "><p>在 %TOMCAT_HOME%\conf\server.xml 中，找到&lt;connector port=&#8221;8080&#8243; &#8230; 部分，加上 URIEncoding = &#8220;UTF-8&#8243;，重啟 Tomcat。</p>
</div>
</div>
<div id="howto-step-643e51f888568" class="rank-math-step">
<h3 class="rank-math-step-title ">POST 亂碼解決</h3>
<div class="rank-math-step-content "><p>若是有多個網站，將 %TOMCAT_HOME%\webapps\jsp-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class 複製到%TOMCAT_HOME%\shared\classes\filters 下，(若沒有資料夾可以自己新增)，修改 %TOMCAT_HOME%\webapps\(mytest)\WEB-INF\web.xml (mytest 為網站目錄)，加上以下內容 </p>
</div>
</div>
</div>
</div>


<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;filter> 
  &lt;filter-name>SetCharacterEncoding&lt;/filter-name> 
  &lt;filter-class>filters.SetCharacterEncodingFilter&lt;/filter-class> 
  &lt;init-param> 
    &lt;param-name>encoding&lt;/param-name> 
    &lt;param-value>Big5&lt;/param-value> 
  &lt;/init-param> 
&lt;/filter> 
&lt;filter-mapping> 
  &lt;filter-name>SetCharacterEncoding&lt;/filter-name> 
  &lt;url-pattern>/*&lt;/url-pattern> 
&lt;/filter-mapping></pre>



<p>照著這些步驟做，應該就能有個能正確顯示中文的平台(或許吧！至少小蛙現在的環境就是)如果發現什麼問題，再隨時補上。</p>



<p><strong>2009/07/31 補充：<br></strong>在使用 Prototype.js 的 Updater / Requester function 時，編碼為 big5 傳遞中文會發生亂碼，<br>PHP 的解決方式 -> <code>header("Content-Type:text/html;charset=BIG5");</code><br>JSP 解決方式 ->  <code>response.setHeader("Charset","BIG5"); </code><br>這裡 AJAX 的傳遞方式要用 get。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/" data-wpel-link="internal">中文亂碼總整理 (Tomcat 5.5)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2902/%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e7%b8%bd%e6%95%b4%e7%90%86-tomcat-5-5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Struts 1.3 + Spring 3.2 + Hibernate 3.6 錯誤筆記</title>
		<link>https://noter.tw/2418/struts-1-3-spring-3-2-hibernate-3-6-%e9%8c%af%e8%aa%a4%e7%ad%86%e8%a8%98/</link>
					<comments>https://noter.tw/2418/struts-1-3-spring-3-2-hibernate-3-6-%e9%8c%af%e8%aa%a4%e7%ad%86%e8%a8%98/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Fri, 02 Nov 2012 13:58:51 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Struts]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[ClassNotFoundException]]></category>
		<category><![CDATA[getDataSource]]></category>
		<category><![CDATA[jndi]]></category>
		<category><![CDATA[Servlet]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2418</guid>

					<description><![CDATA[<p>買了一本「王者歸來 Java Web 整合開發 (第二版)」，書中包含了 JSP + Servlet + Struts + Hibernate + Spring 等的教學及範例，K了一個星期後，發現最&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2418/struts-1-3-spring-3-2-hibernate-3-6-%e9%8c%af%e8%aa%a4%e7%ad%86%e8%a8%98/" data-wpel-link="internal">Struts 1.3 + Spring 3.2 + Hibernate 3.6 錯誤筆記</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>買了一本「<a href="http://www.books.com.tw/exep/prod/booksfile.php?item=0010553897" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right"><strong>王者歸來 Java Web 整合開發 (第二版)</strong><span class="wpel-icon wpel-image wpel-icon-6"></span></a>」，書中包含了 JSP + Servlet + Struts + Hibernate + Spring 等的教學及範例，K了一個星期後，發現最困難的部份竟然在環境架設上，光是環境架設就花了好長的時間，再來是設定檔，小蛙的 Eclipse 有些設定檔的 auto-complete 跑不出東西，加上每個版本的套件下載下來後的 Jar 檔、設定檔都不相同，不同版本的整合方式也不同 &#8230; 花了很久的時間在環境架設及設定檔上，這篇文章不記錄完整經過(忘的差不多了)，只挑著把過程中要注意的事情及錯誤記錄下來。<br />
<span id="more-2418"></span></p>
<ol>
<li>
<h4>隱藏 .jsp 頁面，對外以 .do 為主</h4>
<p>將 jsp 頁面通通放在 <strong><span style="color: #ff0000;">WEB-INF</span></strong> 底下，可自行建立其他分類資料夾，ex. pages, pages/admin, pages/member &#8230; 等。並且在 <strong><span style="color: #ff0000;">struts-conf.xml</span></strong> 中設定</p>
<pre class="brush: xml; gutter: true">&lt;action-mappings&gt;
    &lt;action path="/index" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/index.jsp"&gt;&lt;/action&gt;
&lt;/action-mappings&gt;</pre>
</li>
<li>
<h4>java.lang&nbsp;ClassNotFoundException 各式各樣怪怪的情況</h4>
<p>這情況太容易出現了，小蛙原本使用 Eclipse IDE 裡面自訂 <span style="color: #3366ff;">User Library</span> 的功能，卻一直報錯，最後把所有會用到的 Spring, Struts Jar 通通放在 <strong><span style="color: #ff0000;">Web/WEB-INF/lib</span></strong> 下，這些怪裡怪氣的錯誤就自己消失了 &#8230;</li>
<li>
<h4>亂碼處理</h4>
<p>這應該是最容易遇到的問題，有幾項前提要先遵守：</p>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>R1. &nbsp;必須先將每一頁 JSP 或每一個 Servelt 設定成 <span style="color: #ff0000;"><strong>UTF-8 編碼</strong></span>(小蛙直接用 UTF-8 編碼，可以省掉 BIG5 的一些麻煩)，如果是 Eclipse 專案請確定專案編碼為 UTF-8，有些文字編輯器可以看到目前檔案編碼。</p></blockquote>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>R2. 設定 JSP 表頭：<br />
<span style="color: #ff0000;"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &lt;%@ page language=&#8221;java&#8221; contentType=&#8221;text/html; charset=UTF-8&#8243; pageEncoding=&#8221;UTF-8&#8243;%&gt;</strong></span></p></blockquote>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p>R3. html meta：<br />
<span style="color: #ff0000;"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&gt;</strong></span></p></blockquote>
<p>以上三個一定要指定同一編碼，確定之後接下來四種方法只要選一種來做就可以了：</p>
<ul>
<li>
<h5>勤勞工人法：</h5>
<p>在每個 JSP 頁面及每個 Servelt 中設定</p>
<pre class="brush: java; gutter: true">request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");</pre>
</li>
<li>
<h5>源頭搞定法：</h5>
<p>使用 tomcat 預設過濾器 (在 <span style="color: #ff0000;"><strong>Tomcat安裝目錄/conf/ web.xml</strong></span> 中解開註解)</p>
<pre class="brush: xml; gutter: true">&lt;filter&gt;
    &lt;filter-name&gt;setCharacterEncodingFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.apache.catalina.filters.SetCharacterEncodingFilter&lt;/filter-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;encoding&lt;/param-name&gt;
        &lt;param-value&gt;UTF-8&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;async-supported&gt;true&lt;/async-supported&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;setCharacterEncodingFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre>
</li>
<li>
<h5>自我肯定法：</h5>
<p>自定義過濾器<br />
<strong>CharacterEncodingFilter.java</strong></p>
<pre class="brush: java; gutter: true">package com.hans.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncodingFilter implements Filter {
	private String characterEncoding;
	@Override
	public void init(FilterConfig config) throws ServletException {
		characterEncoding = config.getInitParameter("encoding");
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(characterEncoding);
		response.setCharacterEncoding(characterEncoding);
		chain.doFilter(request, response);
	}
	@Override
	public void destroy() {
		characterEncoding = null;
	}
}</pre>
<p>web 下的 <strong>web.xml</strong> 加入對應</p>
<pre class="brush: xml; gutter: true">&lt;filter&gt;
    &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;com.hans.filter.CharacterEncodingFilter&lt;/filter-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;encoding&lt;/param-name&gt;
        &lt;param-value&gt;utf-8&lt;/param-value&gt;
    &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre>
</li>
<li>
<h5>套件嵌用法：</h5>
<p>使用 Spring 過濾器</p>
<pre class="brush: xml; gutter: true">&lt;filter&gt;
    &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;encoding&lt;/param-name&gt;
            &lt;param-value&gt;utf-8&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
      	&lt;param-value&gt;true&lt;/param-value&gt;
    &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre>
<p>以上幾種方法可以挑選適用於自己的方法來用，勤勞工人法是比較累一點，每一頁都要記得檢查，不然就可能在表單傳遞的時候發生亂碼。</li>
</ul>
</li>
<li>
<h4>JNDI 錯誤</h4>
<pre>javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'"
javax.servlet.ServletException: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'"</pre>
<p>明明設定上沒有什麼問題，卻發生以上 exception，設定檔如下(MySQL)：</p>
<pre class="brush: xml; gutter: true">&lt;Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
	   maxActive="5" maxIdle="2" maxWait="60"
	   username="your_account" password="your_password" driverClassName="com.mysql.jdbc.Driver"
	   url="jdbc:mysql://your_domain:3306/your_database?characterEncoding=UTF-8"/&gt;</pre>
<p class="brush: xml; gutter: true">不管設定在 <span style="color: #ff0000;"><strong>server.xml</strong></span> 的&nbsp;<span style="color: #ff0000;"><strong>&lt;GlobalNamingResources&gt;</strong></span> 標籤中、設定在 <span style="color: #ff0000;"><strong>&lt;Host&gt;</strong></span> 標籤下或是設定在 <span style="color: #ff0000;"><strong>localhost/context.xml</strong></span> 下都會出現上面的錯誤，如果有遇到這樣問題的網友，試試看設定在 <span style="color: #ff0000;"><strong>Tomcat安裝目錄/conf/context.xml</strong></span> 裡面就不會發生這個錯誤囉！如果用這個方法要把&nbsp;<strong>mysql-connector-java-5.1.22-bin.jar</strong> 複製到&nbsp;<span style="color: #ff0000;"><strong>Tomcat安裝目錄/conf/ </strong></span>資料夾下。</p>
<p class="brush: xml; gutter: true">在 Spring 中加入上述 DataSource，可以供其他部份使用(透過 id 指定)</p>
<pre class="brush: xml; gutter: true">&lt;!-- 吃 tomcat 內部設定的 jndi 不然就要自己設定 --&gt;
&lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
    &lt;property name="jndiName" value="java:comp/env/jdbc/TestDB"&gt;&lt;/property&gt;
&lt;/bean&gt;</pre>
</li>
<li>
<h4>Struts 標籤設定</h4>
<p>前面提到書上的範例是寫</p>
<pre class="brush: java; gutter: true">&lt;%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %&gt;
&lt;%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %&gt;</pre>
<p class="brush: java; gutter: true">Eclipse 頻頻出現錯誤訊息，後來才查到 1.3 版要改成以下才可以使用</p>
<pre class="brush: java; gutter: true">&lt;%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %&gt;
&lt;%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %&gt;</pre>
</li>
<li>
<h4>struts-conf.xml 錯誤</h4>
<p>如果使用 1.3 的表頭檔是沒辦法設定標籤的</p>
<pre class="brush: xml; gutter: true">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE struts-config
	PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
	"http://struts.apache.org/dtds/struts-config_1_3.dtd"&gt;</pre>
<p class="brush: xml; gutter: true">要把它改成 1.2 版本的，並且要<span style="color: #ff0000;"><strong>注意每個 tag 之間的順序</strong></span>，這邊出錯的時候 Eclipse 會列出順序，照著排就可以了。</p>
<pre class="brush: xml; gutter: true">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE struts-config
	PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
	"http://struts.apache.org/dtds/struts-config_1_2.dtd"&gt;</pre>
</li>
<li>
<h4>Action 中使用 getDataSource 錯誤</h4>
<p>書上的範例以及網路上查到的資料都說在 Struts Action 中，可以透過 <strong>getDataSource(request).getConnection();</strong> 的方式來取得連接池的 Connection，實際上試了之後才會發現<span style="color: #ff0000;"><strong> 1.3 根本就沒有 getDataSource 這個方法</strong></span>，繞了好久才找到必須透過以下方式取得剛剛設定在 context.xml 中的 datasource。</p>
<pre class="brush: java; gutter: true">DataSource ds = (DataSource)new InitialContext().lookup("java:comp/env/jdbc/TestDB");
Connection conn = ds.getConnection();</pre>
</li>
<li>
<h4>Spring, Struts 接合</h4>
<p><span style="color: #000000;">接著設定 </span><strong style="color: #000000;">struts-config.xml</strong><span style="color: #000000;"> 加入 </span><strong style="color: #000000;">&lt;controller&gt;</strong><span style="color: #000000;"> 及 </span><strong style="color: #000000;">&lt;plug-in&gt;</strong><span style="color: #000000;"> 標籤，這邊家玩如果有 </span><strong style="color: #000000;">ClassNotFoundException</strong><span style="color: #000000;"> 一樣參考這篇文章第 2 點說明，Eclipse 有報錯要注意 tag 間的順序。</span></p>
<pre class="brush: xml; gutter: true">&lt;controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"&gt;&lt;/controller&gt;
&lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;
    &lt;set-property value="/WEB-INF/beans-conf.xml" property="contextConfigLocation"/&gt;
&lt;/plug-in&gt;</pre>
<p class="brush: xml; gutter: true">原本 struts-config.xml 中的 action 設定如下</p>
<pre class="brush: xml; gutter: true">&lt;action-mappings&gt;
    &lt;!-- 這種是預設到特定頁面的，為了隱藏 jsp --&gt;
    &lt;action path="/index" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/pages/index.jsp"&gt;&lt;/action&gt;
    &lt;!-- 接合 Spring 後的寫法，注意 action path (struts) 要跟 bean name (spring) 一致--&gt;
    &lt;action path="/hello2"&gt;
        &lt;forward name="helloUser" path="/WEB-INF/pages/footer.jsp"&gt;&lt;/forward&gt;
    &lt;/action&gt;
&lt;/action-mappings&gt;</pre>
<p class="brush: xml; gutter: true">Spring 設定 beans-conf.xml 如下 (範例參考自<a href="http://caterpillar.onlyfun.net/Gossip/SpringGossip/StrutsSpring.html " target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">良葛格學習筆記<span class="wpel-icon wpel-image wpel-icon-6"></span></a>，userChecker 及 HelloAction 請參考 <a href="http://caterpillar.onlyfun.net/Gossip/SpringGossip/StrutsSpring.html " target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">良葛格學習筆記<span class="wpel-icon wpel-image wpel-icon-6"></span></a>)</p>
<pre class="brush: xml; gutter: true">&lt;bean id="userChecker" class="com.hans.pojo.UserChecker"&gt;&lt;/bean&gt;
&lt;!-- bean name 要跟 struts 裡面設定的 action path 一致 --&gt;
&lt;bean name="/hello2" class="com.hans.struts.action.HelloAction2"&gt;
    &lt;property name="userChecker" ref="userChecker" /&gt;
&lt;/bean&gt;</pre>
</li>
<li><strong>Hibernate &#8211;&nbsp;getHibernateTemplate().find() 錯誤</strong><br />
小蛙讓 MemberDao 繼承&nbsp;HibernateDaoSupport 操作資料庫上比較方便，但透過 HibernateTemplate().find() 查找資料的時候(程式碼如下)</p>
<pre class="brush: java; gutter: true">List&lt;Member&gt; list = this.getHibernateTemplate().find("from Member member");
if(list.size() &gt; 0)
    return list.get(0);</pre>
<p>出現以下錯誤</p>
<pre class="brush: actionscript3; gutter: true">javax.servlet.ServletException: org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: member near line 1, column 29 [from com.hans.entity.Member member]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: member near line 1, column 29 [from com.hans.entity.Member member]</pre>
<p>把 <strong>from Member member</strong> 改成 <strong>from member</strong>，出現下面錯誤</p>
<pre class="brush: shell; gutter: true">javax.servlet.ServletException: org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: member near line 1, column 6 [from member]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: member near line 1, column 6 [from member]</pre>
<p>改成&nbsp;from Member 之後才可正常執行，後來在這篇文章看到原來 &#8230; <span style="color: #ff0000;"><strong>member</strong></span> 是保留字 &#8230; 難怪怎麼試都會錯，還以為自己哪裡有問題。</li>
</ol>
<p><strong>參考資料：</strong></p>
<ol>
<li>Struts+Spring+Hibernate项目框架构建中的问题及解决方法集锦（1） @ Morecans的专栏<br />
<address><a href="http://blog.csdn.net/morecans/article/details/1686416" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">http://blog.csdn.net/morecans/article/details/1686416<span class="wpel-icon wpel-image wpel-icon-6"></span></a></address>
</li>
<li>Spring Gossip: 在 Struts 中整合 Spring @ 良葛格學習筆記<br />
<address><a href="http://caterpillar.onlyfun.net/Gossip/SpringGossip/StrutsSpring.html" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">http://caterpillar.onlyfun.net/Gossip/SpringGossip/StrutsSpring.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a>&nbsp;</address>
</li>
<li>Hibernate 查詢數據時報錯，請問這是什麽原因呢？急死了！ @ JWorld@TW<br />
<address><a href="http://www.javaworld.com.tw/jute/post/view?bid=41&amp;id=171839" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">http://www.javaworld.com.tw/jute/post/view?bid=41&amp;id=171839<span class="wpel-icon wpel-image wpel-icon-6"></span></a></address>
</li>
</ol>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2418/struts-1-3-spring-3-2-hibernate-3-6-%e9%8c%af%e8%aa%a4%e7%ad%86%e8%a8%98/" data-wpel-link="internal">Struts 1.3 + Spring 3.2 + Hibernate 3.6 錯誤筆記</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2418/struts-1-3-spring-3-2-hibernate-3-6-%e9%8c%af%e8%aa%a4%e7%ad%86%e8%a8%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WinRAR解壓縮亂碼</title>
		<link>https://noter.tw/1810/winrar%e8%a7%a3%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc/</link>
					<comments>https://noter.tw/1810/winrar%e8%a7%a3%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sat, 07 Jan 2012 07:19:05 +0000</pubDate>
				<category><![CDATA[技巧分享]]></category>
		<category><![CDATA[zip]]></category>
		<category><![CDATA[壓縮檔]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[winrar]]></category>
		<guid isPermaLink="false">http://wazai.net/1810/winrar%e8%a7%a3%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc</guid>

					<description><![CDATA[<p>有時候下載回來的壓縮檔解壓縮的時候發生「檔案名稱、目錄名稱或磁碟區標籤語法錯誤。」，像小蛙最近在試kuAD的時候就是這樣，這篇紀錄一下怎麼處理這個錯誤。 小蛙下載完kuAD套件要解壓縮的時候發生以下的&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1810/winrar%e8%a7%a3%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc/" data-wpel-link="internal">WinRAR解壓縮亂碼</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>有時候下載回來的壓縮檔解壓縮的時候發生「檔案名稱、目錄名稱或磁碟區標籤語法錯誤。」，像小蛙最近在試kuAD的時候就是這樣，這篇紀錄一下怎麼處理這個錯誤。<br />
<span id="more-1810"></span><br />
小蛙下載完kuAD套件要解壓縮的時候發生以下的錯誤。<br />
<a href="https://noter.tw/wp-content/uploads/2012/01/winrar-1.png" data-wpel-link="internal"><img fetchpriority="high" decoding="async" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="winrar-1" src="https://noter.tw/wp-content/uploads/2012/01/winrar-1_thumb.png" alt="winrar-1" width="544" height="310" border="0"></a><br />
如果直接開啟壓縮檔的話發現裡面有一個檔名亂碼的檔案。<br />
<a href="https://noter.tw/wp-content/uploads/2012/01/winrar-2.png" data-wpel-link="internal"><img decoding="async" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="winrar-2" src="https://noter.tw/wp-content/uploads/2012/01/winrar-2_thumb.png" alt="winrar-2" width="544" height="537" border="0"></a><br />
直接把檔名亂碼的檔案重新命名成正常的檔名。<br />
<a href="https://noter.tw/wp-content/uploads/2012/01/winrar-3.png" data-wpel-link="internal"><img decoding="async" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="winrar-3" src="https://noter.tw/wp-content/uploads/2012/01/winrar-3_thumb.png" alt="winrar-3" width="544" height="537" border="0"></a><br />
再解壓縮一次就可以成功解出來囉！</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1810/winrar%e8%a7%a3%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc/" data-wpel-link="internal">WinRAR解壓縮亂碼</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/1810/winrar%e8%a7%a3%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>網頁壓縮亂碼 &#8211; Lighttpd gzip php, js, css, html, plain</title>
		<link>https://noter.tw/1748/%e7%b6%b2%e9%a0%81%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc-lighttpd-gzip-php-js-css-html-plain/</link>
					<comments>https://noter.tw/1748/%e7%b6%b2%e9%a0%81%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc-lighttpd-gzip-php-js-css-html-plain/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Thu, 29 Dec 2011 15:59:46 +0000</pubDate>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[compress]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[zlib.output_compression]]></category>
		<guid isPermaLink="false">http://wazai.net/1748/%e7%b6%b2%e9%a0%81%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc-lighttpd-gzip-php-js-css-html-plain</guid>

					<description><![CDATA[<p>蛙齋速度慢的原因雖然有很大部份是在VPS的速度，在沒有經費可以提高VPS花費的情況下，只好從網站的優化開始做起，小蛙在Google網站管理員工具中看到一個「網站效能」的選項，裡面提到了可以安裝Fire&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1748/%e7%b6%b2%e9%a0%81%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc-lighttpd-gzip-php-js-css-html-plain/" data-wpel-link="internal">網頁壓縮亂碼 &#8211; Lighttpd gzip php, js, css, html, plain</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>蛙齋速度慢的原因雖然有很大部份是在VPS的速度，在沒有經費可以提高VPS花費的情況下，只好從網站的優化開始做起，小蛙在<a href="https://www.google.com/webmasters/" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Google網站管理員工具<span class="wpel-icon wpel-image wpel-icon-6"></span></a>中看到一個「網站效能」的選項，裡面提到了可以安裝Firefox的plugin來測試網站的效能，小蛙點了安裝之後，瀏覽網頁的時候按下<span style="color: #ff0000;"><strong>F12</strong></span>就可以看到Firefox下面的Firebug最右邊多出了一欄「<a href="http://code.google.com/intl/zh-TW/speed/page-speed/download.html" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Page Speed<span class="wpel-icon wpel-image wpel-icon-6"></span></a>」。<br />
<span id="more-1748"></span><br />
測試之後的結果發現在「<strong><span style="color: #ff0000;">使用瀏覽器快取</span></strong>」跟「<strong><span style="color: #ff0000;">啟用壓縮功能</span></strong>」的部份是黃色驚嘆號，也就是還可以加強的部分。小蛙記得前一陣子就有啟用lighttpd的compress功能，在firebug下面也可以看到Content-Encoding: gzip了，為什麼還會在「啟用壓縮功能」的這個項目不符合呢？Google了一下發現lighttpd中設定的只有下面這些選項，沒有包含到PHP頁面。</p>
<pre>"text/plain", "text/html", "application/x-javascript", "text/css", "application/javascript", "text/javascript", "application/x-javascript"</pre>
<p>這篇文章小蛙記錄一下整個設定的流程，主要分成兩個部份，分別是lighttpd.conf以及php.ini。(參考資料：<a href="http://ching119.pixnet.net/blog/post/59675615-%E3%80%90%EF%BC%AC%E3%80%91%E8%A8%AD%E5%AE%9Agzip---lighttpd-%26-php" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">【Ｌ】設定Gzip &#8211; Lighttpd &amp; PHP @ Ching Wei : This is My Life &#8211; 工作、攝影、旅遊<span class="wpel-icon wpel-image wpel-icon-6"></span></a>)</p>
<h2><span style="color: #ff0000;">/etc/lighttpd/lighttpd.conf</span></h2>
<ol>
<li>首先在<span style="color: #ff0000;"><strong>server.modules</strong></span>的部份要打開<span style="color: #ff0000;"><strong>&#8220;mod_compress&#8221;</strong></span>的選項，把前面的「#」拿掉就可以了。</li>
<li>設定快取儲存資料夾。
<pre>compress.cache-dir = "/var/cache/lighttpd/compress/"</pre>
</li>
<li>設定要壓縮的類型。
<pre>compress.filetype = ("text/plain", "text/html", "application/x-javascript", "text/css", "application/javascript", "text/javascript", "application/x-javascript")</pre>
</li>
<li>重啟lighttpd，就可以透過firebug在剛剛設定的像css, javascript … 等檔案類型中看到「Accept-Encoding: gzip」的字樣。</li>
</ol>
<h2><span style="color: #ff0000;">/etc/php5/cgi/php.ini</span></h2>
<ol>
<li>這個php.ini的檔案可能會在不同路徑下，找不到的話可以下「locate php.ini」找找看，如果還是找不到可以在下locate之前先下「updatedb」的指令更新。</li>
<li>找到「<span style="color: #ff0000;"><strong>;zlib.output_compression = On</strong></span>」的字樣，把前面的「<span style="color: #ff0000;"><strong>;</strong></span>」拿掉。</li>
<li>存檔離開後，重新啟動lighttpd。</li>
</ol>
<p>透過以上設定就可以讓lighttpd跟php都使用gzip壓縮功能，但是小蛙設定完之後卻發現IE跟Chrome開啟頁面的時候是呈現亂碼的狀態，小蛙百思不得其解，Google了一會兒，<a href="http://ching119.pixnet.net/blog/post/59676021-%E3%80%90%E7%A8%8B%E5%BC%8F%E3%80%91php---gzencode-%E5%BE%8C%E8%AE%8A%E4%BA%82%E7%A2%BC-!!" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">【程式】PHP &#8211; gzencode 後變亂碼 !! @ Ching Wei : This is My Life &#8211; 工作、攝影、旅遊<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 和 <a href="http://wenwen.soso.com/z/q202202824.htm" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">PHP静态页面GZIP压缩乱码问题 @ SOSO問問<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 有提到會變成亂碼是做了兩次gzip造成的，小蛙改了半天，卻始終都還是亂碼(不同的theme可能會有不同情況)。<br />
之後小蛙把矛頭指向Hyper Cache這個套件(因為發現這個套件下面有很多跟gzip相關的字眼)，查了一下發現，原來在安裝完Hyper Cache的時候，預設狀態中「Enable compression」是開啟的，所以造成壓縮兩次的情況，只要把Enable compression關掉就可以了。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1748/%e7%b6%b2%e9%a0%81%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc-lighttpd-gzip-php-js-css-html-plain/" data-wpel-link="internal">網頁壓縮亂碼 &#8211; Lighttpd gzip php, js, css, html, plain</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/1748/%e7%b6%b2%e9%a0%81%e5%a3%93%e7%b8%ae%e4%ba%82%e7%a2%bc-lighttpd-gzip-php-js-css-html-plain/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JavaScript列印頁面及mailto寄信亂碼</title>
		<link>https://noter.tw/1704/javascript%e5%88%97%e5%8d%b0%e9%a0%81%e9%9d%a2%e5%8f%8amailto%e5%af%84%e4%bf%a1%e4%ba%82%e7%a2%bc/</link>
					<comments>https://noter.tw/1704/javascript%e5%88%97%e5%8d%b0%e9%a0%81%e9%9d%a2%e5%8f%8amailto%e5%af%84%e4%bf%a1%e4%ba%82%e7%a2%bc/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 28 Dec 2011 09:42:17 +0000</pubDate>
				<category><![CDATA[網頁前端]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[mailto]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[encodeURI]]></category>
		<category><![CDATA[java.net.URLEncoder.encode]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[列印]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=1704</guid>

					<description><![CDATA[<p>接續前兩篇文章，從FancyBox到達申請表頁面後，對方要求可以透過(1)印表機列印申請表，(2)透過email寄送申請表。這篇文章小蛙要來說明怎麼透過JavaScript列印頁面以及用email寄送&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1704/javascript%e5%88%97%e5%8d%b0%e9%a0%81%e9%9d%a2%e5%8f%8amailto%e5%af%84%e4%bf%a1%e4%ba%82%e7%a2%bc/" data-wpel-link="internal">JavaScript列印頁面及mailto寄信亂碼</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>接續前兩篇文章，從FancyBox到達申請表頁面後，對方要求可以透過(1)印表機列印申請表，(2)透過email寄送申請表。這篇文章小蛙要來說明怎麼透過JavaScript列印頁面以及用email寄送申請表，小蛙在處理mailto的時候遇到亂碼問題卡了一天，也會在這篇文章中說明。<br />
<span id="more-1704"></span></p>
<ul>
<li><strong><span style="color: #ff0000;">JavaScript列印頁面</span></strong></li>
</ul>
<p style="padding-left: 30px;">JavaScript列印頁面非常簡單，只需要一行就可以達成，更多詳細資料可以參考<a href="http://nievor.wordpress.com/2008/07/31/javascript_print_preview/" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Javascript呼叫IE列印以及預覽列印的方法@小峰峰的筆記本<span class="wpel-icon wpel-image wpel-icon-6"></span></a>。</p>
<pre style="padding-left: 30px;">&amp;lt;input type="button" onclick="&lt;span style="color: #ff0000;"&gt;&lt;strong&gt;print()&lt;/strong&gt;&lt;/span&gt;" value="列印申請表"&amp;gt;</pre>
<ul>
<li><span style="color: #ff0000;"><strong>JavaScript寄送電子郵件</strong></span></li>
</ul>
<p style="padding-left: 30px;">JavaScript寄送電子郵件指令也非常簡單，但是隱藏的問題其實還蠻多的，小蛙先介紹寄送電子郵件的指令。</p>
<pre style="padding-left: 30px;">&lt;input type="button" onclick="sendMail(true);" value="使用Big5寄送(Outlook)"&gt;
&lt;input type="button" onclick="sendMail(false);" value="使用UTF-8寄送"&gt;</pre>
<p style="padding-left: 30px;">小蛙用了sendMail()來讓使用者自行選擇要用哪種方式寄送等等再說明原因。JavaScript程式碼寄信程式碼如下，因為小蛙只用到簡單的寄件人、主旨及內容，更多詳細設定請參閱<a href="http://ant4html.blogspot.com/2009/01/mailto.html" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">用mailto寄信 @ 螞蟻的HTML<span class="wpel-icon wpel-image wpel-icon-6"></span></a>：</p>
<pre style="padding-left: 30px;">&lt;script type="text/javascript"&gt;
  // 基本寄信，沒有判斷編碼
  function sendMail(){
    var text_title = "這是一封透過JavsScript寄送的郵件主旨";
    var text_body  = "這是內文的部分。";
    sendmail = "mailto:xxxx@gmail.com?subject=" + text_title;
    sendmail += "&amp;body=" + text_body;
    location.href = sendmail;
  }
&lt;/script&gt;</pre>
<p style="padding-left: 30px;">如果要用到換行的話，必須使用<span style="color: #ff0000;"><strong>%0A</strong></span>。這個範例可能有些人跑起來是OK的，有些人卻得到亂碼，接下來小蛙要記錄一下自己的做法，也許有更好的做法，但時間緊迫只好先用這種特殊方式來解決。</p>
<ul>
<li><span style="color: #ff0000;"><strong>JavaScript mailto 亂碼解決</strong></span></li>
</ul>
<p style="padding-left: 30px;">目前小蛙找不到可以自動判斷要用什麼編碼的方法，也就是像上面的兩個按鈕，透過Big5寄送，或是透過UTF-8寄送，當小蛙以為就只有那麼簡單的時候又發現還要多一個條件判斷IE。Microsoft Outlook或Outlook Express會亂碼的原因是Outlook<strong><span style="color: #ff0000;">只能接受BIG5的編碼</span></strong>，但透過頁面location.href引導過去的參數會變成<span style="color: #ff0000;"><strong>UTF-8</strong></span>，所以不管怎麼試只要編碼不是BIG5在Outlook就一定會出現亂碼，但又找不到方法可以判斷客戶端在點了按鈕之後所呼叫的是Outlook還是透過Webmail寄送，像小蛙的電腦點了之後會跳出瀏覽器，透過gmail送出郵件，而不是使用Outlook。</p>
<p style="padding-left: 30px;">要確保送出的資料一定是用BIG5來編碼，在Java可以透過以下方式編碼。</p>
<pre style="padding-left: 30px;">java.net.URLEncoder.encode("一定要轉成BIG5編碼", "BIG5");</pre>
<p style="padding-left: 30px;">小蛙直接以自己的例子來說明，所有的頁面都是透過UTF-8編碼，包括使用者輸入的表單也是，這邊卡了一個問題是java.net.URLEncoder.encode()這個是Java的function，所以必須要在Server端才可以使用，也就是說使用者輸入完的資料沒有辦法直接透過java.net.URLEncoder.encode()來處理，這邊小蛙只好把所有的參數POST到另一頁sendMail.jsp專門處理亂碼及送出，以下是sendMail.jsp中Java編碼的部分。</p>
<pre style="padding-left: 30px;">String applyName = request.getParameter("applyName") == null ? "" : new String(request.getParameter("applyName").getBytes("ISO-8859-1"), "UTF-8");
String applyNameB = java.net.URLEncoder.encode("名稱："&nbsp;&nbsp; + applyName).replaceAll("[+]", " ");</pre>
<p style="padding-left: 30px;">編碼邏輯部分如下(以下是小蛙的dirty code，JavaScript跟Java夾雜，硬要分還是可以分的開，要怎麼處理就看自己的規畫囉！)：</p>
<pre style="padding-left: 30px;">&amp;lt;script type="text/javascript"&amp;gt;
&nbsp; var text_title = "&amp;lt;%=title%&amp;gt;";
&amp;lt;%
&nbsp; //也就是按下上面的「使用Big5寄送(Outlook)」
&nbsp; if("true".equals(big5)){
%&amp;gt;
&nbsp; &nbsp; &nbsp; // 這邊無論是哪個瀏覽器都務必要使用 &lt;span style="color: #ff0000;"&gt;&lt;strong&gt;big5&lt;/strong&gt;&lt;/span&gt; 送出
&nbsp;&nbsp;&nbsp; &nbsp; text_title = "&amp;lt;%=title&lt;span style="color: #ff0000;"&gt;&lt;strong&gt;B&lt;/strong&gt;&lt;/span&gt;%&amp;gt;";
&nbsp;&nbsp; &nbsp;&nbsp; text_body&nbsp; = "%0A" + ("&amp;lt;%=applyName&lt;span style="color: #ff0000;"&gt;&lt;strong&gt;B&lt;/strong&gt;&lt;/span&gt;%&amp;gt;");
&nbsp; &nbsp; &nbsp; ....
&amp;lt;%
&nbsp; }else{
  &nbsp;&nbsp;  if("IE".equals(browser)){
%&amp;gt;
    &nbsp;&nbsp;    // 如果瀏覽器是IE，還要額外做encodeURI處理
      &nbsp;&nbsp;  text_title = &lt;strong&gt;&lt;span style="color: #ff0000;"&gt;encodeURI&lt;/span&gt;&lt;/strong&gt;(text_title);
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;text_body&nbsp; = "&lt;span style="color: #ff0000;"&gt;&lt;strong&gt;%0A&lt;/strong&gt;&lt;/span&gt;" + &lt;strong&gt;&lt;span style="color: #ff0000;"&gt;encodeURI&lt;/span&gt;&lt;/strong&gt;("名稱：&amp;lt;%=applyName%&amp;gt;");
  &nbsp;&nbsp;      ....
&amp;lt;%
    &nbsp; }else{
%&amp;gt;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Firefox, Chrome直接傳送即可
&nbsp;&nbsp;        text_title = (text_title);
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;text_body&nbsp; = "&lt;span style="color: #ff0000;"&gt;&lt;strong&gt;%0A&lt;/strong&gt;&lt;/span&gt;" + ("物件名稱：&amp;lt;%=applyName%&amp;gt;");
&amp;lt;%
    &nbsp; }
&nbsp; }
%&amp;gt;
&nbsp; mail_str = "mailto:xxxxx@gmail.com?subject=" + text_title;
&nbsp; mail_str += "&amp;amp;body=" + text_body;
&nbsp; location.href = mail_str;
&amp;lt;/script&amp;gt;</pre>
<p>好吧！還有時間的話再來看看要不要把這些Code清乾淨一些&#8230;天啊！Web工程師最怕看到的應該就是亂碼，每次一遇到亂碼問題就會卡一段時間 &#8230; 還是希望瀏覽器的行為模式可以統一呀！上面的方法可以「手動」解決亂碼問題，讓使用者自己點選是否透過Outlook或是透過Webmail傳送，而在瀏覽器的特殊行為上小蛙也做了判斷，最有問題的IE特別隔離出來，希望之後能看到更好的做法&gt;&lt;。<br />
這方法有一些不足的地方<br />
(1)程式碼太醜，Java跟JavaScript全都混在一起(剛上面提到可以拆的開!!!)<br />
(2)沒辦法做到完全自動判斷，還要讓使用者點選<br />
(3)小蛙只測試了IE8, FF7.0.1, Chrome 16.0.912.63<br />
(4)如果使用者預設瀏覽器是IE，而透過非IE瀏覽器去點選寄送mail，有些情況可以，有些情況不行，這個部份處理起來真的沒完沒了，真正遇到再說了 = =<br />
打完收工，繼續處理其他工作，希望可以不要抱著工作跨年。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/1704/javascript%e5%88%97%e5%8d%b0%e9%a0%81%e9%9d%a2%e5%8f%8amailto%e5%af%84%e4%bf%a1%e4%ba%82%e7%a2%bc/" data-wpel-link="internal">JavaScript列印頁面及mailto寄信亂碼</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/1704/javascript%e5%88%97%e5%8d%b0%e9%a0%81%e9%9d%a2%e5%8f%8amailto%e5%af%84%e4%bf%a1%e4%ba%82%e7%a2%bc/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
