有馬総一郎のブログ

(彼氏の事情)

2013年06月29日 19:57:00 JST - 2 minute read - Subsonic

Subsonic、ファイルダウンロードをUTF-8で

Subsonicの日本語ファイルのダウンロードでの文字化け。チョット、最近SUPER JUNIORとかいう韓流を借りてきたので、いい加減、マジでUTF-8対応目指すことにした。実際に、ダウンロードすることは少ないんだけど・・・

versionは4.8。

あれこれ、画像のスクショ撮ったり、気合入れて書くとシンドイからテキトーに。

例によって

DownloadController.java

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import java.net.URLEncoder;

をインポート

メソッド downloadFile
引数にHttpServletRequest request追加。

DownloadController.java

        // 2013/06/16 mod character corruption start
//      response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + '\"');
        String filename = "";
        String agent = request.getHeader("USER-AGENT");
        if (agent != null && agent.indexOf("MSIE") != -1)
        {
            filename = URLEncoder.encode(file.getName(), "UTF8");
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment;filename=" + filename);
        }
        else if ( agent != null && agent.indexOf("Mozilla") != -1)
        {
            response.setCharacterEncoding("UTF-8");
            filename = MimeUtility.encodeText(file.getName(), "UTF8", "B");
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + '\"');
        }
        // 2013/06/16 mod character corruption end

downloadFiles
引数にHttpServletRequest request追加。

DownloadController.java

        // 2013/06/16 mod character corruption start
//      String zipFileName = dir.getName() + ".zip";
        String zipFileName = "";
         String agent = request.getHeader("USER-AGENT");
        if (agent != null && agent.indexOf("MSIE") != -1)
        {
            zipFileName = URLEncoder.encode(dir.getName(), "UTF8") + ".zip";
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment; filename=" + zipFileName);
        }
        else if ( agent != null && agent.indexOf("Mozilla") != -1)
        {
            response.setCharacterEncoding("UTF-8");
            zipFileName = MimeUtility.encodeText(dir.getName(), "UTF8", "B") + ".zip";
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + '\"');
        }
        // 2013/06/16 mod character corruption end
        LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
        status.setFile(dir);
        // 2013/06/16 del character corruption start
//      response.setContentType("application/x-download");
//      response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
        // 2013/06/16 del character corruption end

downloadDirectory
引数にHttpServletRequest request追加。

DownloadController.java

        // 2013/06/16 mod character corruption start
//      String zipFileName = file.getName() + ".zip";
        String zipFileName = "";
         String agent = request.getHeader("USER-AGENT");
        if (agent != null && agent.indexOf("MSIE") != -1)
        {
            zipFileName = URLEncoder.encode(file.getName(), "UTF8") + ".zip";
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment; filename=" + zipFileName);
        }
        else if ( agent != null && agent.indexOf("Mozilla") != -1)
        {
            response.setCharacterEncoding("UTF-8");
            zipFileName = MimeUtility.encodeText(file.getName(), "UTF8", "B") + ".zip";
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + '\"');
        }
        // 2013/06/16 mod character corruption end
        LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
        // 2013/06/16 del character corruption start
//      response.setContentType("application/x-download");
//      response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + '"');
        // 2013/06/16 del character corruption end

downloadFiles
引数にHttpServletRequest request追加。

DownloadController.java

        // 2013/06/16 mod character corruption start
         String agent = request.getHeader("USER-AGENT");
        if (agent != null && agent.indexOf("MSIE") != -1)
        {
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment; filename=\"" + zipFileName);
        }
        else if ( agent != null && agent.indexOf("Mozilla") != -1)
        {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + '\"');
        }
        // 2013/06/16 mod character corruption end
        LOG.info("Starting to download '" + zipFileName + "' to " + status.getPlayer());
        // 2013/06/16 del character corruption start
//      response.setContentType("application/x-download");
//      response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + '"');
        // 2013/06/16 del character corruption end

あんまりキチンと考えてないので、ツッコミ多々ありましょうが許してください。OSはWindows, Ubuntu、ブラウザはIE(10)とFirefox(22)では試した。で、成功。他は使ってないので知らん。 日本語、韓国語ともに文字化けせず。 コンパイル時にpom.xmlの編集とか、必要だったけど、チョットあんま覚えてないです。 とりあえず。