%@ page contentType="text/html; charset=big5"%> <%@ page import="java.io.*" %> <%@ page import="java.util.*" %> <%@ page import="javax.imageio.*" %> <%@ page import="java.awt.image.*" %> <%@ page import="imageinfo.*" %> <%! private static class DM{ String id; String title; String content; String photoURL; int width; int height; } public String readFile(File file){ FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; StringBuffer sb = new StringBuffer(); try{ fis = new FileInputStream(file); isr = new InputStreamReader(fis, "Big5"); br = new BufferedReader(isr); String line = null; while ( (line = br.readLine()) != null) sb.append(line); } catch(Exception e){ sb.append(e.getMessage()); } finally{ try{ if ( br != null ) br.close(); if ( isr != null ) isr.close(); if ( fis != null ) fis.close(); } catch(IOException e){} } return sb.toString(); } public String getDMById(ServletContext application, String id) { File dmFolder = new File(application.getRealPath(".")); return null; } public String findPhotoURL(ServletContext application, File file) throws Exception{ File[] fileNames = file.listFiles(new FilenameFilter(){ public boolean accept(File file, String s) { String ext = s.toLowerCase(); return ext.endsWith("jpg") || ext.endsWith("png") || ext.endsWith("gif") ; } }); if (fileNames.length != 0){ //System.getProperties().put("file.encoding", "UTF-8"); //throw new Exception(fileNames[0].getAbsolutePath()); return file.getName() +"/"+new String(fileNames[0].getName()); } else{ return ""; } } public Map getAllDM(ServletContext application) throws Exception{ File dmFolder = new File(application.getRealPath("./DM")); TreeMap dms = new TreeMap(new Comparator(){ public int compare(Object o, Object o1) { String a = (String)o; String b = (String)o1; if (a == null){ if (b == null){ return 0; } else { return -1*b.compareTo(a); } } else{ return -1*a.compareTo(b); } } }); File[] files = dmFolder.listFiles(); for (int i = 0; i < files.length; i++){ File file = files[i]; if (file.exists() && file.isDirectory()){ DM dm = new DM(); dm.id = file.getName(); dm.title = readFile(new File(file.getPath()+"/title.txt")); dm.content = readFile(new File(file.getPath()+"/content.txt")); dm.photoURL = findPhotoURL(application, file); setSize(application,dm); dms.put(dm.id, dm); } } return dms; } public boolean setSize(ServletContext application, DM dm) throws Exception{ FileInputStream file = null; try{ file = new FileInputStream(application.getRealPath("./DM")+"/" + dm.photoURL); //BufferedImage bi = ImageIO.read(file); //return bi.getWidth() < bi.getHeight(); ImageInfo ii = new ImageInfo(); ii.setInput(file); if (!ii.check()) { System.err.println("Not a supported image file format."); throw new Exception("check failed"); } else { dm.width = ii.getWidth(); dm.height = ii.getHeight(); int width = Math.min(dm.width, 800); int height = Math.min(dm.height, 600); double ratio = Math.min(width/(double)dm.width, height/(double)dm.height ); dm.width = (int)(ratio*dm.width); dm.height = (int)(ratio*dm.height); return ii.getWidth() < ii.getHeight(); // there are other properties, check out the API documentation } } finally{ if ( file != null) file.close(); } } %> <% Map dmList = getAllDM(application); %>
|
|||||||||||||||||||||||
©2005.Mae Nam Thai Food. All rights reserved. |