java通过获取img标签⾥⾯的是src路径下载图⽚上传到指定服
务器上并替换src路径
领导要求将⽂章中添加的图⽚通过路径现在到⾃⼰的服务器下⾯并把路径替换成⾃⼰的服务器图⽚链接,第⼀篇⽂章,不知道说啥,直接
上代码吧!  *******皆为不可展⽰内容  请体谅
public class updateImg {
private static final String endpoint = "*********";
// 阿⾥云主账号AccessKey拥有所有API的访问权限,风险很⾼。强烈建议您创建并使⽤RAM账号进⾏API访问或⽇常运维,请登录 sole.aliyun    private static final String accessKeyId = "********";
private static final String accessKeySecret = "*********";
/*
*获取图⽚连接并判断是否为⽹址
*/
public String getImgStr(String count) throws Exception{
Pattern p = Patternpile("<(img|IMG)(.*?)(>|></img>|/>)");//匹配字符串中的img标签
Matcher matcher = p.matcher(count);
boolean hasPic = matcher.find();
if(hasPic == true){//判断是否含有图⽚
while(hasPic){ //如果含有图⽚,那么持续进⾏查,直到匹配不到
String group = up();
if (ains("data-src")== true) {
String src = group.substring(group.lastIndexOf("src"));
Pattern srcText = Patternpile("(src|SRC)=(\"|\')(.*?)(\"|\')");//匹配图⽚的地址
Pattern srcText2 = Patternpile("(data-src|DATA-SRC)=(\"|\')(.*?)(\"|\')");//匹配图⽚的地址
Matcher matcher2 = srcText2.matcher(group);
Matcher matcher3 = srcText.matcher(src);
//判断是否为域名,不是域名则下载到服务器
Pattern pattern = Patternpile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
if (matcher2.find() == true && pattern.up()).matches() == false && matcher3.find() == true) {
URL url = new up().substring(10, up().length() - 1));
URL url1 = new up().substring(5, up().length() - 1));
String last = getUrl(String.valueOf(url));
URLConnection con = url.openConnection();
con.setConnectTimeout(120 * 1000);
linux查看根目录命令InputStream is = InputStream();
byte[] bs = new byte[1024];
int len;
String xxUrl = "temp";
File sf = new File(xxUrl);
if (!sf.exists()) {
sf.mkdirs();
}
String uuid32 = getUUID32();
OutputStream os = new Path() + "/" + uuid32 + last);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
//将图⽚上传到服务器
js date对象判断URL xUrl = new URL("*************" + "/2/");
File excelFile = ateTempFile(uuid32, last);
File file = new Path() + "/" + uuid32 + last);
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
PutObjectResult putObjectResult = ossClient.putObject("starwant-dev", "2/" + uuid32 + last, file);
os.close();
is.close();
ossClient.shutdown();
this.deleteFile(excelFile);
this.deleteFile(file);
//替换图⽚url
//替换图⽚url
String replaceHttp = xUrl + uuid32 + last;
String countx = place(String.valueOf(url1), replaceHttp);
count = countx;
}
}else {
Pattern srcText = Patternpile("(src|SRC)=(\"|\')(.*?)(\"|\')");//匹配图⽚的地址
Matcher matcher2 = srcText.matcher(group);
Pattern pattern = Patternpile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");                    if (matcher2.find() == true && pattern.up()).matches() == false) {
URL url = new up().substring(5, up().length() - 1));
String last = getUrl(String.valueOf(url));
URLConnection con = url.openConnection();
con.setConnectTimeout(120 * 1000);
InputStream is = InputStream();
byte[] bs = new byte[1024];
python常用函数总结
int len;
String xxUrl = "temp";
java下载过程File sf = new File(xxUrl);
if (!sf.exists()) {
sf.mkdirs();
}
String uuid32 = getUUID32();
OutputStream os = new Path() + "/" + uuid32 + last);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
//将图⽚上传到服务器
URL xUrl = new URL("***************" + "/2/");
File excelFile = ateTempFile(uuid32, last);
File file = new Path() + "/" + uuid32 + last);
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
PutObjectResult putObjectResult = ossClient.putObject("starwant-dev", "2/" + uuid32 + last, file);
os.close();
is.close();
ossClient.shutdown();
this.deleteFile(excelFile);
this.deleteFile(file);
//替换图⽚url
String replaceHttp = xUrl + uuid32 + last;
String countx = place(String.valueOf(url), replaceHttp);
count = countx;
}
}
hasPic = matcher.find();//判断是否还有img标签
}
}
return count;
}
/*
css怎么把图片填充满一个框*⽣成随机32位数字
*/
public static String getUUID32() {
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
return uuid;
}
/*
*删除⽂件
*/
private void files) {
File[] var2 = files;
int var3 = files.length;
for(int var4 = 0; var4 < var3; ++var4) {
File file = var2[var4];
if (ists()) {
file.delete();
}
}
}
/*
*判断链接是否带有?后缀
*/
public static String getUrl(String url) {
if (!String.valueOf(url).contains("?")) {
String last = url.substring(url.lastIndexOf("."));
return last;
}else{
String lastIndex = url.substring(0,url.indexOf("?"));
String last = lastIndex.substring(lastIndex.lastIndexOf("."));            return last;
}
}html对齐方式
}
OSS的配置⽂件
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.2</version>
</dependency>
原始参数:
替换后的数据:
⾄此,整个过程全部完成,最后在将替换后的链接打开查看是否添加到服务器上,经验不⾜的⼩⽩菜,代码不够美观,请各位⼤佬见谅,此⽂章仅仅是个⼈记录,若能帮到各位也是荣幸之⾄。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。