01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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
34: ByteArrayPropertyEditor {
35:
36: /** Static to avoid creating a new logger every time */
37: private static final Log logger = LogFactory
38: .getLog(ByteArrayMultipartFileEditor.class);
39:
40: public void setValue(Object value) {
41: if (value instanceof MultipartFile) {
42: MultipartFile multipartFile = (MultipartFile) value;
43: try {
44: super .setValue(multipartFile.getBytes());
45: } catch (IOException ex) {
46: logger.warn("Cannot read contents of multipart file",
47: ex);
48: throw new IllegalArgumentException(
49: "Cannot read contents of multipart file: "
50: + ex.getMessage());
51: }
52: } else if (value instanceof byte[]) {
53: super .setValue(value);
54: } else {
55: super .setValue(value != null ? value.toString().getBytes()
56: : null);
57: }
58: }
59:
60: public String getAsText() {
61: byte[] value = (byte[]) getValue();
62: return (value != null ? new String(value) : "");
63: }
64:
65: }
|