KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > multipart > support > ByteArrayMultipartFileEditor


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

16
17 package org.springframework.web.multipart.support;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.springframework.beans.propertyeditors.ByteArrayPropertyEditor;
25 import org.springframework.web.multipart.MultipartFile;
26
27 /**
28  * Custom PropertyEditor for converting MultipartFiles to byte arrays.
29  *
30  * @author Juergen Hoeller
31  * @since 13.10.2003
32  */

33 public class ByteArrayMultipartFileEditor extends ByteArrayPropertyEditor {
34
35     /** Static to avoid creating a new logger every time */
36     private static final Log logger = LogFactory.getLog(ByteArrayMultipartFileEditor.class);
37
38
39     public void setValue(Object JavaDoc value) {
40         if (value instanceof MultipartFile) {
41             MultipartFile multipartFile = (MultipartFile) value;
42             try {
43                 super.setValue(multipartFile.getBytes());
44             }
45             catch (IOException JavaDoc ex) {
46                 logger.warn("Cannot read contents of multipart file", ex);
47                 throw new IllegalArgumentException JavaDoc("Cannot read contents of multipart file: " + ex.getMessage());
48             }
49         }
50         else if (value instanceof byte[]) {
51             super.setValue(value);
52         }
53         else {
54             super.setValue(value != null ? value.toString().getBytes() : null);
55         }
56     }
57
58     public String JavaDoc getAsText() {
59         byte[] value = (byte[]) getValue();
60         return (value != null ? new String JavaDoc(value) : "");
61     }
62
63 }
64
Popular Tags