2008年7月7日 星期一

Struts Download File Example

public ActionForward doViewAttachFile(ActionMapping aMapping, ActionForm aForm,
HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception {
Efp136Form efp136Form = (Efp136Form) aForm;
Efp136m efp136m = (Efp136m) efp136Form.getEntity();

String fileName = URLEncoder.encode(efp136m.getAttachFileName(), "UTF-8");
aResponse.setHeader("Content-disposition", "attachment; filename=" + fileName);
aResponse.setContentType(efp136m.getAttachFileType());
aResponse.setContentLength(efp136m.getAttachFileSize().intValue());

ServletOutputStream servletOutputStream = aResponse.getOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(efp136m.getAttachFileData());
BufferedInputStream bufferedInputStream = new BufferedInputStream(byteArrayInputStream);
byte[] bytes = new byte[2048];
while (bufferedInputStream.read(bytes) != -1) {
bufferedOutputStream.write(bytes);
}

servletOutputStream.flush();
byteArrayInputStream.close();
servletOutputStream.close();
return aMapping.findForward(VIEW_ATTACH_FILE_SUCCESS);
}

2008年7月6日 星期日

根據hibernate configuration file建立table schema

程式範例如下:

package mytest;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class SchemaExportTester {

public static void main(String[] args) {
new SchemaExportTester().createSchema();
}

private void createSchema() {
Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.setOutputFile("schema.sql");
schemaExport.create(true, true);
System.out.print("ok");
}
}


其中
schemaExport.create(true, true);
第一個參數是表示是否要產生schema.sql,第二個參數是表示是否要在database立建立table。

2008年6月2日 星期一

MySQL Character Encoding Configuration

It's common practice that the MySQL configuration file, in *nix systems is located in /etc/mysql/my.cnf
in windows, which is located in %MYSQL_HOME%\my.ini

[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
character-set-server = utf8
collation-server = utf8_general_ci

XWiki encoding

web.xml:

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.xpn.xwiki.web.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>


xwiki.cfg:

xwiki.encoding = UTF-8

XWiki: XWikiPreferences:
Default Language:en

2008年5月20日 星期二

用Windows Live Writer試試post

windows live write

還不錯用耶

2008年2月14日 星期四

Tomcat 5.5中設定JNDI resource的方法

1. $TOMCAT_HOME/conf/server.xml:
<GlobalNamingResources>
...
<Resource name="bean/CalendarFactory"
type="java.util.GregorianCalendar"
auth="Container"
factory="org.apache.naming.factory.BeanFactory"/>
</GlobalNamingResources>

2. $TOMCAT_HOME/conf/context.xml
<Context>
...
<Resource name="jdbc/AboveeDB" auth="Container"
type="javax.sql.DataSource" username="pp50" password="pp50"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://robbibm:1433/pp50p2"
maxActive="8" maxIdle="4"/>
</Context>

3. $TOMCAT_HOME/conf/web.xml
</web-app>
...
<resource-env-ref>
<description>
Fake up a Factory for Calendar objects
</description>
<resource-env-ref-name>
bean/CalendarFactory
</resource-env-ref-name>
<resource-env-ref-type>
java.util.GregorianCalendar
</resource-env-ref-type>
</resource-env-ref>
</web-app>

4. JSP client程式片斷
<%
javax.naming.Context ctx = new javax.naming.InitialContext( );
java.util.Calendar calendar = (java.util.GregorianCalendar)
ctx.lookup("java:comp/env/bean/CalendarFactory");
out.println(calendar.getTime());
%>

2008年2月4日 星期一

note

eclipse font: Georgia 16, Microsoft Sans Serif 14
package: nimble, brisk