1.需要pom
org.apache.tika
tika-core
1.14
2.FileUtil类
public static File downloadFromUrl(String url ) {
try {
URL httpurl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)httpurl.openConnection();
// 获取文件后缀名
String contentType = conn.getContentType();
MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
MimeType jpeg = allTypes.forName(contentType);
String ext = jpeg.getExtension();
// 缓存本地
File f = new File(System.getProperty("user.dir") + "/fileName"+ext);
FileUtils.copyURLToFile(httpurl, f);
return f;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}



