解决网络图片不展示的问题

This commit is contained in:
季圣华
2023-07-06 23:59:52 +08:00
parent 9d98db0eda
commit 810978ff0e
3 changed files with 34 additions and 4 deletions

View File

@@ -2,6 +2,8 @@ package com.jsh.erp.utils;
import org.springframework.util.StringUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
/**
@@ -352,4 +354,25 @@ public class FileUtils {
return fileName;
}
//将Url转换为File
public static File UrltoFile(String url) throws Exception {
HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
httpUrl.connect();
InputStream ins=httpUrl.getInputStream();
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "xie");//System.getProperty("java.io.tmpdir")缓存
if (file.exists()) {
file.delete();//如果缓存中存在该文件就删除
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}
}