|
@@ -164,16 +164,63 @@ public class FileUtils {
|
|
|
return (MultipartFile) new CommonsMultipartFile(item);
|
|
|
}
|
|
|
|
|
|
- public static File getFile(String url, String type) throws Exception {
|
|
|
+ public static File getFile(String url, String fileName) throws Exception {
|
|
|
+ String name = "";
|
|
|
+ if (fileName.contains(".")) {
|
|
|
+ name = fileName.substring(0, fileName.indexOf("."));
|
|
|
+ }
|
|
|
+ //读取图片类型
|
|
|
+ String fileType = url.substring(url.lastIndexOf("."), url.length());
|
|
|
+ File file = null;
|
|
|
+
|
|
|
+ URL urlfile;
|
|
|
+ InputStream inStream = null;
|
|
|
+ OutputStream os = null;
|
|
|
+ try {
|
|
|
+ file = File.createTempFile(name, fileType);
|
|
|
+ //获取文件
|
|
|
+ urlfile = new URL(url);
|
|
|
+ inStream = urlfile.openStream();
|
|
|
+ os = new FileOutputStream(file);
|
|
|
+
|
|
|
+ int bytesRead = 0;
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (null != os) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ if (null != inStream) {
|
|
|
+ inStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File getFile1(String url, String fileName) throws Exception {
|
|
|
+ String name = "";
|
|
|
+ if (fileName.contains(".")) {
|
|
|
+ name = fileName.substring(0, fileName.indexOf("."));
|
|
|
+ }
|
|
|
//读取图片类型
|
|
|
- String fileName = url.substring(url.lastIndexOf("."), url.length());
|
|
|
+ String fileType = url.substring(url.lastIndexOf("."), url.length());
|
|
|
File file = null;
|
|
|
|
|
|
URL urlfile;
|
|
|
InputStream inStream = null;
|
|
|
OutputStream os = null;
|
|
|
try {
|
|
|
- file = File.createTempFile("new_url", type);
|
|
|
+ file = new File(name + fileType);
|
|
|
//获取文件
|
|
|
urlfile = new URL(url);
|
|
|
inStream = urlfile.openStream();
|
|
@@ -252,8 +299,8 @@ public class FileUtils {
|
|
|
return savedFile;
|
|
|
}
|
|
|
|
|
|
- public static MultipartFile urlToMultipartFile(String url, String type) throws Exception {
|
|
|
- File file = FileUtils.getFile(url, type);
|
|
|
+ public static MultipartFile urlToMultipartFile(String url, String fileName) throws Exception {
|
|
|
+ File file = FileUtils.getFile1(url, fileName);
|
|
|
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(16, null);
|
|
|
FileItem item = diskFileItemFactory.createItem(file.getName(), "text/plain", true, file.getName());
|
|
|
int bytesRead = 0;
|