View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Binary body part backed by a file.
43   *
44   * @see MultipartEntityBuilder
45   *
46   * @since 4.0
47   */
48  public class FileBody extends AbstractContentBody {
49  
50      private final File file;
51      private final String filename;
52  
53      /**
54       * @since 4.1
55       *
56       * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType, String)}
57       *   or {@link MultipartEntityBuilder}
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       * @since 4.1
69       *
70       * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)}
71       *   or {@link MultipartEntityBuilder}
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       * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)}
82       *   or {@link MultipartEntityBuilder}
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       * @since 4.3
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      * @since 4.3
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 }