1 11 package com.tonbeller.jpivot.chart; 12 13 import java.io.BufferedInputStream ; 14 import java.io.BufferedOutputStream ; 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileNotFoundException ; 18 import java.io.IOException ; 19 import java.text.SimpleDateFormat ; 20 import java.util.Date ; 21 import java.util.Locale ; 22 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 34 public class GetChart extends HttpServlet { 35 String basePath; 36 38 final static String fileNotFound="/WEB-INF/jpivot/img_not_found.gif"; 39 40 public void init(ServletConfig config) throws ServletException { 41 super.init(config); 42 } 43 45 public void destroy() { 46 47 } 48 49 53 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 54 throws ServletException , IOException { 55 56 HttpSession session = request.getSession(); 57 String filename = request.getParameter("filename"); 58 System.out.println("GetChart called: filename="+filename); 59 if (filename == null) { 60 throw new ServletException ("Parameter 'filename' must be supplied"); 61 } 62 63 filename = searchReplace(filename, "..", ""); 66 67 File file = new File (System.getProperty("java.io.tmpdir"), filename); 69 if (!file.exists()) { 70 throw new ServletException ("File '" + file.getAbsolutePath() + "' does not exist"); 71 } 72 sendTempFile(file, response); 74 75 return; 76 77 } 78 public static void sendTempFile(File file, HttpServletResponse response) 79 throws IOException , FileNotFoundException { 80 81 String mimeType = null; 82 String filename = file.getName(); 83 if (filename.length() > 5) { 84 if (filename.substring(filename.length() - 5, filename.length()).equals(".jpeg") || 85 filename.substring(filename.length() - 5, filename.length()).equals(".jpg")) { 86 mimeType = "image/jpeg"; 87 } 88 else if (filename.substring(filename.length() - 4, filename.length()).equals(".png")) { 89 mimeType = "image/png"; 90 } 91 else if (filename.substring(filename.length() - 4, filename.length()).equals(".gif")) { 92 mimeType = "image/gif"; 93 } 94 } 95 sendTempFile(file, response, mimeType); 96 } 97 98 108 public static void sendTempFile(File file, HttpServletResponse response, 109 String mimeType) 110 throws IOException , FileNotFoundException { 111 112 if (file.exists()) { 113 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); 114 115 if (mimeType != null) { 117 response.setHeader("Content-Type", mimeType); 118 } 119 response.setHeader("Content-Length", String.valueOf(file.length())); 120 response.setDateHeader("Last-Modified", file.lastModified()); 121 BufferedOutputStream bos = new BufferedOutputStream (response.getOutputStream()); 122 byte[] input = new byte[1024]; 123 boolean eof = false; 124 while (!eof) { 125 int length = bis.read(input); 126 if (length == -1) { 127 eof = true; 128 } else { 129 bos.write(input, 0, length); 130 } 131 } 132 bos.flush(); 133 bis.close(); 134 bos.close(); 135 } 136 else { 137 throw new FileNotFoundException (file.getAbsolutePath()); 138 } 139 return; 140 } 141 142 152 public static String searchReplace(String inputString, 153 String searchString, 154 String replaceString) { 155 156 int i = inputString.indexOf(searchString); 157 if (i == -1) { 158 return inputString; 159 } 160 161 String r = ""; 162 r += inputString.substring(0, i) + replaceString; 163 if (i + searchString.length() < inputString.length()) { 164 r += searchReplace(inputString.substring(i + searchString.length()), 165 searchString, 166 replaceString); 167 } 168 169 return r; 170 } 171 172 176 protected void doGet(HttpServletRequest request, HttpServletResponse response) 177 throws ServletException , IOException { 178 processRequest(request, response); 179 } 180 181 185 protected void doPost(HttpServletRequest request, HttpServletResponse response) 186 throws ServletException , IOException { 187 processRequest(request, response); 188 } 189 190 192 public String getServletInfo() { 193 return "Short description"; 194 } 195 196 } 197 | Popular Tags |