1 16 17 package org.springframework.mock.web; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 import org.springframework.util.Assert; 25 import org.springframework.util.FileCopyUtils; 26 import org.springframework.web.multipart.MultipartFile; 27 28 40 public class MockMultipartFile implements MultipartFile { 41 42 private final String name; 43 44 private String originalFilename; 45 46 private String contentType; 47 48 private final byte[] content; 49 50 51 56 public MockMultipartFile(String name, byte[] content) { 57 this(name, "", null, content); 58 } 59 60 66 public MockMultipartFile(String name, InputStream contentStream) throws IOException { 67 this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); 68 } 69 70 77 public MockMultipartFile(String name, String originalFilename, String contentType, byte[] content) { 78 Assert.hasLength(name, "Name must not be null"); 79 this.name = name; 80 this.originalFilename = (originalFilename != null ? originalFilename : ""); 81 this.contentType = contentType; 82 this.content = (content != null ? content : new byte[0]); 83 } 84 85 93 public MockMultipartFile(String name, String originalFilename, String contentType, InputStream contentStream) 94 throws IOException { 95 96 this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); 97 } 98 99 100 public String getName() { 101 return this.name; 102 } 103 104 public String getOriginalFilename() { 105 return this.originalFilename; 106 } 107 108 public String getContentType() { 109 return this.contentType; 110 } 111 112 public boolean isEmpty() { 113 return (this.content.length == 0); 114 } 115 116 public long getSize() { 117 return this.content.length; 118 } 119 120 public byte[] getBytes() throws IOException { 121 return this.content; 122 } 123 124 public InputStream getInputStream() throws IOException { 125 return new ByteArrayInputStream (this.content); 126 } 127 128 public void transferTo(File dest) throws IOException , IllegalStateException { 129 FileCopyUtils.copy(this.content, dest); 130 } 131 132 } 133 | Popular Tags |