KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > common > UploadMultipartFilter


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.common;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.servlet.Filter JavaDoc;
27 import javax.servlet.FilterChain JavaDoc;
28 import javax.servlet.FilterConfig JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.ServletResponse JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.ServletContext JavaDoc;
34
35 import org.springframework.web.multipart.MultipartFile;
36 import org.springframework.web.multipart.MultipartHttpServletRequest;
37 import org.springframework.web.multipart.MultipartResolver;
38 import org.springframework.web.multipart.commons.CommonsMultipartResolver;
39 //import org.springframework.web.multipart.cos.CosMultipartResolver;
40

41 public class UploadMultipartFilter implements Filter JavaDoc{
42
43        public void doFilter(ServletRequest JavaDoc request,
44                             ServletResponse JavaDoc response,
45                             FilterChain JavaDoc chain)
46                     throws IOException JavaDoc, ServletException JavaDoc {
47                         
48           HttpServletRequest JavaDoc hRequest = (HttpServletRequest JavaDoc)request;
49           //Check whether we're dealing with a multipart request
50
MultipartResolver resolver= new CommonsMultipartResolver();
51               
52           // Giulio: If the getContentLength is -1, avoid to consider this
53
// message like a multipart request
54
if(resolver.isMultipart(hRequest) && hRequest.getContentLength() != -1){
55             
56               MultipartHttpServletRequest mreq = resolver.resolveMultipart(hRequest);
57                   
58                   if(mreq!=null){
59                   Iterator JavaDoc iterator=mreq.getFileNames();
60                   String JavaDoc fieldName=null;
61                   while(iterator.hasNext()){
62                       fieldName=(String JavaDoc)iterator.next();
63                       // Assuming only 1 file is uploaded per page
64
// can be modified to handle mulit uploads per request
65
}
66                   MultipartFile file=mreq.getFile(fieldName);
67                   if(file!=null){
68                       String JavaDoc fullName = file.getOriginalFilename();
69                       if(fullName!=null && fullName.trim().length()!=0){
70                           int lastIndex = fullName.lastIndexOf(".");
71                           if(lastIndex!=-1){
72                               String JavaDoc fileName=fullName.substring(0,lastIndex);
73                               String JavaDoc extension=fullName.substring(lastIndex+1);
74                               mreq.setAttribute(JasperServerConst.UPLOADED_FILE_NAME,fileName);
75                               mreq.setAttribute(JasperServerConst.UPLOADED_FILE_EXT,extension);
76                           }
77                       }
78                   }
79               }
80               chain.doFilter(mreq,response);
81           }else
82                
83             chain.doFilter(request,response);
84        }
85
86     public void init(FilterConfig JavaDoc arg0) throws ServletException JavaDoc {
87         // TODO Auto-generated method stub
88
}
89
90     public void destroy() {
91         // TODO Auto-generated method stub
92

93     }
94 }
Popular Tags