added headers support to MultipartFile abstraction
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.web.multipart;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A representation of an uploaded file received in a multipart request.
|
||||
@@ -101,4 +102,28 @@ public interface MultipartFile {
|
||||
*/
|
||||
void transferTo(File dest) throws IOException, IllegalStateException;
|
||||
|
||||
/**
|
||||
* Return the first value associated with the given header name, if any.
|
||||
* @param name the name of the header
|
||||
* @return the first header value, or <code>null</code> if no such header was found
|
||||
* @since 3.1
|
||||
*/
|
||||
String getHeader(String name);
|
||||
|
||||
/**
|
||||
* Return all values associated with the given header name, if any.
|
||||
* @param name the name of the header
|
||||
* @return the header values as an array, or <code>null</code> if no such header was found
|
||||
* @since 3.1
|
||||
*/
|
||||
String[] getHeaders(String name);
|
||||
|
||||
/**
|
||||
* Return an {@link java.util.Iterator} of Strings containing the
|
||||
* names of headers associated with this file.
|
||||
* @return the names of the headers
|
||||
* @since 3.1
|
||||
*/
|
||||
Iterator<String> getHeaderNames(String name);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,8 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.FileUploadException;
|
||||
@@ -33,10 +35,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
/**
|
||||
* MultipartFile implementation for Jakarta Commons FileUpload.
|
||||
*
|
||||
* <p><b>NOTE:</b> As of Spring 2.0, this class requires Commons FileUpload 1.1
|
||||
* or higher. The implementation does not use any deprecated FileUpload 1.0 API
|
||||
* anymore, to be compatible with future Commons FileUpload releases.
|
||||
*
|
||||
* @author Trevor D. Cook
|
||||
* @author Juergen Hoeller
|
||||
* @since 29.09.2003
|
||||
@@ -44,6 +42,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
*/
|
||||
public class CommonsMultipartFile implements MultipartFile, Serializable {
|
||||
|
||||
private static final String CONTENT_TYPE = "Content-Type";
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(CommonsMultipartFile.class);
|
||||
|
||||
private final FileItem fileItem;
|
||||
@@ -191,4 +191,26 @@ public class CommonsMultipartFile implements MultipartFile, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
public String getHeader(String name) {
|
||||
if (CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
return this.fileItem.getContentType();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getHeaders(String name) {
|
||||
if (CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
return new String[] {this.fileItem.getContentType()};
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<String> getHeaderNames(String name) {
|
||||
return Collections.singleton(CONTENT_TYPE).iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
@@ -29,6 +30,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
@@ -149,6 +151,19 @@ public class StandardServletMultipartResolver implements MultipartResolver {
|
||||
public void transferTo(File dest) throws IOException, IllegalStateException {
|
||||
this.part.write(dest.getPath());
|
||||
}
|
||||
|
||||
public String getHeader(String name) {
|
||||
return this.part.getHeader(name);
|
||||
}
|
||||
|
||||
public String[] getHeaders(String name) {
|
||||
Collection<String> headers = this.part.getHeaders(name);
|
||||
return (headers != null ? StringUtils.toStringArray(headers) : null);
|
||||
}
|
||||
|
||||
public Iterator<String> getHeaderNames(String name) {
|
||||
return this.part.getHeaderNames().iterator();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,8 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
@@ -39,6 +41,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
*/
|
||||
public class MockMultipartFile implements MultipartFile {
|
||||
|
||||
private static final String CONTENT_TYPE = "Content-Type";
|
||||
|
||||
private final String name;
|
||||
|
||||
private String originalFilename;
|
||||
@@ -61,7 +65,7 @@ public class MockMultipartFile implements MultipartFile {
|
||||
* Create a new MockMultipartFile with the given content.
|
||||
* @param name the name of the file
|
||||
* @param contentStream the content of the file as stream
|
||||
* @throws java.io.IOException if reading from the stream failed
|
||||
* @throws IOException if reading from the stream failed
|
||||
*/
|
||||
public MockMultipartFile(String name, InputStream contentStream) throws IOException {
|
||||
this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
|
||||
@@ -88,7 +92,7 @@ public class MockMultipartFile implements MultipartFile {
|
||||
* @param originalFilename the original filename (as on the client's machine)
|
||||
* @param contentType the content type (if known)
|
||||
* @param contentStream the content of the file as stream
|
||||
* @throws java.io.IOException if reading from the stream failed
|
||||
* @throws IOException if reading from the stream failed
|
||||
*/
|
||||
public MockMultipartFile(String name, String originalFilename, String contentType, InputStream contentStream)
|
||||
throws IOException {
|
||||
@@ -129,4 +133,26 @@ public class MockMultipartFile implements MultipartFile {
|
||||
FileCopyUtils.copy(this.content, dest);
|
||||
}
|
||||
|
||||
}
|
||||
public String getHeader(String name) {
|
||||
if (CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
return this.contentType;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getHeaders(String name) {
|
||||
if (CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
return new String[] {this.contentType};
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<String> getHeaderNames(String name) {
|
||||
return Collections.singleton(CONTENT_TYPE).iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user