get File Name From Http Response - Android Network

Android examples for Network:HTTP Response

Description

get File Name From Http Response

Demo Code

/*/*from   w  w  w . ja  v  a 2s  .c  o  m*/
 * Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Locale;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class Main{
    public static String getFileNameFromHttpResponse(
            final HttpURLConnection conn) {
        if (conn == null)
            return null;
        String result = null;
        List<String> header = conn.getHeaderFields().get(
                "Content-Disposition");
        if (header != null) {
            for (String str : header) {
                if (str.equals("filename")) {
                    result = conn.getHeaderField(str);
                    // try to get correct encoding str
                    result = CharsetUtils.toCharset(result, "UTF-8",
                            result.length());
                    break;
                }
            }
        }
        return result;
    }
}

Related Tutorials