Struts2 图片上传、缩放、剪切处理 第二节

1、技术目标:
 对上传的图片进行缩放、剪切操作;

2、处理流程:
1)在影片列表页面(films.jsp)点"修改"进入影片修改页面,如图:

 

 

2)在影片修改页面(update.jsp)中双击图片,进入图片缩放、剪切处理页面(imgscissor.jsp),如图,

update.jsp效果图:

 

 imgscissor.jsp效果图:

 

 3)在imgscissor.jsp页面中设置图片的缩放宽度、高度,如图,

 压缩前效果图:

 

4)在imgscissor.jsp页面中使用jQuery图片剪切插件"imgAreaSelect"对图片进行剪切操作,如图,

 剪切前效果图:

 

 5)采用ajax方式将图片的缩放、剪切参数提交给Action进行处理

6)服务器Action根据页面设置的参数对图片进行如下处理:

  •  采用java-image-scaling-0.8.5.jar提供的功能对图片进行缩放处理
  • 采用javax.imageio以及java.awt提供的功能对图片进行剪切处理
    7)用处理好的图片替换原有的图片,服务器Action返回处理结果,页面提示处理结果并显示处理后的图片,如图,

 压缩后效果图:

 

 剪切后效果图:

 

 注意:第二步、第三步处理可同时进行

  

3、使用准备

 3.1)站点根路径下创建js文件夹,导入如下js、css文件:

 jquery.js         版本:v1.4.2

jquery.tooltip.css jQuery信息提示插件样式

jquery.tooltip.js jQuery信息提示插件

jquery.form.js jQuery表单插件

loading.gif         进度提示图片

jquery.loadmask.css jQuery窗口屏蔽插件样式

jquery.loadmask.js jQuery窗口屏蔽插件

 3.2)导入jQuery插件imgareaselect

 在js文件夹下创建文件夹imgareaselect,imgareaselect下再创建css文件夹,imgareaselect下导入如下文件:

 jquery.imgareaselect.jsjQuery图片剪切插件

 imgareaselect/css下导入如下文件(图片剪切插件所选样式与图片素材):

 border-anim-h.gif

border-anim-v.gif

border-h.gif

border-v.gif

imgareaselect-animated.css

imgareaselect-default.css

imgareaselect-deprecated.css

 

3.3)导入相关的jar包

 gson-1.5.jar将Java对象转换成JSON

java-image-scaling-0.8.5.jar图片压缩工具

4、给影片修改页面(update.jsp)加入如下两处代码

 4.1)导入相关js、css文件

 Html代码  收藏代码

  1. <link rel="stylesheet" href="<%=basePath %>/js/jquery.tooltip.css" type="text/css"/>  
  2. <script type="text/javascript" src="<%=basePath %>/js/jquery.js"></script>  
  3. <script type="text/javascript" src="<%=basePath %>/js/imgscissor.js"></script>  
  4. <script type="text/javascript" src="<%=basePath %>/js/jquery.tooltip.js"></script>  


4.2)为img标签加入scissor类样式,如下:

  Html代码  收藏代码

  1. <img src="<s:property value="imgurl" />" class="scissor" width="92" height="130"   
  2. onerror="javascript:this.src='<%=basePath %>/images/error.gif'" /><br />  

  

修改后的update.jsp如下:

 Html代码  收藏代码

  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2. pageEncoding="utf-8" %>  
  3. <%@taglib uri="/struts-tags" prefix="s" %>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <title>修改影片</title>  
  12.     <link rel="stylesheet" href="<%=basePath %>/js/jquery.tooltip.css" type="text/css"/>  
  13.     <script type="text/javascript" src="<%=basePath %>/js/jquery.js"></script>  
  14.     <script type="text/javascript" src="<%=basePath %>/js/imgscissor.js"></script>  
  15.     <script type="text/javascript" src="<%=basePath %>/js/jquery.tooltip.js"></script>  
  16.   </head>  
  17.   <body>  
  18.     <s:form action="/film/updateFilm" method="post" enctype="multipart/form-data">  
  19.         <s:hidden name="id" />  
  20.         影片名称:<s:textfield name="fname" /><br />  
  21.         <%-- 处理原有图片 --%>  
  22.         <s:if test='imgurl != null and imgurl != ""'>  
  23.             <%-- 保存原有图片的信息在修改时提交 --%>  
  24.             <s:hidden name="imgurl"></s:hidden>  
  25.             <%-- 显示原有图片,onerror属性:在打不开图片时显示提示图片 --%>  
  26.             <img src="<s:property value="imgurl" />" class="scissor" width="92" height="130"   
  27.              onerror="javascript:this.src='<%=basePath %>/images/error.gif'" /><br />  
  28.         </s:if>  
  29.         <%--文件选择框 --%>  
  30.         影片海报:<s:file id="imgPhoto" name="imgPhoto"/><br />  
  31.         <s:submit value=" 修改 "></s:submit>  
  32.     </s:form>  
  33.   </body>  
  34. </html>  

 

5、在js文件夹下创建imgscissor.js,用于处理"双击图片"向Action提交请求并进入图片缩放、剪切页面,代码如下:

 Js代码  收藏代码

  1. $(document).ready(function() {  
  2.       
  3.         //获取JS文件当前路径并设置站点绝对路径  
  4.         var CurrentJsPath = (function (){  
  5.             var k = document.getElementsByTagName("script");  
  6.             srcStr = k[0].getAttribute("src");  
  7.             //截取出站点路径  
  8.             srcStr = srcStr.substring(0, srcStr.indexOf("/js/"));  
  9.             return srcStr;   
  10.         })();  
  11.       
  12.     /* 
  13.      * 创建表单,该表单访问ImageScissorAction并将两个参数传过去 
  14.      * 参数:proportion    裁剪比例(一般的处理要求等比例裁剪) 
  15.      * 参数:originPath    图片url路径 
  16.      */  
  17.     $('body').append('<form id="toScissrorForm" action="' + CurrentJsPath + 'film/toScissor" method="post">' +   
  18.             '<input type="hidden" name="proportion" value="" />' +   
  19.             '<input type="hidden" name="originPath" value="" /> </form>');  
  20.     $('#toScissrorForm').hide();  
  21.       
  22.     //为样式为scissor的元素(img标签)加入处理  
  23.     $('.scissor').each(function() {  
  24.           
  25.         //获取标签的src属性值(图片的url)并加入一个参数(当前时间)以防止缓存  
  26.         var imgPath = $(this).attr('src') + '?' + new Date().getTime();  
  27.           
  28.         //将带时间参数的url再设置给图片的src属性  
  29.         $(this).attr('src',imgPath);  
  30.           
  31.         //设置鼠标移到图片上的提示文字  
  32.         $(this).tooltip({  
  33.             showURL: false,  
  34.             bodyHandler: function() {  
  35.                 return '双击裁剪图片';  
  36.             }  
  37.         });  
  38.           
  39.         //设置图片的双击事件处理  
  40.         $(this).dblclick(function(){  
  41.               
  42.             //获取img标签的src值(url路径)  
  43.             var imageSrc = $(this).attr('src');  
  44.               
  45.             //读取img的宽、高  
  46.             var width = $(this).attr("width");  
  47.             var height = $(this).attr("height");  
  48.             //设置拖拽比例(裁剪图片应该按比例裁剪)  
  49.             var proportion = width + ":" + height;  
  50.               
  51.             //将值设置给表单  
  52.             $('#toScissrorForm input[name=originPath]').val(imageSrc);  
  53.             $('#toScissrorForm input[name=proportion]').val(proportion);  
  54.             $('#toScissrorForm').submit();//双击提交表单  
  55.         });  
  56.           
  57.       });  
  58.       
  59. });  

  

6、在manager文件夹下创建jsp文件imgscissor.jsp(图片压缩、剪切操作页面),压缩、剪切操作完成后将参数异步发送给ImageScissorAction,代码如下:

  Html代码  收藏代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags"%>  
  4.   
  5. <%   
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;   
  8. %>  
  9.   
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  11. <html>  
  12. <head>  
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  14. <title>图片裁剪页面</title>  
  15.   
  16. <!-- 导入插件样式 -->  
  17. <link rel="stylesheet"     
  18. href="<%=basePath %>/js/imgareaselect/css/imgareaselect-animated.css" type="text/css" />  
  19. <link rel="stylesheet" href="<%=basePath %>/js/jquery.tooltip.css" type="text/css"/>  
  20. <link href="<%=basePath %>/js/jquery.loadmask.css" rel="stylesheet" type="text/css" />  
  21.   
  22. <!-- 设置图片操作DIV的样式 -->  
  23. <style type="text/css">  
  24.   
  25. #imgDiv{  
  26.   
  27.     margin-left: 3px; margin-top: 5px; width: 800px; height: 700px;  
  28.     overflow: auto;  
  29.     scrollbar-3dlight-color:#595959;   
  30.     scrollbar-arrow-color:#FFFFFF;   
  31.     scrollbar-base-color:#CFCFCF;   
  32.     scrollbar-darkshadow-color:#FFFFFF;   
  33.     scrollbar-face-color:#CFCFCF;   
  34.     scrollbar-highlight-color:#FFFFFF;   
  35.     scrollbar-shadow-color:#595959;  
  36. }  
  37.   
  38. </style>  
  39.   
  40. <!-- 导入jQuery各种插件 -->  
  41. <script type="text/javascript" charset="UTF-8" src="<%=basePath %>/js/jquery.js"></script>  
  42. <script type="text/javascript" src="<%=basePath %>/js/imgareaselect/jquery.imgareaselect.js"></script>  
  43. <script type="text/javascript" src="<%=basePath %>/js/jquery.tooltip.js"></script>  
  44. <script type="text/javascript" src="<%=basePath %>/js/jquery.form.js"></script>  
  45. <script type="text/javascript" src="<%=basePath %>/js/jquery.loadmask.js"></script>  
  46.   
  47. <script type="text/javascript">  
  48.   
  49.     $(document).ready(function() {  
  50.   
  51.         //获取拖拽比例  
  52.         var proportion = $('#proportion').val();  
  53.   
  54.         //unescape可对通过escape()编码的字符串进行解码  
  55.         var imagePath = unescape($('#img').attr('src'));  
  56.   
  57.         //图片的url设置给隐藏表单originPath,准备提交给Action  
  58.         $('input[name=originPath]').val(imagePath);  
  59.   
  60.         //通过图片的url取出服务器域名全路径并设置好访问Action的url  
  61.         var imgPath = $('#img').attr('src');  
  62.         var newAction = imgPath.substring(0, imgPath.indexOf('/images')) +"/film/scissor";  
  63.         //设置表单的action属性为Action的rul  
  64.         $('#scissorForm').attr('action',newAction);  
  65.           
  66.         //为id为img的图片设置图片剪切插件  
  67.         var imgArea = $('#img').imgAreaSelect({  
  68.               
  69.             fadeSpeed: 400,  
  70.             handles: true,  
  71.                     instance: true,  
  72.                 aspectRatio: proportion, //设置拖拽比例  
  73.             onSelectEnd : function(img, selection) {  
  74.                 $('#areaHeight').html((selection.y2 - selection.y1)+" 像素");  
  75.                 $('#areaWidth').html((selection.x2 - selection.x1)+" 像素");  
  76.                 $('input[name=x1]').val(selection.x1);  
  77.                 $('input[name=y1]').val(selection.y1);  
  78.                 $('input[name=x2]').val(selection.x2);  
  79.                 $('input[name=y2]').val(selection.y2);  
  80.                 $('#warning').hide();  
  81.             }  
  82.         });  
  83.           
  84.         //提交按钮的单击事件处理  
  85.         $('#submitBtn').click(function(){  
  86.   
  87.             if(confirm("是否提交?")){  
  88.                 $('#scissorForm').submit();  
  89.             }  
  90.               
  91.         });  
  92.   
  93.         //设置表单ajax异步提交  
  94.         $('#scissorForm').submit(function() {  
  95.                       
  96.                     $(this).ajaxSubmit({   
  97.                               
  98.                             beforeSubmit:  function(){//提交前的处理  
  99.                                   
  100.                                 //提交表单处理期间,屏蔽整个窗口                             
  101.                                 $('#content').mask("正在提交数据,请稍候。");  
  102.                                 //关闭提交按钮  
  103.                                 $('input[name=submit]').attr("disabled", true);  
  104.                             },  
  105.                             dataType:  'json',   
  106.                             success: function showResponse(responseText, statusText, xhr, $form){   
  107.   
  108.                                 //取消窗口屏蔽  
  109.                                 $('#content').unmask();  
  110.                                 $('#warning').show();  
  111.   
  112.                                 //打开提交按钮  
  113.                                 $('input[name=submit]').attr("disabled", false);  
  114.                                   
  115.                                 imgArea.cancelSelection();  
  116.                                 var imgPath = $('#img').attr('src')+'?'+new Date().getTime();  
  117.                                 $('#img').attr('src',imgPath);  
  118.                                   
  119.                                 //重置参数  
  120.                                 $('input[name=scaleHeight]').val('');  
  121.                                 $('input[name=scaleWidth]').val('');  
  122.                                 $('#areaHeight').html('');  
  123.                                 $('#areaWidth').html('');  
  124.                                 alert(responseText);  
  125.                             }  
  126.                     });   
  127.                       
  128.                     return false;   
  129.         });   
  130.   
  131.         //返回上一页  
  132.         $('#back').click(function(){  
  133.             history.go(-1);  
  134.         });  
  135.           
  136.     });  
  137. </script>  
  138. </head>  
  139.   
  140. <body>  
  141. <div id="content" >  
  142. <!-- 保存图片修改后参数的表单 -->  
  143. <form id="scissorForm" method="post">  
  144.   
  145.     剪切区域,高:<label id="areaHeight"></label>  
  146.     宽:<label id="areaWidth"></label>  
  147.     <br /><br />  
  148.       
  149.     缩放宽:<input type="text" name="scaleWidth" value="" style="width: 100px;" />   
  150.     缩放高:<input type="text" name="scaleHeight" value="" style="width: 100px;" />  
  151.     <br /><br />  
  152.       
  153.     <!-- 提示信息 -->  
  154.     <font color="red">[请先选取裁剪区域]&nbsp;[大尺寸图片可先缩放再剪切]</font>  
  155.     <br /><br />  
  156.       
  157.     <input type="button" id="submitBtn" name="submitBtn" value="提交" />  
  158.     <input type="button" id="back"  value="返回" />  
  159.       
  160.     <%-- 图片剪切区域 --%>  
  161.     <div id="imgDiv">  
  162.         <!-- 待处理的图片 -->  
  163.         <img id="img" src="<s:property value="originPath" />" class="scissor" alt="点击裁剪图片">  
  164.     </div>  
  165.       
  166.     <!-- 保存图片操作参数的隐藏表单 -->  
  167.     <input type="hidden" name="x1" value="" />   
  168.     <input type="hidden" name="y1" value="" />   
  169.     <input type="hidden" name="x2" value="" />   
  170.     <input type="hidden" name="y2" value="" />   
  171.     <input type="hidden" name="originPath" value="" />   
  172. </form>  
  173. </div>  
  174.   
  175. <!-- 保存缩放比的隐藏表单 -->  
  176. <input type="hidden" id="proportion" name="proportion"   
  177. value="<s:property value="proportion" />" />  
  178.   
  179. </body>  
  180. </html>  

 

7、在com.xxx.util包下创建图片处理工具类ImageUtil,提供图片的压缩(scaleImage)、剪切(scissor)处理方法,代码如下:

 Java代码  收藏代码

  1. package com.xxx.util;  
  2.   
  3. import java.awt.Rectangle;  
  4. import java.awt.image.BufferedImage;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.IOException;  
  8. import java.util.Iterator;  
  9. import javax.imageio.ImageIO;  
  10. import javax.imageio.ImageReadParam;  
  11. import javax.imageio.ImageReader;  
  12. import javax.imageio.stream.ImageInputStream;  
  13. import com.mortennobel.imagescaling.ResampleOp;  
  14.   
  15. /** 
  16.  * 图片处理工具类 
  17.  * 
  18.  */  
  19. public class ImageUtil {  
  20.       
  21.     /** 
  22.      * 根据传入的图片坐标进行图片截取 
  23.      *  
  24.      * @param x1 X起点坐标 
  25.      * @param x2 X终点坐标 
  26.      * @param y1 Y起点坐标 
  27.      * @param y2 Y终点坐标 
  28.      * @param originPath 原始图片的存放路径 
  29.      * @param savePath 截取后图片的存储路径 
  30.      * @throws IOException 
  31.      */  
  32.     public static void scissor(int x1, int x2, int y1, int y2,  
  33.             String originPath, String savePath) throws IOException {  
  34.           
  35.         FileInputStream is = null;  
  36.         ImageInputStream iis = null;  
  37.   
  38.         try {  
  39.               
  40.             // 读取图片文件  
  41.             is = new FileInputStream(originPath);  
  42.   
  43.             /* 
  44.              * 返回包含所有当前已注册 ImageReader 的 Iterator, 
  45.              * 这些 ImageReader 声称能够解码指定格式。 
  46.              * 参数:formatName - 包含非正式格式名称 .(例如 "jpeg" 或 "tiff")等 。 
  47.              */  
  48.             Iterator<ImageReader> it = ImageIO  
  49.                     .getImageReadersByFormatName(getExtention(originPath)  
  50.                             .toLowerCase());  
  51.             ImageReader reader = it.next();  
  52.             // 获取图片流  
  53.             iis = ImageIO.createImageInputStream(is);  
  54.   
  55.             /* 
  56.              * iis:读取源.true:只向前搜索,将它标记为 ‘只向前搜索’。 
  57.              * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许  
  58.              * reader 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。 
  59.              */  
  60.             reader.setInput(iis, true);  
  61.   
  62.             /* 
  63.              * 描述如何对流进行解码的类,用于指定如何在输入时从 Java Image I/O 
  64.              * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件  
  65.              * 将从其 ImageReader 实现的 
  66.              * getDefaultReadParam方法中返回 ImageReadParam 的实例。 
  67.              */  
  68.             ImageReadParam param = reader.getDefaultReadParam();  
  69.   
  70.             /* 
  71.              * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 
  72.              * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 
  73.              */  
  74.             Rectangle rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);  
  75.   
  76.             // 提供一个 BufferedImage,将其用作解码像素数据的目标。  
  77.             param.setSourceRegion(rect);  
  78.   
  79.             /* 
  80.              * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 它作为一个完整的 
  81.              * BufferedImage 返回。 
  82.              */  
  83.             BufferedImage bi = reader.read(0, param);  
  84.   
  85.             // 保存新图片  
  86.             ImageIO.write(bi, getExtention(originPath).toLowerCase(), new File(  
  87.                     savePath));  
  88.         } finally {  
  89.             if (is != null)  
  90.                 is.close();  
  91.             if (iis != null)  
  92.                 iis.close();  
  93.         }  
  94.     }  
  95.   
  96.     /** 
  97.      *  
  98.      * 缩放图片 
  99.      *  
  100.      * @param width 宽 
  101.      * @param height 高 
  102.      * @param originPath 原始路径 
  103.      * @param savePath 保存路径 
  104.      * @throws IOException 
  105.      */  
  106.     public static void scaleImage(int width, int height, String originPath,  
  107.             String savePath) throws IOException {  
  108.           
  109.         BufferedImage sourceImage = readImage(originPath);  
  110.         ResampleOp resampleOp = new ResampleOp(width, height);  
  111.         BufferedImage rescaledTomato = resampleOp.filter(sourceImage, null);  
  112.         ImageIO.write(rescaledTomato, getExtention(originPath).toLowerCase(),  
  113.                 new File(savePath));  
  114.     }  
  115.       
  116.     private static BufferedImage readImage(String imagePath) throws IOException {  
  117.         return readImage(new File(imagePath));  
  118.     }  
  119.       
  120.     private static BufferedImage readImage(File image) throws IOException {  
  121.         return ImageIO.read(image);  
  122.     }  
  123.       
  124.     /** 
  125.      * 功能:提取文件名的后缀 
  126.      *  
  127.      * @param fileName 
  128.      * @return 
  129.      */  
  130.     private static String getExtention(String fileName) {  
  131.         int pos = fileName.lastIndexOf(".");  
  132.         return fileName.substring(pos + 1);  
  133.     }  
  134.   
  135. }  

8、在com.xxx.web.struts.action包下创建ImageScissorAction控制器,负责接收页面参数调用ImageUtil执行图片压缩、剪切操作,代码如下:

 Java代码  收藏代码

  1. package com.xxx.web.struts.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import javax.servlet.http.HttpServletResponse;  
  6. import org.apache.struts2.ServletActionContext;  
  7. import com.google.gson.Gson;  
  8. import com.opensymphony.xwork2.ActionSupport;  
  9. import com.xxx.util.ImageUtil;  
  10.   
  11. public class ImageScissorAction extends ActionSupport {  
  12.       
  13.     private static final long serialVersionUID = -5971162241884111578L;  
  14.     private Integer x1;  
  15.     private Integer x2;  
  16.     private Integer y1;  
  17.     private Integer y2;  
  18.     private String originPath;  
  19.     private String savePath;  
  20.     private Integer scaleWidth;  
  21.     private Integer scaleHeight;  
  22.       
  23.     /** 
  24.      * 缩放比例 
  25.      */  
  26.     private String proportion;  
  27.       
  28.     /** 
  29.      * 获取web服务器路径 
  30.      * @param relativePath 
  31.      * @return 
  32.      */  
  33.     protected String getRealPath(String relativePath) {  
  34.           
  35.         return ServletActionContext.getServletContext().getRealPath(relativePath);  
  36.     }  
  37.       
  38.     @Override  
  39.     public String execute() throws Exception {  
  40.           
  41.         try {  
  42.             // 取出服务器host以及端口等  
  43.             originPath = originPath.substring(originPath.indexOf("/images"));  
  44.             if (originPath.contains("?")) {  
  45.                 originPath = originPath.substring(0, originPath.indexOf("?"));  
  46.             }  
  47.               
  48.             originPath = getRealPath(originPath);  
  49.             if (savePath == null || savePath.trim().equals("")) {  
  50.                 savePath = originPath;  
  51.             }  
  52.             if (x1 != null && x2 != null && y1 != null && y2 != null) {  
  53.                 ImageUtil.scissor(x1, x2, y1, y2, originPath, savePath);  
  54.             }  
  55.               
  56.             if (scaleWidth != null && scaleWidth != null) {  
  57.                 ImageUtil.scaleImage(scaleWidth, scaleHeight, originPath,  
  58.                         savePath);  
  59.             }  
  60.               
  61.             outputJson("图片处理成功");  
  62.               
  63.         } catch (Exception e) {  
  64.               
  65.             outputJson("图片处理失败");  
  66.         }  
  67.           
  68.         return null;  
  69.     }  
  70.       
  71.     /** 
  72.      * 直接转发imgscissor.jsp页面 
  73.      * @return 
  74.      */  
  75.     public String toScissor() {  
  76.         return SUCCESS;  
  77.     }  
  78.       
  79.     /**  
  80.     * 输出JSON信息  
  81.     * @param jsonObj  
  82.     */    
  83.     private void outputJson(Object jsonObj){    
  84.   
  85.         HttpServletResponse response = ServletActionContext.getResponse();    
  86.         response.setContentType("application/json;charset=utf-8");    
  87.         response.setHeader("Cache-Control""no-cache");    
  88.             
  89.         try {  
  90.               
  91.             PrintWriter pw = response.getWriter();    
  92.             //将Java对象转换为JSON字符串    
  93.             String gsonStr = new Gson().toJson(jsonObj);    
  94.             //输出JSON字符串    
  95.             pw.print(gsonStr);    
  96.                 
  97.             pw.flush();    
  98.             pw.close();    
  99.               
  100.         } catch (IOException e) {    
  101.             System.out.println("输出GSON出错:" + e);    
  102.         }  
  103.     }  
  104.   
  105.     public void setX1(Integer x1) {  
  106.         this.x1 = x1;  
  107.     }  
  108.   
  109.     public void setX2(Integer x2) {  
  110.         this.x2 = x2;  
  111.     }  
  112.   
  113.     public void setY1(Integer y1) {  
  114.         this.y1 = y1;  
  115.     }  
  116.   
  117.     public void setY2(Integer y2) {  
  118.         this.y2 = y2;  
  119.     }  
  120.   
  121.     public void setOriginPath(String originPath) {  
  122.         this.originPath = originPath;  
  123.     }  
  124.   
  125.     public void setSavePath(String savePath) {  
  126.         this.savePath = savePath;  
  127.     }  
  128.   
  129.     public String getOriginPath() {  
  130.         return originPath;  
  131.     }  
  132.   
  133.     public void setScaleWidth(Integer scaleWidth) {  
  134.         this.scaleWidth = scaleWidth;  
  135.     }  
  136.   
  137.     public void setScaleHeight(Integer scaleHeight) {  
  138.         this.scaleHeight = scaleHeight;  
  139.     }  
  140.   
  141.     public String getProportion() {  
  142.         return proportion;  
  143.     }  
  144.   
  145.     public void setProportion(String proportion) {  
  146.         this.proportion = proportion;  
  147.     }  
  148.       
  149. }  

 

 9、在applicationContext-actions.xml中配置ImageScissorAction,由Spring管理,配置如下:

  Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.   
  6.     <!-- 创建FilmAction -->  
  7.     <bean id="filmAction"      
  8.         class="com.xxx.web.struts.action.FilmAction"  
  9.         scope="prototype"/>  
  10.       
  11.     <!-- 创建ImageScissorAction -->  
  12.     <bean id="imageScissorAction"      
  13.         class="com.xxx.web.struts.action.ImageScissorAction"  
  14.         scope="prototype"/>  
  15.       
  16. </beans>  

 

10、在struts.xml中配置ImageScissorAction访问名称与方法的对应,配置如下:

  Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  11.     <constant name="struts.objectFactory" value="spring" />  
  12.     <constant name="struts.objectFactory.spring.autoWire" value="type" />  
  13.     <constant name="struts.ui.theme" value="simple"></constant>  
  14.       
  15.     <package name="film" namespace="/film" extends="struts-default">  
  16.           
  17.         <!-- 获取所有影片 -->  
  18.         <action name="findFilm" class="filmAction" method="findFilm">  
  19.             <result name="success">/manager/films.jsp</result>  
  20.         </action>  
  21.         <!-- 添加影片 -->  
  22.         <action name="insertFilm" class="filmAction" method="insertFilm">  
  23.             <!-- 默认拦截器 -->  
  24.             <interceptor-ref name="defaultStack" />  
  25.             <!-- 文件上传拦截器 -->  
  26.             <interceptor-ref name="fileUploadStack">  
  27.                 <!-- 配置允许上传的文件大小,单位字节(默认2M) -->  
  28.                 <param name="maximumSize">10000000</param>  
  29.                 <param name="allowedTypes">  
  30.                     image/bmp,image/png,image/gif,image/jpeg  
  31.                 </param>  
  32.             </interceptor-ref>  
  33.             <result name="success" type="redirectAction">findFilm</result>  
  34.         </action>  
  35.         <!-- 获取影片详情 -->  
  36.         <action name="detailFilm" class="filmAction" method="detailFilm">  
  37.             <result name="success">/manager/updateFilm.jsp</result>  
  38.         </action>  
  39.         <!-- 修改影片 -->  
  40.         <action name="updateFilm" class="filmAction" method="updateFilm">  
  41.             <!-- 默认拦截器 -->  
  42.             <interceptor-ref name="defaultStack" />  
  43.             <!-- 文件上传拦截器 -->  
  44.             <interceptor-ref name="fileUploadStack">  
  45.                 <!-- 配置允许上传的文件大小,单位字节(默认2M) -->  
  46.                 <param name="maximumSize">10000000</param>  
  47.                 <param name="allowedTypes">  
  48.                     image/bmp,image/png,image/gif,image/jpeg  
  49.                 </param>  
  50.             </interceptor-ref>  
  51.             <result name="success" type="redirectAction">findFilm</result>  
  52.         </action>  
  53.         <!-- 删除影片 -->  
  54.         <action name="deleteFilm" class="filmAction" method="deleteFilm">  
  55.             <result name="success" type="redirectAction">findFilm</result>  
  56.         </action>  
  57.           
  58.         <!-- 图片剪切、压缩处理Action -->  
  59.         <action name="toScissor" class="imageScissorAction" method="toScissor">  
  60.             <result name="success">/manager/imgscissor.jsp</result>  
  61.         </action>  
  62.         <action name="scissor" class="imageScissorAction">  
  63.             <!-- 响应类型为text/html -->  
  64.             <param name="contentType">text/html</param>  
  65.         </action>  
  66.           
  67.     </package>  
  68.       
  69. </struts>  

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值