文章标题

来源:互联网 发布:pscad软件介绍 编辑:程序博客网 时间:2024/06/14 22:59

android 上传大文件及数据的httpUtiles

使用:MultipartEntity

    // 上传头像/ 附件    public static String postMethod(String mUrl, String authorization,            HashMap<String, String> textMap, List<String> imageUrlList,            String filePath) {        String type = getMimeType(filePath);        System.out.println("type:" + type);        try {            // 链接超时,请求超时设置            BasicHttpParams httpParams = new BasicHttpParams();            HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);            HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000);            // 请求参数设置            HttpClient client = new DefaultHttpClient(httpParams);            HttpPost post = new HttpPost(mUrl);            post.setHeader("authorization", authorization);            MultipartEntity entity = new MultipartEntity(                    HttpMultipartMode.BROWSER_COMPATIBLE, null,                    Charset.forName("UTF-8"));// 编码格式 必须这里设定            // 上传 文本, 转换编码为utf-8 其中"text" 为字段名,            // 后边new StringBody(text,            // Charset.forName(CHARSET))为参数值,其实就是正常的值转换成utf-8的编码格式            if (textMap != null && !textMap.equals("")) {                Iterator iter = textMap.entrySet().iterator();                while (iter.hasNext()) {                    Map.Entry entry = (Map.Entry) iter.next();                    String key = entry.getKey().toString();                    String val = entry.getValue().toString();                    entity.addPart(key,                            new StringBody(val, Charset.forName("UTF-8")));                }                // 上传多个文本可以在此处添加上边代码,修改字段和值即可            }            if (filePath != null && !filePath.equals("")) {                // 上传文件                entity.addPart("file", new FileBody(new File(filePath),                        getMimeType(filePath), "UTF-8"));            }            if (imageUrlList != null) {                String[] imagePath = new String[imageUrlList.size()];                int size = imageUrlList.size();                for (int i = 0; i < size; i++) {                    imagePath[i] = imageUrlList.get(i);                }                if (imagePath != null && imagePath.length > 0) {                    // 上传图片                    for (String p : imagePath) {                        entity.addPart("image", new FileBody(new File(p),                                getMimeType(p), "UTF-8"));                    }                }            }            post.setEntity(entity);            HttpResponse resp = client.execute(post);            (resp.getEntity());            int code = resp.getStatusLine().getStatusCode();            if (code == 200) {                InputStream mInputStream = resp.getEntity().getContent();                return AppUtils.InputStreamTOString(mInputStream );            } else if (code == 401) {                return "401";// 登录失效            } else {                return null;            }        } catch (Exception e) {            System.out.println(e.toString());            return null;        }    }

android获取文件getMimeType的两种方法

##

文件上传需要指定MimeType

第一种:import java.util.Locale;private static String getSuffix(File file) {            if (file == null || !file.exists() || file.isDirectory()) {                return null;            }            String fileName = file.getName();            if (fileName.equals("") || fileName.endsWith(".")) {                return null;            }            int index = fileName.lastIndexOf(".");            if (index != -1) {                return fileName.substring(index + 1).toLowerCase(Locale.US);            } else {                return null;            }    }    public static String getMimeType(File file){          String suffix = getSuffix(file);            if (suffix == null) {                return "file/*";            }            String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix);            if (type != null || !type.isEmpty()) {                return type;            }            return "file/*";}
第二种:public static String getMimeType(String filePath) {    MediaMetadataRetriever mmr = new MediaMetadataRetriever();    String mime = "text/plain";    if (filePath != null) {         try {             mmr.setDataSource(filePath);             mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);         } catch (IllegalStateException e) {        return mime;         } catch (IllegalArgumentException e) {        return mime;    } catch (RuntimeException e) {        return mime;    }    }    return mime;}
0 0