2015年3月31日火曜日

Javaでファイルの拡張子を簡単に得る方法

Javaでファイルの拡張子を簡単に得るには、FilenameUtilsを使う。


import org.apache.commons.io.FilenameUtils;


String ext = FilenameUtils.getExtension(fileName);

http://stackoverflow.com/questions/924394/how-to-get-file-name-without-the-extension

2015年3月24日火曜日

libapache2-mod-fastcgiがApacheのモジュールとしてロードされていない時の対処方法

Apacheでfastcgiを使っていて、Ubuntuのリリースアップグレードなどのきっかけで、FastCgiServerのシンタックスがiipsrv.confで使えませんというエラーが出た。

/etc/apache2/mods-enabled/iipsrv.conf

---> FastCgiServer .......    ------>ここがエラーになる。

1.fastcgiがApacheのモジュールとしてロードされていない事を確認

  %> sudo apache2ctl -M

2./etc/apt/source.listでfastcgiが含まれるmultiverseに編集
3.sudo apt-get udpate
4.sudo apt-get install libapache2-mod-fastcgi
5.sudo a2enmod fastcgi でfastcgiをApacheのモジュールとしてロード
6.sudo apachectl configtest で設定ファイルのシンタックスが正しいかチェック
7.sudo /etc/init.d/apache2 restart

これでfastcgi_module (shared)が出ればOK。

きっかけはUbuntuのリリース・アップグレードで起こったので、注意しないなと思いました。こういうところは気づきにくい。。。


参考
https://help.ubuntu.com/community/HelpOnInstalling/FastCgi
https://www.howtoforge.com/how-to-set-up-apache2-with-mod_fcgid-and-php5-on-ubuntu-10.04
http://serverfault.com/questions/599707/e-package-libapache2-mod-fastcgi-has-no-installation-candidate-debian-ser
http://serverfault.com/questions/395139/cant-install-fastcgi-ubuntu-server-package-libapache2-mod-fastcgi-is-not-availa
http://www.binarytides.com/install-apache-php-mod-fastcgi-ubuntu/
https://wiki.archlinux.org/index.php/Apache_and_FastCGI
http://superuser.com/questions/284898/how-to-check-which-apache-modules-are-enabled-installed
http://www.unixmen.com/install-apache2-fastcgi-ubuntu-server-14-0413-10/


2015年3月20日金曜日

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bagsが発生した場合の対処方法

Hibernateでorg.hibernate.loader.MultipleBagFetchException発生した場合、Listを使っているプロパティをSetに変えてやれば問題は解消される。

原因については未だ調査中。。。

http://stackoverflow.com/questions/24675340/org-hibernate-loader-multiplebagfetchexception-cannot-simultaneously-fetch-mult

Apache POIでエクセルファイルを読み込むサンプルコード


         ByteArrayInputStream bis = null;
            Workbook workbook = null;
            try {
                bis = new ByteArrayInputStream(file.getBytes());            
                
                if (file.getOriginalFilename().endsWith("xls")) {
                    workbook = new HSSFWorkbook(bis);
                } else if (file.getOriginalFilename().endsWith("xlsx")) {
                    workbook = new XSSFWorkbook(bis);
                } else {
                    throw new IllegalArgumentException("Received file does not "
                    + "have a standard excel extension.");
                }
                
                // Iterate rows
                for ( Row row : workbook.getSheetAt(0) ) {                

                }
                
                ....
                
              }


http://stackoverflow.com/questions/14740727/upload-excel-file-into-database-using-apache-poi-and-spring-framework

Spring MVC 3.1ではRedirectAttributesが便利

Spring MVC 3.1ではリダイレクト先のページに値を受け渡しするのにRedirectAttributesが使えるので便利。


redirectAttributes.addFlashAttribute("key", value);

そしてリダイレクトするには、


return "redirect:someView";

http://www.tikalk.com/redirectattributes-new-feature-spring-mvc-31/

Tomcatで8080から8443にフォワードするには?

web.xmlを

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Someapp</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>

    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

server.xmlを

<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
          maxThreads="150" scheme="https" secure="true"
          clientAuth="false" sslProtocol="TLS" 
          keystoreFile="/somepath/.keystore"
          keystorePass="somepass"/>


JavaScriptでparseIntの戻り値がNaNの場合の対処方法

var s = '';

var num = parseInt(s) || 0;

http://stackoverflow.com/questions/6736476/javascript-parseint-return-nan-for-empty-string

jQueryで兄弟(同列)のDomエレメントにアクセスするには?

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li class="third-item">list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>

$( "li.third-item" ).siblings().css( "background-color", "red" );


http://api.jquery.com/siblings/

POIでエクセルのセルが空かどうか調べるには?

Cell c = row.getCell(3);
 if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
    // This cell is empty
 }

http://stackoverflow.com/questions/15764417/how-to-check-if-an-excel-cell-is-empty-using-poi

Spring Securityでベーシック認証をするには?

applicationContext.xmlに以下を書き込む。


<security:http auto-config='true'>
<security:intercept-url pattern="/somepath/*" access="ROLE_USER"/>
<security:intercept-url pattern="/somepath/**" access="ROLE_USER"/>
     <security:http-basic />
</security:http>

<security:authentication-manager>
   <security:authentication-provider>
        <security:user-service>
        <security:user name="someid" password="somepass" authorities="ROLE_USER" />
        </security:user-service>
    </security:authentication-provider>

</security:authentication-manager>

Javaでリモートからの画像ファイルをダウンロードするには?



    response.setHeader("Content-Disposition""filename=\""+ fileName + "\"");
    response.setContentType("image/jpeg");

    URL url = new URL(fileLink);
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();


ヘッダーの設定をダウンロードするファイルによって設定する。IOUtilsでストリームをコピーするのは便利。

Ubuntuでシステムクロックを正常にするにはntpdateを使う

ntpdate ntp.ubuntu.com

https://help.ubuntu.com/10.04/serverguide/NTP.html

mysqldumpでデータのみをダンプする場合

$ mysqldump -uroot -p --no-create-info testdb > dump.sql

http://zetcode.com/databases/mysqltutorial/exportimport/

JavaでURLエンコードをする場合、特定の文字に注意する。

特定の文字を別途変換する必要がある。

String encodeStr = URLEncoder.encode("*-_", "UTF-8");
encodeStr = encodeStr.replace("*", "%2a");
encodeStr = encodeStr.replace("-", "%2d");

String encodeStr = URLEncoder.encode("a a", "UTF-8");
encodeStr = encodeStr.replace("+", "%20");

http://javatechnology.net/java/url-encode/

Apacheで特定のIPアドレスからのみリクエストを受け付ける場合

Allow From xxxx.xxxx.xxxx.xxxx を使う。

<Directory /APP>

    Order Allow,Deny

    Allow from 160.120.25.65
    Allow from 127.0.0.0/8

</Directory>

http://stackoverflow.com/questions/714332/allow-request-coming-from-specific-ip-only

Apacheでサブディレクトリをリバースプロキシさせたくない場合

サブディレクトリをリバースプロキシさせたくない場合は、!を使う。

ProxyPass /mirror/foo/i !
ProxyPass /mirror/foo http://backend.example.com


http://httpd.apache.org/docs/2.2/ja/mod/mod_proxy.html#proxypass
http://serverfault.com/questions/391457/how-does-apache-merge-multiple-matching-location-sections