utStream(); byte[] b = new byte[1000]; while (stream.read(b) > 0) {
82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. til 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110.
baos.write(b); } byte[] imageByte = baos.toByteArray(); String type = ImageUtil.getImageType(imageByte); if (type.equals(ImageUtil.TYPE_NOT_AVAILABLE)) throw new Exception("file is not a image"); BufferedImage myImage = ImageUtil .readImage(imageByte); // display the image ImageUtil.printImage(myImage, type, res .getOutputStream()); // save the image // if you want to save the file into database, do it here // when you want to display the image, use the method printImage in ImageU
item.write(new File(filePath + "\\" + m.group(1)));
stream.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } } else { throw new IOException("fail to upload"); } } } } catch (IOException e) { e.printStackTrace(); } catch (FileUploadException e) {
111. 112. 113. 114. 115.
e.printStackTrace(); } } /** * Initialization of the servlet.
116. 117. 118. 119. 120. 121. 122. 123.
* * @throws ServletException * */ public void init() throws ServletException { // Change the file path here filePath = getServletContext().getRealPath("/"); } if an error occure
124. } servlet 中使用到一个 ImageUtil 类,其中封装了图片处理的实用方法,用于读写图片,代码如下:
ImageUtil.java java 代码 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. package com.ek.image; import java.awt.Component; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.MediaTracker; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.PixelGrabber;
12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41.
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.MemoryCacheImageInputStream; import net.jmge.gif.Gif89Encoder; import org.apachemons.logging.Log; import org.apachemons.logging.LogFactory; import com.sun.imageio.plugins.bmp.BMPImageReader; import com.sun.imageio.plugins.gif.GIFImageReader; import com.sun.imageio.plugins.jpeg.JPEGImageReader; import com.sun.imageio.plugins.png.PNGImageReader; /** * @author Erick Kong * @see ImageUt
il.java * @createDate: 2007-6-22 * @version 1.0 */ public class ImageUtil {
public static final String TYPE_GIF = "gif"; public static final String TYPE_JPEG = "jpeg"; public static final String TYPE_PNG = "png"; public static final String TYPE_BMP = "bmp"; public static final String TYPE_NOT_AVAILABLE = "na"; private static ColorModel getColorModel(Image image) throws InterruptedException, IllegalArgumentException {
42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71.
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); if (!pg.grabPixels()) throw new IllegalArgumentException(); return pg.getColorModel(); } private static void loadImage(Image image) throws InterruptedException, IllegalArgumentException { Component dummy = new Component() { private static final long serialV