update:优化url上传,修复多个问题

This commit is contained in:
hellohao
2023-06-22 19:12:53 +08:00
parent 7906948ab7
commit c960d2a812
4 changed files with 139 additions and 49 deletions
@@ -47,7 +47,7 @@ public class IndexController {
@RequestMapping(value = "/")
public String Welcome(Model model, HttpServletRequest httpServletRequest) {
model.addAttribute("name", "服务端程序(开源版)");
model.addAttribute("version", "20230320");
model.addAttribute("version", "20230622");
model.addAttribute("ip", GetIPS.getIpAddr(httpServletRequest));
model.addAttribute("links", "https://github.com/Hello-hao/tbed");
return "welcome";
@@ -146,6 +146,7 @@ public class IndexController {
JSONObject jsonObj = JSONObject.parseObject(data);
Integer setday = jsonObj.getInteger("day");
String imgUrl = jsonObj.getString("imgUrl");
String referer = jsonObj.getString("referer");
String[] URLArr = imgUrl.split("\n");
Subject subject = SecurityUtils.getSubject();
@@ -165,7 +166,10 @@ public class IndexController {
for (int i = 0; i < URLArr.length; i++) {
int temp = i + 1;
if (syscounts >= temp) {
Msg msg = uploadServicel.uploadForLoc(request, null,"URL转存图像", setday, URLArr[i],null);
JSONObject imgJson = new JSONObject();
imgJson.put("imgUrl",URLArr[i]);
imgJson.put("referer",referer);
Msg msg = uploadServicel.uploadForLoc(request, null,"URL转存图像", setday, imgJson,null);
if (!msg.getCode().equals("200")) {
errcounts++;
} else {
@@ -53,7 +53,7 @@ public class UploadServicel {
File file,
String imgName,
Integer setday,
String imgUrl,
JSONObject imgJson,
String md5key) {
Msg msg = new Msg();
try {
@@ -69,8 +69,8 @@ public class UploadServicel {
u = userMapper.getUsers(u);
}
Integer sourceKeyId = 0;
if (imgUrl != null) {
Msg imgData = uploadForURL(request, imgUrl);
if (imgJson != null) {
Msg imgData = uploadForURL(request, imgJson.getString("imgUrl"),imgJson.getString("referer"));
if (imgData.getCode().equals("200")) {
file = new File((String) imgData.getData());
} else {
@@ -254,42 +254,49 @@ public class UploadServicel {
}
public static Msg uploadForURL(HttpServletRequest request, String imgurl) {
Msg msg = new Msg();
if (true) {
Long imgsize = 0L;
try {
if (imgsize == 0) {
String ShortUID = SetText.getShortUuid();
String savePath =
request.getSession().getServletContext().getRealPath("/")
+ File.separator
+ "hellohaotmp"
+ File.separator;
Map<String, Object> bl = ImgUrlUtil.downLoadFromUrl(imgurl, ShortUID, savePath);
if ((Boolean) bl.get("res") == true) {
msg.setCode("200");
msg.setData(bl.get("imgPath"));
return msg;
} else {
if (bl.get("StatusCode").equals("110403")) {
msg.setInfo("该链接非图像文件,无法上传");
} else {
msg.setInfo("该链接暂时无法上传");
}
msg.setCode("500");
}
public static Msg uploadForURL(HttpServletRequest request, String imgurls,String referer) {
final Msg msg = new Msg();
String url = imgurls;
// 先判断是不是有效链接 并且自动判断协议头http(s)://
GetProtocol getProtocol = new GetProtocol();
String protocol = getProtocol.getProtocol(url,referer);
if (protocol == null) {
msg.setInfo("服务器解析该链接失败");
msg.setCode("500");
return msg;
} else {
url = protocol;
}
Long imgsize = 0L;
try {
if (imgsize == 0) {
String ShortUID = SetText.getShortUuid();
String savePath =
request.getSession().getServletContext().getRealPath("/")
+ File.separator
+ "hellohaotmp"
+ File.separator;
Map<String, Object> bl = ImgUrlUtil.downLoadFromUrl(url,referer, ShortUID, savePath);
if ((Boolean) bl.get("res") == true) {
// File file = new File();
msg.setCode("200");
msg.setData(bl.get("imgPath")); // savePath + File.separator + ShortUID
return msg;
} else {
if (bl.get("StatusCode").equals("110403")) {
msg.setInfo("该链接非图像文件,无法上传");
} else {
msg.setInfo("该链接暂时无法上传");
}
msg.setCode("500");
msg.setInfo("获取资源失败");
}
} catch (Exception e) {
} else {
msg.setCode("500");
msg.setInfo("获取资源失败");
}
} else {
} catch (Exception e) {
msg.setCode("500");
msg.setInfo("该链接无效");
msg.setInfo("获取资源失败");
}
return msg;
@@ -0,0 +1,89 @@
package cn.hellohao.utils;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.lang3.StringUtils;
public class GetProtocol {
private final static String HTTP= "http://";
private final static String HTTPS = "https://";
private String newurl;
//判断协议 能连接上则协议正确
public String getProtocol(String url,String referer) {
newurl = clearUrl(url);
url = HTTP + newurl;
if (exists(url,referer)) {
return url;
} else {
url = HTTPS + newurl;
if (exists(url,referer)) {
return url;
} else {
return null;
}
}
}
//清除URL里多余的符号
private String clearUrl(String url) {
if (url.contains(HTTP)) {
url = url.substring(url.lastIndexOf(HTTP) + HTTP.length());
for (int i = 0; i < url.length(); i++) {
if (url.charAt(i) == '/' || url.charAt(i) == '.'
|| url.charAt(i) == '\\') {
} else {
url = url.substring(i);
return url;
}
}
} else if (url.contains(HTTPS)) {
url = url.substring(url.lastIndexOf(HTTPS) + HTTPS.length());
for (int i = 0; i < url.length(); i++) {
if (url.charAt(i) == '/' || url.charAt(i) == '.'
|| url.charAt(i) == '\\') {
} else {
url = url.substring(i);
return url;
}
}
}else{
for (int i = 0; i < url.length(); i++) {
if (url.charAt(i) == '/' || url.charAt(i) == '.'
|| url.charAt(i) == '\\') {
} else {
url = url.substring(i);
return url;
}
}
}
return url;
}
//是否能连接上
private boolean exists(String url,String referer) {
try {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setConnectTimeout(3000);
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36");
con.setRequestProperty("referer", StringUtils.isBlank(referer)?"no-referrer":referer);
System.out.println(con.getResponseCode() );
return (con.getResponseCode() == 200);
} catch (Exception e) {
return false;
}
}
public static void main(String[] args){
try {
String url = "https://img-blog.csdnimg.cn/cbf3201a3cfe4fc38a7cef006487158c.png";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
String referer = con.getRequestProperty("Referer");
System.out.println("Referer: " + referer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -2,6 +2,7 @@ package cn.hellohao.utils;
import cn.hellohao.TbedApplication;
import cn.hellohao.pojo.Msg;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.net.HttpURLConnection;
@@ -83,14 +84,7 @@ public class ImgUrlUtil {
}
/**
* 从网络Url中下载文件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static Map<String ,Object> downLoadFromUrl(String urlStr,String fileName,String savePath){
public static Map<String ,Object> downLoadFromUrl(String urlStr,String referer,String fileName,String savePath){
Map<String ,Object> resmap = new HashMap<>();
Map<String ,String> map = checkURLStatusCode(urlStr);
@@ -102,11 +96,11 @@ public class ImgUrlUtil {
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("referer", StringUtils.isBlank(referer) ? "no-referrer" : referer);
//设置超时间为3秒
conn.setConnectTimeout(5*1000);
//防止屏蔽程序抓取而返回403错误 Referer
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
@@ -125,7 +119,6 @@ public class ImgUrlUtil {
inputStream.close();
}
//下载并且保存成功后 判断格式 如果不是图像格式 就删除
//
if(new File(saveDir+File.separator+fileName).exists()){
Msg msg = TypeDict.FileMiME(file);
if(msg.getCode().equals("200")){
@@ -144,8 +137,6 @@ public class ImgUrlUtil {
resmap.put("res",false);
resmap.put("StatusCode","500");
}
return resmap;
}catch (Exception e){
e.printStackTrace();
@@ -155,7 +146,6 @@ public class ImgUrlUtil {
}
}
/**
* 从输入流中获取字节数组
* @param inputStream