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.util.Args;
39  
40  /**
41   * Binary body part backed by a file.
42   *
43   * @see org.apache.http.entity.mime.MultipartEntityBuilder
44   *
45   * @since 4.0
46   */
47  public class FileBody extends AbstractContentBody {
48  
49      private final File file;
50      private final String filename;
51  
52      /**
53       * @since 4.1
54       *
55       * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType, String)}
56       *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
57       */
58      @Deprecated
59      public FileBody(final File file,
60                      final String filename,
61                      final String mimeType,
62                      final String charset) {
63          this(file, ContentType.create(mimeType, charset), filename);
64      }
65  
66      /**
67       * @since 4.1
68       *
69       * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)}
70       *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
71       */
72      @Deprecated
73      public FileBody(final File file,
74                      final String mimeType,
75                      final String charset) {
76          this(file, null, mimeType, charset);
77      }
78  
79      /**
80       * @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType)}
81       *   or {@link org.apache.http.entity.mime.MultipartEntityBuilder}
82       */
83      @Deprecated
84      public FileBody(final File file, final String mimeType) {
85          this(file, ContentType.create(mimeType), null);
86      }
87  
88      public FileBody(final File file) {
89          this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
90      }
91  
92      /**
93       * @since 4.3
94       */
95      public FileBody(final File file, final ContentType contentType, final String filename) {
96          super(contentType);
97          Args.notNull(file, "File");
98          this.file = file;
99          this.filename = filename == null ? file.getName() : filename;
100     }
101 
102     /**
103      * @since 4.3
104      */
105     public FileBody(final File file, final ContentType contentType) {
106         this(file, contentType, file != null ? file.getName() : null);
107     }
108 
109     public InputStream getInputStream() throws IOException {
110         return new FileInputStream(this.file);
111     }
112 
113     @Override
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     @Override
130     public String getTransferEncoding() {
131         return MIME.ENC_BINARY;
132     }
133 
134     @Override
135     public long getContentLength() {
136         return this.file.length();
137     }
138 
139     @Override
140     public String getFilename() {
141         return filename;
142     }
143 
144     public File getFile() {
145         return this.file;
146     }
147 
148 }