forked from pcan/feign-client-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InMemoryMultipartFile.java
78 lines (64 loc) · 1.88 KB
/
InMemoryMultipartFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package it.pcan.test.feign.client;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
/**
*
* @author Pierantonio Cangianiello
*/
public class InMemoryMultipartFile implements MultipartFile {
private final String name;
private final String originalFileName;
private final String contentType;
private final byte[] payload;
public InMemoryMultipartFile(String originalFileName, byte[] payload) {
this.originalFileName = originalFileName;
this.payload = payload;
this.name = "file";
this.contentType = "application/octet-stream";
}
public InMemoryMultipartFile(String name, String originalFileName, String contentType, byte[] payload) {
if (payload == null) {
throw new IllegalArgumentException("Payload cannot be null.");
}
this.name = name;
this.originalFileName = originalFileName;
this.contentType = contentType;
this.payload = payload;
}
@Override
public String getName() {
return name;
}
@Override
public String getOriginalFilename() {
return originalFileName;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public boolean isEmpty() {
return payload.length == 0;
}
@Override
public long getSize() {
return payload.length;
}
@Override
public byte[] getBytes() throws IOException {
return payload;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(payload);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(payload);
}
}