Repository navigation
Expand file tree
/
Copy pathFilePara.java
More file actions
86 lines (70 loc) · 2.09 KB
/
Copy pathFilePara.java
File metadata and controls
86 lines (70 loc) · 2.09 KB
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
79
80
81
82
83
84
85
86
package cn.zhxu.okhttps;
import cn.zhxu.okhttps.internal.StreamRequestBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
/**
* @since 2.5.0 从 HttpTask 中分离
*/
public class FilePara {
private final String type;
private final String fileName;
private final byte[] content;
private final File file;
private final InputStream stream;
public FilePara(String type, String fileName, byte[] content) {
this(type, fileName, null, content, null);
}
public FilePara(String type, String fileName, File file) {
this(type, fileName, file, null, null);
}
public FilePara(String type, String fileName, InputStream stream) {
this(type, fileName, null, null, stream);
}
private FilePara(String type, String fileName, File file, byte[] content, InputStream stream) {
this.type = type;
this.fileName = fileName;
this.file = file;
this.content = content;
this.stream = stream;
}
public RequestBody toRequestBody(MediaType contentType) {
if (file != null) {
return RequestBody.create(contentType, file);
}
if (content != null) {
return RequestBody.create(contentType, content);
}
if (stream != null) {
return new StreamRequestBody(contentType, stream);
}
throw new IllegalStateException("Invalid FilePara");
}
public String getType() {
return type;
}
public String getFileName() {
return fileName;
}
public byte[] getContent() {
return content;
}
public File getFile() {
return file;
}
public InputStream getStream() {
return stream;
}
@Override
public String toString() {
return "FilePara{" +
"type='" + type + '\'' +
", fileName='" + fileName + '\'' +
", content=" + Arrays.toString(content) +
", file=" + file +
", stream=" + stream +
'}';
}
}