1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.common.util;
18
19 import java.awt.Component;
20 import java.awt.Graphics2D;
21 import java.awt.GraphicsConfiguration;
22 import java.awt.GraphicsDevice;
23 import java.awt.GraphicsEnvironment;
24 import java.awt.Image;
25 import java.awt.MediaTracker;
26 import java.awt.image.BufferedImage;
27 import java.awt.image.ColorModel;
28 import java.awt.image.PixelGrabber;
29 import java.io.BufferedInputStream;
30 import java.io.BufferedOutputStream;
31 import java.io.File;
32 import java.io.FileNotFoundException;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35
36 import javax.imageio.ImageIO;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.myfaces.custom.fileupload.UploadedFile;
41
42 import com.sun.image.codec.jpeg.JPEGCodec;
43 import com.sun.image.codec.jpeg.JPEGEncodeParam;
44 import com.sun.image.codec.jpeg.JPEGImageEncoder;
45
46
47 /***
48 * @author bbr (last modified by $Author: shally $)
49 * @version $Revision: 1.9 $ - $Date: 2005/12/21 11:38:41 $
50 * Central class with methods for File information
51 */
52 public class FileUtilities {
53
54 /***
55 * The LOG Log.
56 */
57 private static final Log LOG = LogFactory.getLog(FileUtilities.class);
58
59
60 /***
61 * Static utility class
62 */
63
64 private FileUtilities() {
65 }
66
67 /***
68 * gets the filename of a file.
69 * @param file a String indicating the name of the file
70 * @return a String indicating the filename -without extension- of the file
71 */
72 public static String getFileName(String file) {
73 String filename = file;
74 int index = file.lastIndexOf('.');
75 if (index > 0) {
76 filename = file.substring(0, index);
77 }
78 int startIndex = file.lastIndexOf('/');
79 if (startIndex > 0) {
80 filename = file.substring(startIndex, filename.length());
81 }
82 int startIndex2 = file.lastIndexOf('//');
83 if (startIndex2 > 0) {
84 filename = file.substring(startIndex2, filename.length());
85 }
86
87 return filename;
88 }
89
90 /***
91 * @param fileOld is old file
92 * @param fileNew is new file
93 * @param id is id
94 * @return true if files are equal
95 */
96 public static boolean fileEquals(String fileOld, String fileNew, int id) {
97 String file1 = FileUtilities.getFileName(fileOld)
98 + FileUtilities.getFileExtension(fileOld);
99 String file2 = FileUtilities.getFileName(fileOld) + "_" + id
100 + FileUtilities.getFileExtension(fileOld);
101 return file1.equals(file2);
102
103 }
104
105 /***
106 * gets the extension for a file.
107 * @param file the File to retrieve the extension for.
108 * @return a String containing the extension.
109 */
110 public static String getFileExtension(String file) {
111 String filename = file;
112 int index = file.lastIndexOf('.');
113 if (index >= 0) {
114 filename = file.substring(index + 1, file.length());
115 }
116 return filename;
117
118 }
119
120 /***
121 * @param file to delete.
122 */
123 public static void deleteUploadedFile(File file ) {
124
125 if (file.exists()) {
126 if (!file.delete()) {
127 LOG.error("File " + file.getAbsolutePath()
128 + " can not be deleted");
129 }
130 LOG.info("File " + file.getAbsolutePath() + " is deleted");
131 } else {
132 LOG.info("File " + file.getAbsolutePath() + " does not exists");
133 }
134
135 }
136
137 /***
138 * write a uplaoded file is uplaoded file
139 * @param inFile is inFile
140 * @param file is file
141 * @param filePath is filePath
142 */
143 public static void writeUploadedFile(UploadedFile inFile, File file, File filePath) {
144
145 if (!filePath.exists() || !filePath.isDirectory()) {
146 boolean success = filePath.mkdir();
147 if (success) {
148 LOG.info("Upload directory created");
149 } else {
150 LOG
151 .fatal("Upload directory does not exist, and could not create one");
152 }
153 }
154
155 LOG.info("File creating: " + file.getAbsolutePath());
156
157 BufferedOutputStream out = null;
158 BufferedInputStream in = null;
159
160 try {
161
162 out = new BufferedOutputStream(new FileOutputStream(file));
163
164 in = new BufferedInputStream(inFile.getInputStream());
165
166 int c = in.read();
167 while (c != -1) {
168 out.write(c);
169 c = in.read();
170 }
171 try {
172 out.close();
173 in.close();
174 } catch (IOException e2) {
175 LOG.error(e2);
176 }
177
178 } catch (FileNotFoundException e) {
179 LOG.error(e);
180 } catch (IOException e) {
181 LOG.error(e);
182 } finally {
183 try {
184 if (out != null) {
185 out.close();
186 }
187 if (in != null) {
188 in.close();
189 }
190 } catch (IOException e2) {
191 LOG.error(e2);
192 }
193 }
194
195 LOG.info("Done writing to: " + file);
196 }
197
198 /***
199 * @param file the file to resize.
200 * @param width the width in px.
201 * @param height the hight in px.
202 * @param filename is fname
203 * @return the resized image.
204 */
205 public static File resizeImage(UploadedFile file, int width, int height, File filename) {
206
207 BufferedImage image = null;
208 File writtenfile = null;
209
210 LOG.info("Starting conversion");
211 try {
212
213 image = ImageIO.read(file.getInputStream());
214
215 LOG.info("Created image");
216 writtenfile = resizeImage(image, width, height, filename);
217 } catch (IOException e) {
218 LOG.debug(e);
219 }
220
221
222
223 return writtenfile;
224 }
225
226 /***
227 * @param image is image
228 * @param width is width
229 * @param height is height
230 * @param filename is the filename
231 * @return the resized image
232 */
233 public static File resizeImage(BufferedImage image, int width, int height, File filename) {
234
235 Image scaledImageSmooth = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
236
237
238
239 BufferedImage bufferedImage2 = toBufferedImage(scaledImageSmooth,getDefaultConfiguration());
240
241 LOG.info("Resized image");
242
243
244
245
246
247
248
249 LOG.info("Created file" + filename);
250 try {
251
252
253 File file = new File(filename.getParent());
254
255 if (!file.exists() || !file.isDirectory()) {
256 LOG.info("Creating dir: "+file);
257 boolean success = file.mkdir();
258 if (success) {
259 LOG.info("Upload directory created");
260 } else {
261 LOG.fatal("Upload directory does not exist, and could not create one");
262 }
263 }
264 ImageIO.write(bufferedImage2, "jpeg", filename);
265 } catch (IOException e) {
266 LOG.error(e);
267 }
268
269
270
271 LOG.info("Written file");
272 LOG.info("Written to: "+filename.getAbsolutePath());
273
274 return filename;
275
276
277 }
278
279 /***
280 * @param im is image
281 * @param file is file
282 */
283 public static void writeAsJpeg(BufferedImage im, File file) {
284 try
285 {
286 JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(new FileOutputStream(file));
287 JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(im);
288
289 param.setQuality(1.2f,true);
290 encoder.setJPEGEncodeParam(param);
291 encoder.encode(im);
292 }
293 catch(IOException e)
294 {
295 LOG.info(e);
296 }
297 }
298
299 /***
300 * @param image is image
301 * @param c is component
302 */
303 public static void loadImage(Image image, Component c) {
304 try {
305 if (image instanceof BufferedImage){
306 return;
307 }
308 MediaTracker tracker = new MediaTracker(c);
309 tracker.addImage(image, 0);
310 tracker.waitForID(0);
311 if (MediaTracker.COMPLETE != tracker.statusID(0, false)){
312 throw new IllegalStateException("image loading fails");
313 }
314 } catch (InterruptedException e) {
315 throw new FileUtilitiesException("interrupted", e);
316 }
317 }
318
319 /***
320 * @param image is image
321 * @return the color model for the image
322 */
323 public static ColorModel getColorModel(Image image) {
324 try {
325 PixelGrabber grabby = new PixelGrabber(image, 0, 0, 1, 1, false);
326 if (!grabby.grabPixels()){
327 throw new FileUtilitiesException("pixel grab fails");
328 }
329 return grabby.getColorModel();
330 } catch (InterruptedException e) {
331 throw new FileUtilitiesException("interrupted", e);
332 }
333 }
334
335 /***
336 * @param image is image
337 * @param gc is GraphicsConfiguration
338 * @return a buffered image
339 */
340 public static BufferedImage toBufferedImage(Image image, GraphicsConfiguration grc) {
341 if (image instanceof BufferedImage){
342 return (BufferedImage) image;
343 }
344
345 int w = image.getWidth(null);
346 int h = image.getHeight(null);
347
348 GraphicsConfiguration gc = grc == null ? getDefaultConfiguration() : grc;
349
350 BufferedImage result = gc.createCompatibleImage(w, h);
351 Graphics2D g = result.createGraphics();
352 g.drawImage(image, 0, 0, null);
353 g.dispose();
354 return result;
355 }
356
357 /***
358 * @return the graphic configuration
359 */
360 public static GraphicsConfiguration getDefaultConfiguration() {
361 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
362 GraphicsDevice gd = ge.getDefaultScreenDevice();
363 return gd.getDefaultConfiguration();
364 }
365
366 /***
367 * @param file is file
368 * @param id is id
369 * @return converted file name
370 */
371 public static String makeNewName(UploadedFile file, int id) {
372 String ext = FileUtilities.getFileExtension(file.getName());
373 String name = FileUtilities.getFileName(file.getName());
374 String newname = name + "_" + id + "." + ext;
375 LOG.info("Named file: " + newname);
376 return newname;
377
378 }
379
380 }
381
382 /***
383 * $Log: FileUtilities.java,v $
384 * Revision 1.9 2005/12/21 11:38:41 shally
385 * *** empty log message ***
386 *
387 * Revision 1.8 2005/12/20 15:36:46 shally
388 * CheckStyle and PMD changes.
389 *
390 * Revision 1.7 2005/12/08 14:53:46 shally
391 * Opkuis voor checkstyle.
392 *
393 * Revision 1.6 2005/09/24 17:30:46 schauwvliege
394 * delete content, refactory of resume
395 *
396 * Revision 1.5 2005/09/23 07:29:48 schauwvliege
397 * delete content refactory for interview
398 *
399 * Revision 1.4 2005/09/15 13:58:08 schauwvliege
400 * Wizard post interview
401 *
402 * Revision 1.2 2005/09/14 10:46:22 schauwvliege
403 * Upload files
404 *
405 * Revision 1.1 2005/08/20 13:00:48 stephan_janssen
406 * Moved to util package
407 *
408 * Revision 1.2 2005/08/08 09:38:22 bme_jcs
409 * resolved checkstyle errors
410 *
411 * Revision 1.1 2005/08/05 10:09:33 bavo_jcs
412 * Extracted File methods to static class
413 *
414 */