FileUtils.java
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.com.glsx.entmicro.util;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author zhouhaibao
* @version 1.0
* @description: 读取文件流
* @date 2021/11/12 10:41
*/
public class FileUtils {
/**
* 根据url下载文件流
*
* @param urlStr
* @return
*/
public static InputStream getInputStreamFromUrl(String urlStr) {
InputStream inputStream = null;
try {
//url解码
URL url = new URL(java.net.URLDecoder.decode(urlStr, "UTF-8"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
inputStream = conn.getInputStream();
} catch (IOException e) {
e.getStackTrace();
}
return inputStream;
}
/**
* 获取文件扩展名
*
* @return
*/
public static String getFileType(String filePath) {
if(filePath.lastIndexOf(".") != -1 && filePath.lastIndexOf(".") != 0){
return filePath.substring(filePath.lastIndexOf("."));
}
return "";
}
}