驗證資料庫中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>
程式比較渣..以後得學習一下物件封裝
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>
程式比較渣..以後得學習一下物件封裝
- 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();
- }
- }
- }
-
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2142047/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- jsonlint:python的json資料驗證庫JSONPython
- URL格式驗證
- SQL資料庫開發中的SSIS 延遲驗證方法SQL資料庫
- Pydantic:強大的Python 資料驗證庫Python
- JavaScript 如何驗證 URLJavaScript
- MySQL中資料型別的驗證MySql資料型別
- 如何提高少樣本學習中的引數有效性以及資料有效性
- 中國身份證號驗證庫
- Oracle23ai 資料庫的簡單驗證OracleAI資料庫
- Oracle資料庫密碼延遲驗證Oracle資料庫密碼
- 騰訊資料庫tdsql部署與驗證資料庫SQL
- 【YashanDB知識庫】崖山資料庫Outline功能驗證資料庫
- 1.6.3. 資料庫管理員身份驗證方法資料庫
- javascript 資料型態/結構驗證庫 : SkeletonsJavaScript
- 利用Grafana監控influxdb表中資料有效性GrafanaUX
- 如何實現 Java SpringBoot 自動驗證入引數據的有效性JavaSpring Boot
- 資料庫欄位設定非空, phalcon建立資料驗證不透過資料庫
- 正規表示式驗證url的合法性
- 2.4.4 Step 3: 選擇資料庫管理員的身份驗證方法資料庫
- 阿里雲PolarDB-X資料庫透過分散式資料庫金融標準驗證阿里資料庫分散式
- 表單資料驗證
- 驗證資料是否存在
- 1.6.3.1. 關於資料庫管理員身份驗證方法資料庫
- 資料庫實驗五 資料庫的安全性資料庫
- 【詳細、開箱即用】.NET企業微信回撥配置(資料回撥URL和指令回撥URL驗證)
- data型別的Url格式:把小資料直接嵌入到Url中型別
- 有關資料驗證的原則
- 使用表單驗證,建立資料驗證層,Ajax 統一返回驗證錯誤資訊
- PostgreSQL資料庫PGCM高階認證考試經驗分享SQL資料庫GC
- APEX 通過資料庫中使用者資訊驗證登陸資料庫
- beego表達資料驗證Go
- 初涉後端,還請見諒···By Ajax驗證註冊的賬號資料庫中是否存在後端資料庫
- 延遲密碼驗證特性引起的資料庫HANG死及當機密碼資料庫
- MySQL手動資料校驗+雲資料庫資料校驗MySql資料庫
- 解決requests庫中SSL驗證問題
- 資料庫實驗五:資料庫程式設計資料庫程式設計
- 資料庫實驗八 資料庫程式設計資料庫程式設計
- 資料庫實驗二資料庫
- 1.6.5.2. 通過密碼檔案驗證連線資料庫密碼資料庫