1 package org.nextime.ion.common; 2 3 import java.awt.Color ; 4 import java.awt.Graphics ; 5 import java.awt.Image ; 6 import java.awt.image.BufferedImage ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.IOException ; 10 11 import javax.activation.FileTypeMap ; 12 import javax.imageio.ImageIO ; 13 import javax.servlet.ServletException ; 14 import javax.servlet.http.HttpServlet ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 18 import org.nextime.ion.frontoffice.bean.ResourceXmlBean; 19 import org.nextime.ion.frontoffice.bean.Resources; 20 21 public class ResourceServlet extends HttpServlet { 22 23 public static String relativePath = null; 24 public static String configPath = null; 25 26 29 protected void doGet( 30 HttpServletRequest request, 31 HttpServletResponse response) 32 throws ServletException , IOException { 33 34 String qs = request.getPathInfo().substring(1); 35 36 String resourcesId = qs.substring(0, qs.indexOf("/")); 37 String resourceId = qs.substring(qs.indexOf("/") + 1); 38 39 String path = null; 41 try { 42 ResourceXmlBean bean = 43 Resources.getResourceXmlBean(this, resourcesId); 44 path = bean.getDirectory(); 45 } catch (Exception e) { 46 throw new ServletException (e); 47 } 48 String realPath = getServletContext().getRealPath("/"); 49 File resources = new File (realPath, ResourceServlet.relativePath); 50 File tresources = new File (resources, path); 51 File tfile = new File (tresources, resourceId); 52 53 if (request.getParameter("width") != null 54 || request.getParameter("height") != null) { 55 56 int w = Integer.parseInt(request.getParameter("width") + ""); 57 int h = Integer.parseInt(request.getParameter("height") + ""); 58 59 60 File cache = new File (resources, "cache"); 61 File possibleCached = 62 new File (cache, w + "_" + h + "_" + tfile.getName() + ".jpg"); 63 64 String mimeType = 65 FileTypeMap.getDefaultFileTypeMap().getContentType(possibleCached); 66 response.setContentType(mimeType); 67 68 if (possibleCached.exists()) { 69 byte[] buffer = new byte[(int) possibleCached.length()]; 71 FileInputStream fis = new FileInputStream (possibleCached); 72 fis.read(buffer); 73 fis.close(); 74 response.getOutputStream().write(buffer); 75 response.getOutputStream().flush(); 76 response.getOutputStream().close(); 77 } else { 78 79 BufferedImage image = ImageIO.read(tfile); 80 81 86 87 int width = image.getWidth(); 88 89 106 107 Image newImage = null; 108 Image newImage2 = null; 109 110 if( width>w ) { 111 newImage = 112 image.getScaledInstance(w, -1, Image.SCALE_SMOOTH); 113 } else { 114 newImage = (Image )image; 115 } 116 117 int height = newImage.getHeight(null); 118 119 if( height>h ) { 120 newImage2 = 121 newImage.getScaledInstance(-1, h, Image.SCALE_SMOOTH); 122 } else { 123 newImage2 = newImage; 124 } 125 126 BufferedImage myImage = 127 new BufferedImage (newImage2.getWidth(null), newImage2.getHeight(null), BufferedImage.TYPE_INT_RGB); 128 Graphics g = myImage.getGraphics(); 129 g.drawImage(newImage2,0,0,null); 130 131 132 ImageIO.write(myImage, "jpg", possibleCached); 133 ImageIO.write(myImage, "jpg", response.getOutputStream()); 134 response.getOutputStream().flush(); 135 response.getOutputStream().close(); 136 } 137 138 } else { 139 140 String mimeType = 142 FileTypeMap.getDefaultFileTypeMap().getContentType(tfile); 143 response.setContentType(mimeType); 144 145 byte[] buffer = new byte[(int) tfile.length()]; 147 FileInputStream fis = new FileInputStream (tfile); 148 fis.read(buffer); 149 fis.close(); 150 response.getOutputStream().write(buffer); 151 response.getOutputStream().flush(); 152 response.getOutputStream().close(); 153 154 } 155 } 156 157 160 public void init() throws ServletException { 161 relativePath = getInitParameter("relativePath"); 162 configPath = getInitParameter("configPath"); 163 } 164 165 } 166 | Popular Tags |