KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > editor > FileUploadController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.editor;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.springframework.web.multipart.MultipartFile;
33 import org.springframework.web.multipart.MultipartHttpServletRequest;
34 import org.springframework.web.servlet.ModelAndView;
35 import org.springframework.web.servlet.mvc.Controller;
36
37 /**
38  * @author Felix Gnass [fgnass at neteye dot de]
39  * @since 6.5
40  */

41 public class FileUploadController implements Controller {
42
43     private String JavaDoc fileParam = "Filedata";
44     
45     private String JavaDoc tokenParam = "token";
46     
47     private UploadManagerImpl uploadManager;
48     
49     public FileUploadController(UploadManagerImpl uploadManager) {
50         this.uploadManager = uploadManager;
51     }
52
53     public ModelAndView handleRequest(HttpServletRequest JavaDoc request,
54             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
55         
56         if (request instanceof MultipartHttpServletRequest) {
57             handleMultipartRequest((MultipartHttpServletRequest) request, response);
58         }
59         return null;
60     }
61     
62     protected void handleMultipartRequest(MultipartHttpServletRequest request,
63             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
64
65         String JavaDoc token = request.getParameter(tokenParam);
66         if (uploadManager.isValidToken(token)) {
67             MultipartFile multipartFile = request.getFile(fileParam);
68             if ((multipartFile != null) && (!multipartFile.isEmpty())) {
69                 String JavaDoc fileName = multipartFile.getOriginalFilename();
70                 File JavaDoc tempFile = File.createTempFile("upload", ".bin");
71                 multipartFile.transferTo(tempFile);
72                 uploadManager.storeFile(token, tempFile, fileName);
73             }
74         }
75         else {
76             response.sendError(HttpServletResponse.SC_FORBIDDEN);
77         }
78     }
79     
80 }
81
Popular Tags