温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何验证数据库中URL的有效性

发布时间:2021-11-25 14:30:36 来源:亿速云 阅读:121 作者:柒染 栏目:编程语言

这篇文章将为大家详细讲解有关如何验证数据库中URL的有效性,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

曲库中一些歌曲的URL虽然存在,但是根据URL已经下载不到音乐了.
Nginx显示404错误.

验证数据库中歌曲的URL是否能够下载

首先,先把数据库中的歌曲URL导出到文件.如下格式(歌曲ID,音乐地址类型,路径)
1000;AccompanimentURL ;/00/00/00001000_accompaniment.m4a

然后使用程序扫描URL

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>

        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.0.Final</version>
        </dependency>

程序比较渣..以后得学习一下对象封装


<ol start="1" class="dp-j" font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:2;text-align:left;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-stroke-width:0px;text-decoration-style:initial;text-decoration-color:initial;">

  • import java.io.BufferedReader;  

  • import java.io.FileInputStream;  

  • import java.io.FileWriter;  

  • import java.io.IOException;  

  • import java.io.InputStreamReader;  

  • import java.util.HashMap;  

  • import java.util.HashSet;  

  • import java.util.Map;  

  • import java.util.Set;  

  • import java.util.concurrent.BlockingQueue;  

  • import java.util.concurrent.LinkedBlockingQueue;  

  • import java.util.concurrent.Semaphore;  

  • import java.util.concurrent.atomic.AtomicInteger;  

  •   

  • import io.netty.bootstrap.Bootstrap;  

  • import io.netty.buffer.ByteBuf;  

  • import io.netty.buffer.Unpooled;  

  • import io.netty.channel.Channel;  

  • import io.netty.channel.ChannelHandlerContext;  

  • import io.netty.channel.ChannelInboundHandlerAdapter;  

  • import io.netty.channel.ChannelInitializer;  

  • import io.netty.channel.EventLoopGroup;  

  • import io.netty.channel.nio.NioEventLoopGroup;  

  • import io.netty.channel.socket.nio.NioSocketChannel;  

  • import io.netty.handler.codec.LineBasedFrameDecoder;  

  • import io.netty.handler.codec.string.StringDecoder;  

  •   

  • public class URLChecker {  

  •     public static void main(String[] args) {  

  •         String sourceFile="F:\\normal.txt";  

  •         String resultFile="F:\\result.csv";  

  •         new URLInput(sourceFile, resultFile);  

  •           

  •     }  

  •   

  • }  

  •   

  • class URLConnection extends Thread{  

  •     @Override  

  •     public void run() {  

  •         while(true){  

  •             int start=count.get();  

  •             try {  

  •                 Thread.sleep(1000);  

  •             } catch (InterruptedException e) {  

  •                 // TODO Auto-generated catch block  

  •                 e.printStackTrace();  

  •             }  

  •             int end=count.get();  

  •             System.out.println("每秒检查:"+(end-start));  

  •         }  

  •     }  

  •   

  •     Semaphore sem = new Semaphore(20);  

  •     String[] hosts = new String[5];  

  •     AtomicInteger count = new AtomicInteger();  

  •   

  •     EventLoopGroup group = new NioEventLoopGroup(3);  

  •     URLInput input;  

  •     URLOutput output;  

  •     URLConnection(URLInput input,URLOutput writer) {  

  •         hosts[0] = "172.16.1.151";  

  •         hosts[1] = "172.16.1.152";  

  •         hosts[2] = "172.16.1.153";  

  •         hosts[3] = "172.16.1.154";  

  •         hosts[4] = "172.16.1.155";  

  •           

  •         this.output=writer;  

  •         this.input=input;  

  •   

  •     }  

  •   

  •     public void connection(final Map<String, String> map)  

  •             throws InterruptedException {  

  •         sem.acquire();  

  •         int index = count.getAndIncrement();  

  •         Bootstrap boot = new Bootstrap();  

  •   

  •         boot.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {  

  •             @Override  

  •             protected void initChannel(Channel ch) throws Exception {  

  •                 ch.pipeline().addLast(new LineBasedFrameDecoder(409600));  

  •                 ch.pipeline().addLast(new StringDecoder());  

  •                 ch.pipeline().addLast(new HttpClientHandler(map, sem,input,output));  

  •             }  

  •         });  

  •         boot.connect(hosts[index % hosts.length], 80);  

  •   

  •     }  

  •       

  •       

  • }  

  •   

  • class HttpClientHandler extends ChannelInboundHandlerAdapter {  

  •     StringBuffer sb = new StringBuffer(512);  

  •     Map<String, String> map = new HashMap<String, String>();  

  •     Semaphore sem;  

  •     URLInput input;  

  •     URLOutput writer;  

  •   

  •   

  •     public HttpClientHandler(Map<String, String> map, Semaphore sem, URLInput input, URLOutput writer) {  

  •         this.map = map;  

  •         this.sem = sem;  

  •   

  •         this.writer=writer;  

  •         this.input=input;  

  •     }  

  •   

  •     @Override  

  •     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {  

  •         sem.release();  

  •           

  •         ctx.close();  

  •         writer.addSuccURL(map);  

  •     }  

  •   

  •     @Override  

  •     public void channelActive(ChannelHandlerContext ctx) throws Exception {  

  •   

  •         StringBuilder sb = new StringBuilder();  

  •         sb.append("HEAD " + map.get("url") + " HTTP/1.0\r\n");  

  •         sb.append("HOST:" + 80 + "\r\n");  

  •         sb.append("Accept:*/*\r\n");  

  •         sb.append("\r\n");  

  •         ByteBuf bb = Unpooled.copiedBuffer(sb.toString().getBytes("utf8"));  

  •         ctx.writeAndFlush(bb);  

  •   

  •     }  

  •   

  •     @Override  

  •     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  

  •         String content = (String) msg;  

  •         if (content.contains(":")) {  

  •             String[] s = content.split(":");  

  •             map.put(s[0].trim(), s[1].trim());  

  •         } else if (content.startsWith("HTTP/1.1")) {  

  •             map.put("httpcode", content.replaceAll("HTTP/1.1 ", ""));  

  •         }  

  •   

  •     }  

  •   

  •     @Override  

  •     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  

  •         cause.printStackTrace();  

  •         ctx.close();  

  •         input.addFailUrl(map);  

  •         sem.release();  

  •     }  

  •   

  • }  

  •   

  • class URLInput {  

  •     URLConnection urlcon;  

  •     URLInput(String sourceFile, String resultFile) {  

  •         URLOutput output=new URLOutput(resultFile);  

  •         urlcon=new URLConnection(this, output);  

  •         output.start();  

  •         urlcon.start();  

  •         try {  

  •             init(resultFile);  

  •             read(sourceFile);  

  •         } catch (IOException e) {  

  •             e.printStackTrace();  

  •         } catch (InterruptedException e) {  

  •             e.printStackTrace();  

  •         }  

  •     }  

  •   

  •     /**  

  •      * 初始化已经处理的文件,用于中断处理后的恢复运行  

  •      *   

  •      * @param resultFile  

  •      * @throws IOException  

  •      */  

  •     private void init(String resultFile) throws IOException {  

  •         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(resultFile)));  

  •         String row = null;  

  •         while ((row = br.readLine()) != null) {  

  •             String[] data = row.split(",");  

  •             set.add(data[0]);  

  •   

  •         }  

  •         br.close();  

  •     }  

  •   

  •     public void addFailUrl(Map<String, String> map) {  

  •         failq.add(map);  

  •     }  

  •   

  •     final BlockingQueue<Map<String, String>> failq = new LinkedBlockingQueue<Map<String, String>>();  

  •     Set<String> set = new HashSet<String>();  

  •   

  •     private void read(String sourceFile) throws IOException, InterruptedException {  

  •         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile)));  

  •         String row = null;  

  •         while ((row = br.readLine()) != null) {  

  •             while (failq.size() != 0) {  

  •   

  •                 final Map<String, String> m = failq.take();  

  •                 urlcon.connection(m);  

  •             }  

  •   

  •             String[] data = row.split(";");  

  •             final Map<String, String> map = new HashMap<String, String>();  

  •             map.put("songid", data[0]);  

  •             map.put("type", data[1]);  

  •             map.put("url", data[2]);  

  •             if (!set.contains(data[0])) {  

  •                 urlcon.connection(map);  

  •             }  

  •         }  

  •         br.close();  

  •         System.out.println("Finish!!");  

  •     }  

  • }  

  •   

  • class URLOutput extends Thread {  

  •     BlockingQueue<Map<String, String>> succq = new LinkedBlockingQueue<Map<String, String>>();  

  •     String resultFile;  

  •   

  •     public void addSuccURL(Map<String, String> map) {  

  •         succq.add(map);  

  •     }  

  •   

  •     public URLOutput(String resultFile) {  

  •         this.resultFile = resultFile;  

  •     }  

  •   

  •     @Override  

  •     public void run() {  

  •         Map<String, String> map = null;  

  •         FileWriter fw = null;  

  •         try {  

  •             fw = new FileWriter(resultFile, true);  

  •             while ((map = succq.take()) != null) {  

  •                 fw.write(map.get("songid") + "," + map.get("type") + "," + map.get("url") + "," + map.get("httpcode")  

  •                         + "," + map.get("Content-Length") + "\n");  

  •             }  

  •         } catch (InterruptedException e) {  

  •             e.printStackTrace();  

  •         } catch (IOException e) {  

  •             e.printStackTrace();  

  •         } finally {  

  •             try {  

  •                 fw.close();  

  •             } catch (IOException e) {  

  •                 e.printStackTrace();  

  •             }  

  •         }  

  •     }  

  •   



关于如何验证数据库中URL的有效性就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI