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 package org.apache.http.entity.mime.content;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.entity.mime.MIME;
38 import org.apache.http.entity.mime.MultipartEntityBuilder;
39 import org.apache.http.util.Args;
40
41
42
43
44
45
46
47
48 public class FileBody extends AbstractContentBody {
49
50 private final File file;
51 private final String filename;
52
53
54
55
56
57
58
59 @Deprecated
60 public FileBody(final File file,
61 final String filename,
62 final String mimeType,
63 final String charset) {
64 this(file, ContentType.create(mimeType, charset), filename);
65 }
66
67
68
69
70
71
72
73 @Deprecated
74 public FileBody(final File file,
75 final String mimeType,
76 final String charset) {
77 this(file, null, mimeType, charset);
78 }
79
80
81
82
83
84 @Deprecated
85 public FileBody(final File file, final String mimeType) {
86 this(file, ContentType.create(mimeType), null);
87 }
88
89 public FileBody(final File file) {
90 this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
91 }
92
93
94
95
96 public FileBody(final File file, final ContentType contentType, final String filename) {
97 super(contentType);
98 Args.notNull(file, "File");
99 this.file = file;
100 this.filename = filename;
101 }
102
103
104
105
106 public FileBody(final File file, final ContentType contentType) {
107 this(file, contentType, null);
108 }
109
110 public InputStream getInputStream() throws IOException {
111 return new FileInputStream(this.file);
112 }
113
114 public void writeTo(final OutputStream out) throws IOException {
115 Args.notNull(out, "Output stream");
116 final InputStream in = new FileInputStream(this.file);
117 try {
118 final byte[] tmp = new byte[4096];
119 int l;
120 while ((l = in.read(tmp)) != -1) {
121 out.write(tmp, 0, l);
122 }
123 out.flush();
124 } finally {
125 in.close();
126 }
127 }
128
129 public String getTransferEncoding() {
130 return MIME.ENC_BINARY;
131 }
132
133 public long getContentLength() {
134 return this.file.length();
135 }
136
137 public String getFilename() {
138 return filename;
139 }
140
141 public File getFile() {
142 return this.file;
143 }
144
145 }