2
peternameyakj
2024-08-14 9bcf85f8d8f9b503e386a67924f5943cd77bd813
1
2
3
4
5
6
7
8
9
10
11
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
 * 查找图片地址
 */
package kernel.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import kernel.springframework.ServiceLocator;
 
/**
 * @author cwj
 */
public class ImageDispatcher {
    
    private static boolean startUp;
    
    private static String saveDir;
    
    private static List viewDirs;
    
    //查找缩略图 但是原图片不存在!
    private static Map<String, String> noFindImgZoom ;
    
    private static List noFindImgZoomNeed =new ArrayList();
    
    private static List noFindImgZoomSupply=new ArrayList() ;
    
    private static Properties properties = PropertiesLoaderUtils.loadProperties("config/system.properties");
 
    //查找图片, 但原图片不存在! 
    private static Map<String, String> noFindImg;
    
    private static Map<String,String> noFindPerson;
    
    public static String getSaveDir() {
        return saveDir;
    }
    public static void setSaveDir(String saveDir) {
        ImageDispatcher.saveDir = saveDir;
    }
    public static boolean isStartUp() {
        return startUp;
    }
    public static void setStartUp(String startUp) {
        if ("true".equals(startUp)) {
            ImageDispatcher.startUp = true;
        } else {
            ImageDispatcher.startUp = false;
        }
    }
    public static List getViewDirs() {
        return viewDirs;
    }
    public static void setViewDirs(List viewDirs) {
        ImageDispatcher.viewDirs = viewDirs;
    }
    
    /**
     * 响应客户端请求的图片的过滤器处理过程
     * @param request
     * @param response
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    public static void doImageFilter(ServletRequest request, ServletResponse response, 
            FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpResp = (HttpServletResponse) response;
        String reqStr = httpReq.getServletPath();
        FileInputStream imgStream = null;
        OutputStream toClientStream = null;
 
        try {
            File file = findFile(reqStr);
            if (file != null) {
                imgStream = new FileInputStream(file);
                int i = imgStream.available(); //得到文件大小
                httpResp.setContentType("image/*");    //设置文件类型
                httpResp.setContentLength(i);        //设置文件大小
                toClientStream = httpResp.getOutputStream();    //得到输出流
                byte data[] = new byte[1024];
                //逐步输出数据
                while( (i = imgStream.read(data)) > 0){
                    toClientStream.write(data,0,i);
                    toClientStream.flush();
                }
            } else {
                filterChain.doFilter(request, response);
            }
        } catch(IOException e) {
            throw e;
        } finally {
            if (toClientStream != null) {
                toClientStream.close();
            }            
            if (imgStream != null) {
                imgStream.close();
            }        
        }
    }
 
    /**
     * 从数据库中得到图片地址    
     * @param filePath        /img/2007/10/18/15/T3NHUG4MRO1D.jpg
     * @param type            1,2,3
     * @return
     */
    public static String getImg(String filePath) {
        String imgPath = "";
        if(!StringUtils.isNullOrEmpty(filePath)&&filePath.indexOf("http://")!=-1){
            return filePath;
        }
        if (filePath == null) {
//            if (noFindImg == null || noFindImg.isEmpty()) {
//                noFindImg = new HashMap<String, String>();
//                noFindImg.put("1", "../sysImg/no-find-1.jpg");
//                noFindImg.put("2", "../sysImg/no-find-2.jpg");
//                noFindImg.put("3", "../sysImg/no-find-3.jpg");
//            }
//            imgPath = noFindImg.get(type);
//            return imgPath;
            return null;
        }
        
        
        File file = ImageDispatcher.findFile(filePath);
        if (file == null) {
            //原图片不存在!
            return null;
        } else {
            imgPath = ServiceLocator.getMessage("http.server.host") + filePath;
        }
        return imgPath;
    }
        
    
    /**
     * 根据相对路径查找真实路径,如果不存在返回空
     * @return 
     */
    public static File findFile(String absPath) {
        File file = null;
        if (startUp && saveDir != null) {
            file = new File(saveDir + absPath);
            if (file.isFile()) {
                return file;
            } else if (viewDirs != null) {
                for (Iterator it = viewDirs.iterator(); it.hasNext(); ) {
                    file = new File((String)it.next() + absPath);
                    if (file.isFile()) {
                        return file;
                    }
                }
            }
        } else {
            file = new File(properties.getProperty("images.dir") + absPath);
            if (file.isFile()) {
                return file;
            }
        }
        return null;
    }
 
    public static Map<String, String> getNoFindImgZoom() {
        return noFindImgZoom;
    }
    public static void setNotFindImgZoom(Map<String, String> noFindImgZoom) {
        ImageDispatcher.noFindImgZoom = noFindImgZoom;
    }
    public static Map<String, String> getNoFindImg() {
        return noFindImg;
    }
    public static void setNoFindImg(Map<String, String> noFindImg) {
        ImageDispatcher.noFindImg = noFindImg;
    }    
    
    /**
     * 删除一张原图
     * @param filePath
     */
    public static void delFile(String filePath){
        File file = findFile(filePath);
        if(file != null){
            file.delete();
        }
    }
 
 
    
    
    public static void main(String[] args) {
//        //System.out.println(ImageDispatcher.getNotImage());2007/10/18/15/T3NHUG4MRO1D.jpg
//        //System.out.println(ImageDispatcher.getImgZoom("", "2"));
//        //System.out.println(ImageDispatcher.getImg("", "2"));
        ImageDispatcher.delFile("2007/10/18/20/PT517L1U6841.jpg");
        
    }
    public static List getNoFindImgZoomNeed() {
        return noFindImgZoomNeed;
    }
    public static void setNoFindImgZoomNeed(List noFindImgZoomNeed) {
        ImageDispatcher.noFindImgZoomNeed = noFindImgZoomNeed;
    }
    public static List getNoFindImgZoomSupply() {
        return noFindImgZoomSupply;
    }
    public static void setNoFindImgZoomSupply(List noFindImgZoomSupply) {
        ImageDispatcher.noFindImgZoomSupply = noFindImgZoomSupply;
    }
 
}