最近做了一个关于图片浏览的内容。因为图片都是一些证件的资料的扫描件所以比较大,对系统的影响也是非常之大的,有很大可能直接把系统干死。那么我是这么处理的,给大家分享一下。如果大家有好的方案的话一定要早点告诉我。

需求简单介绍:

上传文件是压缩包,但是到查看资料的时候压缩包可下载本地看,同时也可以在系统中直接在线浏览。

设计方案

  

  

1 营业部用户上传图片文件压缩包文件到综合业务系统tomcat服务器,系统在tomcat服务器将压缩文件解压到系统临时目录。

2 系统分析解压的图片文件(文件名上有分类和序号),按照分类和顺序将文件名存入到数据库表中。存入数据库的文件名带有路径。

3 将解压的文件和压缩包按照规定的路径上传到FTP服务器中。

4 FTP服务器同时是一台tomcat服务器,图片的目录在tomcat服务器的webapps目录下。

5 图片的路径命名方式:

     webapps/imageserver/年(如2014)/月(如05)/日(如19)/16位唯一随机码/压缩包和解压文件

6 信审部查看图片文件和下载压缩包时,直接通过URL访问。

7 文件服务器不另外设置权限访问控制,所有访问控制通过16位唯一随机码控制,只有知道了16位唯一随机码,才能访问到图片文件和压缩包。

8 为了保证访问速度,单个图片文件最好控制在1M一下(我们可以推荐用户使用较低分辨率的扫描文件)。

9 如果图片访问量较大,需要购买硬件来升级文件服务器,例如采用NAS存储,升级带宽等。

实现步骤:

  1:上传压缩包:解压到本地临时目录,检测图片大小修改图片大小,上传到ftp。

  2:读取对应的的数据进行浏览。

这些相信大家都会,我下面贴出一个修改图片大小的工具类,方便我自己记忆,如果对大家有帮助也可以参考。

这个图片处理机制效率实在太慢,6个图片4-5秒,查了很多试了很多也没找到合适方法,暂且就用这个吧。如果有好的方法还望大家多多指教。

  1. package com.minxinloan.black.web.utils;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. import org.apache.log4j.Logger;
  10. //
  11. import com.sun.image.codec.jpeg.JPEGCodec;
  12. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  13. //
  14. //import com.sun.media.jai.codecimpl.JPEGCodec;
  15. //import com.sun.media.jai.codecimpl.JPEGImageEncoder;
  16.  
  17.  
  18.  
  19. public class ImagesUtils {
  20. private final static Logger log = Logger.getLogger(ImagesUtils.class);
  21. //图片允许的最大大小
  22. public static final int IMAGEMAXSIZE = 1024*1024;
  23. // 图片宽和高的最大尺寸
  24. public static final int IMAGEMAXBIG = 2000;
  25. // 图片宽和高的最小尺寸
  26. public static final int IMAGEMINBIG = 10;
  27. // 按原图大小生成新图
  28. public static final int CREATENEWIMAGETYPE_0 = 0;
  29. // 按指定的大小生成新图
  30. public static final int CREATENEWIMAGETYPE_1 = 1;
  31. // 按原图宽高比例生成新图-按指定的宽度
  32. public static final int CREATENEWIMAGETYPE_2 = 2;
  33. // 按原图宽高比例生成新图-按指定的高度
  34. public static final int CREATENEWIMAGETYPE_3 = 3;
  35. // 按原图宽高比例生成新图-按指定的宽和高中较大的尺寸
  36. public static final int CREATENEWIMAGETYPE_4 = 4;
  37. // 按原图宽高比例生成新图-按指定的宽和高中较小的尺寸
  38. public static final int CREATENEWIMAGETYPE_5 = 5;
  39. // 按原图宽高比例生成新图-按原图大小的90%进行修改
  40. public static final int CREATENEWIMAGETYPE_6 = 6;
  41. /**
  42. *
  43. * @param _file
  44. * 原图片
  45. * @param createType
  46. * 处理类型
  47. * @param newW
  48. * 新宽度
  49. * @param newH
  50. * 新高度
  51. * @return
  52. * @throws Exception
  53. */
  54. public static String createNewImage(File _file, int createType, int newW,
  55. int newH) throws Exception {
  56. if (_file == null)
  57. return null;
  58. String fileName = _file.getPath();
  59. if (fileName == null || "".equals(fileName)
  60. || fileName.lastIndexOf(".") == -1)
  61. return null;
  62. /*
  63. * else newFileName = "_" + newFileName;
  64. */
  65. String outFileName = fileName.substring(0, fileName.lastIndexOf("."))
  66. + fileName.substring(fileName.lastIndexOf("."), fileName
  67. .length());
  68. String fileExtName = fileName.substring(
  69. (fileName.lastIndexOf(".") + 1), fileName.length());
  70. if (newW < IMAGEMINBIG)
  71. newW = IMAGEMINBIG;
  72. else if (newW > IMAGEMAXBIG)
  73. newW = IMAGEMAXBIG;
  74. if (newH < IMAGEMINBIG)
  75. newH = IMAGEMINBIG;
  76. else if (newH > IMAGEMAXBIG)
  77. newH = IMAGEMAXBIG;
  78. // 得到原图信息
  79. if (!_file.exists() || !_file.isAbsolute() || !_file.isFile()
  80. || !checkImageFile(fileExtName))
  81. return null;
  82. Image src = ImageIO.read(_file);
  83. int w = src.getWidth(null);
  84. int h = src.getHeight(null);
  85. // 确定目标图片的大小
  86. int nw = w;
  87. int nh = h;
  88. if (createType == CREATENEWIMAGETYPE_0)
  89. ;
  90. else if (createType == CREATENEWIMAGETYPE_1) {
  91. nw = newW;
  92. nh = newH;
  93. } else if (createType == CREATENEWIMAGETYPE_2) {
  94. nw = newW;
  95. nh = (int) ((double) h / (double) w * nw);
  96. } else if (createType == CREATENEWIMAGETYPE_3) {
  97. nh = newH;
  98. nw = (int) ((double) w / (double) h * nh);
  99. } else if (createType == CREATENEWIMAGETYPE_4) {
  100. if ((double) w / (double) h >= (double) newW / (double) newH) {
  101. nh = newH;
  102. nw = (int) ((double) w / (double) h * nh);
  103. } else {
  104. nw = newW;
  105. nh = (int) ((double) h / (double) w * nw);
  106. }
  107. } else if (createType == CREATENEWIMAGETYPE_5) {
  108. if ((double) w / (double) h <= (double) newW / (double) newH) {
  109. nh = newH;
  110. nw = (int) ((double) w / (double) h * nh);
  111. } else {
  112. nw = newW;
  113. nh = (int) ((double) h / (double) w * nw);
  114. }
  115. } else if(createType == CREATENEWIMAGETYPE_6){
  116. // nw = (int)(w*0.5);
  117. // nh = (int)(h*0.5);
  118. double proportion = (double)1700/(double)w;
  119. nw = (int)((double)w*proportion);
  120. nh = (int)((double)h*proportion);
  121. }
  122. // 构造目标图片
  123. BufferedImage tag = new BufferedImage(nw, nh,
  124. BufferedImage.TYPE_INT_RGB);
  125. // 得到目标图片输出流
  126. FileOutputStream out = new FileOutputStream(outFileName);
  127. // 根据需求画出目标图片 方式1
  128. tag.getGraphics().drawImage(src, 0, 0, nw, nh, null);
  129. // 将画好的目标图输出到输出流 方式1
  130. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  131. encoder.encode(tag);
  132. out.close();
  133. return outFileName;
  134. }
  135. public static boolean checkImageFile(String extName) {
  136. if ("jpg".equalsIgnoreCase(extName))
  137. return true;
  138. if ("gif".equalsIgnoreCase(extName))
  139. return true;
  140. if ("bmp".equalsIgnoreCase(extName))
  141. return true;
  142. if ("jpeg".equalsIgnoreCase(extName))
  143. return true;
  144. if ("png".equalsIgnoreCase(extName))
  145. return true;
  146. return false;
  147. }
  148. public static String checkImageFile2(String extName) {
  149. if ("jpg".equalsIgnoreCase(extName))
  150. return "jpg";
  151. if ("gif".equalsIgnoreCase(extName))
  152. return "gif";
  153. if ("bmp".equalsIgnoreCase(extName))
  154. return "bmp";
  155. if ("jpeg".equalsIgnoreCase(extName))
  156. return "jpeg";
  157. if ("png".equalsIgnoreCase(extName))
  158. return "jpeg";
  159. return null;
  160. }
  161. //递归修改图片大小
  162. public static void changeImgSize(String filePath,int createType)
  163. {
  164. try {
  165. File tempFile = new File(filePath);
  166. if(tempFile.length()>IMAGEMAXSIZE){
  167. System.out.println("sss");
  168. changeImgSize(createNewImage(tempFile, createType, 0, 0),createType);
  169. }
  170. } catch (Exception e) {
  171. log.error("the changeImgSize is failed . the message is "+e.getMessage());
  172. }
  173. }
  174. /**
  175. * 缩放图像(按比例缩放)
  176. * @param srcImageFile 源图像文件地址
  177. * @param result 缩放后的图像地址
  178. * @param scale 缩放比例
  179. * @param flag 缩放选择:true 放大; false 缩小;
  180. */
  181. public final static void scale(String srcImageFile,String type, String result) {
  182. try {
  183. File tempFile = new File(srcImageFile);
  184. if(tempFile.length()>IMAGEMAXSIZE){
  185. System.out.println("sss");
  186. BufferedImage src = ImageIO.read(tempFile); // 读入文件
  187. int width = src.getWidth(); // 得到源图宽
  188. int height = src.getHeight(); // 得到源图长
  189. double sc = (double)1700/(double)width;
  190. width = (int)((double)width * sc);
  191. height = (int)((double)height * sc);
  192. Image image = src.getScaledInstance(width, height,
  193. Image.SCALE_DEFAULT);
  194. BufferedImage tag = new BufferedImage(width, height,
  195. BufferedImage.TYPE_INT_RGB);
  196. Graphics g = tag.getGraphics();
  197. g.drawImage(image, 0, 0, null); // 绘制缩小后的图
  198. g.dispose();
  199. ImageIO.write(tag, type, new File(result));// 输出到文件流
  200. }
  201. } catch (IOException e) {
  202. e.printStackTrace();
  203. }
  204. }
  205. public static void changeImgSize(File file)
  206. {
  207. try {
  208. // 判断文件是否是文件,如果是文件,获取路径,并计数
  209. if (file.isFile()) {
  210. String fileExtName = file.getName().substring(
  211. (file.getName().lastIndexOf(".") + 1), file.getName().length());
  212. String temp = ImagesUtils.checkImageFile2(fileExtName);
  213. if(temp!=null)
  214. //scale(file.getAbsolutePath(),temp,file.getAbsolutePath());
  215. ImagesUtils.changeImgSize(file.getAbsolutePath(), ImagesUtils.CREATENEWIMAGETYPE_6);
  216. } else {
  217. // 如果是文件夹,声明一个数组放文件夹和他的子文件
  218. File[] f = file.listFiles();
  219. // 遍历文件件下的文件,并获取路径
  220. for (File file2 : f) {
  221. changeImgSize(file2);
  222. }
  223. }
  224. } catch (RuntimeException e) {
  225. e.printStackTrace();
  226. }
  227. }
  228. public static void main(String[] args) {
  229. //
  230. // long start=System.currentTimeMillis();
  231. // String filePath = "C:\\Users\\zhangmi\\Desktop\\资料";
  232. // changeImgSize(new File(filePath));
  233. //
  234. // long end=System.currentTimeMillis();
  235. // //在最好的一行加上:
  236. // System.out.println("执行耗时 : "+(end-start)/1000f+" 秒 ");
  237. }
  238. }

 

做已铭记

 

 

版权声明:本文为puqiuxiaomao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/puqiuxiaomao/p/java.html