KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > file > ImageShowAction


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.strutsutil.file;
17
18 import org.apache.struts.action.*;
19 import javax.servlet.http.*;
20
21 import java.io.*;
22 import com.jdon.util.Debug;
23
24 import com.jdon.strutsutil.file.UploadFile;
25 import com.jdon.strutsutil.file.filter.*;
26
27 import com.jdon.util.UtilValidate;
28
29 /**
30  * 显示图片 Action
31  * 需è¦?继承实现。
32  *
33  * å?‚æ•°id, æ¯?个图片的id
34  * å?‚数为tempId,å?–出HttpSession中当å‰?图片
35  *
36  * <action type="com.jdon.estore.web.catalog.ProductImgShowAction" validate="false" path="/imageShowAction" />
37  *
38  * <p>Copyright: Jdon.com Copyright (c) 2003</p>
39  * <p></p>
40  * @author banq
41  * @version 1.0
42  */

43 public abstract class ImageShowAction extends Action {
44   public final static String JavaDoc module = ImageShowAction.class.getName();
45
46   private static final byte[] BLANK = {
47       71, 73, 70, 56, 57, 97, 1, 0, 1, 0, -111, 0, 0, 0, 0, 0, -1, -1, -1, -1,
48       -1, -1, 0, 0, 0, 33, -7, 4, 1, 0, 0, 2, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0,
49       0, 2, 2, 76, 1, 0, 59};
50
51   public ActionForward execute(ActionMapping actionMapping,
52                                ActionForm actionForm,
53                                HttpServletRequest request,
54                                HttpServletResponse response) throws
55       Exception JavaDoc {
56
57     String JavaDoc Id = request.getParameter("id");
58     String JavaDoc tempId = request.getParameter("tempId");
59
60     String JavaDoc imageId = null;
61     if (!UtilValidate.isEmpty(tempId))
62       imageId = tempId;
63     else if (!UtilValidate.isEmpty(Id))
64       imageId = Id;
65     else
66       throw new Exception JavaDoc("parameter id and tempId all is null");
67
68     UploadFile uploadFile = null;
69     try {
70
71       ImageFilter imageCacheFilter = new ImageCacheFilter(getImageFilter());
72       ImageFilter imageFilter = new ImageSessionFilter(imageCacheFilter);
73
74       uploadFile = imageFilter.getUploadFile(request, imageId);
75       if (uploadFile != null)
76         outImage(response, uploadFile.getData());
77       else
78         outImage(response, BLANK);
79     } catch (Exception JavaDoc ex) {
80       Debug.logError("[JdonFramework]get the image error:" + ex, module);
81
82     }
83     return null;
84   }
85
86   private void outImage(HttpServletResponse response, byte[] data) throws
87       Exception JavaDoc {
88     response.setContentType("images/jpeg");
89     OutputStream toClient = response.getOutputStream();
90     try {
91
92       toClient.write(data);
93
94     } catch (Exception JavaDoc ex) {
95       Debug.logError("[JdonFramework]get the image error:" + ex, module);
96     } finally {
97       toClient.close();
98     }
99
100   }
101
102   /**
103    * 从æŒ?久层获得图片数æ?®ã€‚
104    * @param request
105    * @return byte[]
106    */

107   public abstract ImageFilter getImageFilter();
108
109 }
110
Popular Tags