Skip to content

Commit 6d0bdef

Browse files
author
ghc
committed
Branch:master
1.修改mimetype获取方式
1 parent 45bf242 commit 6d0bdef

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

EvolvingNet/src/main/java/com/codingcoderscode/lib/net/request/entity/CCFile.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package com.codingcoderscode.lib.net.request.entity;
22

33

4-
import android.text.TextUtils;
4+
import com.codingcoderscode.lib.net.util.CCFileUtils;
55

6-
import java.net.FileNameMap;
7-
import java.net.URLConnection;
8-
9-
import okhttp3.MediaType;
106
import okhttp3.RequestBody;
117

128
/**
@@ -22,21 +18,9 @@ public class CCFile {
2218
*/
2319
private String fileName;
2420

25-
public CCFile(String url){
21+
public CCFile(String url) {
2622
this.url = url;
27-
28-
if (TextUtils.isEmpty(url)){
29-
this.mimeType = "multipart/form-data;";
30-
}else {
31-
FileNameMap fileNameMap = URLConnection.getFileNameMap();
32-
//url = url.replace("#", ""); //解决文件名中含有#号异常的问题
33-
String contentType = fileNameMap.getContentTypeFor(url.replace("#", ""));
34-
if (contentType == null) {
35-
this.mimeType = MediaType.parse("application/octet-stream").toString();
36-
}
37-
this.mimeType = MediaType.parse(contentType).toString();
38-
}
39-
23+
this.mimeType = CCFileUtils.getMimeType(url);
4024
this.fileName = null;
4125
}
4226

EvolvingNet/src/main/java/com/codingcoderscode/lib/net/util/CCFileUtils.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import android.text.TextUtils;
44

55
import java.io.File;
6+
import java.net.FileNameMap;
7+
import java.net.URLConnection;
8+
9+
import okhttp3.MediaType;
610

711
/**
812
* Created by CodingCodersCode on 2017/10/31.
@@ -17,7 +21,7 @@ public class CCFileUtils {
1721
*/
1822
public static void deleteFile(String filePath) {
1923
try {
20-
if (!TextUtils.isEmpty(filePath)){
24+
if (!TextUtils.isEmpty(filePath)) {
2125
deleteFile(new File(filePath));
2226
}
2327
} catch (Exception e) {
@@ -40,4 +44,37 @@ public static void deleteFile(File file) {
4044
}
4145
}
4246

47+
/**
48+
* 获取File上传的mimeType
49+
*
50+
* @param filePath
51+
* @return
52+
*/
53+
public static String getMimeType(String filePath) {
54+
String mimeType = "";
55+
MediaType mediaType;
56+
try {
57+
if (TextUtils.isEmpty(filePath)) {
58+
mimeType = "multipart/form-data;";
59+
} else {
60+
FileNameMap fileNameMap = URLConnection.getFileNameMap();
61+
//url = url.replace("#", ""); //解决文件名中含有#号异常的问题
62+
String contentType = fileNameMap.getContentTypeFor(filePath.replace("#", ""));
63+
if (contentType == null) {
64+
mediaType = MediaType.parse("application/octet-stream");
65+
} else {
66+
mediaType = MediaType.parse(contentType);
67+
}
68+
69+
if (mediaType != null) {
70+
mimeType = mediaType.toString();
71+
} else {
72+
mimeType = "multipart/form-data;";
73+
}
74+
}
75+
} catch (Exception e) {
76+
77+
}
78+
return mimeType;
79+
}
4380
}

0 commit comments

Comments
 (0)