2010年7月14日 星期三

[Java] 檔案刪除 File.delete()

application.getRealPath("")為抓取真實路徑

File file = new File(application.getRealPath("abc.txt")); //jsp user
或是
File file = new File("c:/abc.txt")

file.delete();

2010年7月8日 星期四

[Jsp] 檔案上傳(org.apache.commons.fileupload)

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

List  items = upload.parseRequest(request);
   
   Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (item.isFormField()) {
           //一般的request item
            String name = item.getFieldName();
            String value = item.getString();
            out.println(name + "=" + item.getString("UTF-8"));
        } else {
            // 如果是上傳檔案, 就在這接收
            //processUploadedFile(item);
            String fieldName = item.getFieldName();
            String fileName = item.getName();
            String contentType = item.getContentType();
            boolean isInMemory = item.isInMemory();
            long sizeInBytes = item.getSize();
            out.println("fieldName="+fieldName+"");
            out.println("fileName="+fileName+"");
            out.println("contentType="+contentType+"");
            out.println("isInMemory="+isInMemory+"");
            out.println("sizeInBytes="+sizeInBytes+"");
            if (fileName != null && !"".equals(fileName)) {
                fileName= FilenameUtils.getName(fileName);
                out.println("fileName saved="+fileName+"");
                File uploadedFile = new File(saveDirectory, fileName);
                item.write(uploadedFile);
            }
        }
    }           

2010年7月7日 星期三

[Hibernate] 使用hibernate會使用到的jar檔

hibernate-distribution-3.5.3-Final  require lib
antlr-2.7.6.jar
c3po-0.9.1.2.jar
cglib-2.2.jar
commons-collections-3.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.0.jar
slf4j-jcl-1.6.0.jar

有用到ehcach才使用ehcach
有用到c3po才使用c3po

[Hibernate] 使用hibernate會遇到的slf4j錯誤

at org.slf4j.impl.JCLLoggerAdapter.(JCLLoggerAdapter.java:49)
at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:70)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:103)
at org.hibernate.cfg.Configuration.(Configuration.java:151)
at com.xxxxxx.SessionFactory(自訂的SessionFactory).(SessionFactory.java:xxx)

解決方法是更新"slf4j-jcl.jar"

http://www.slf4j.org/codes.html#jclDelegationLoop