Hello Friends, Today I am going to share a url checking tool which I have developed few days ago in Java. This tool filter outs the bad urls and working urls from list of urls. I am going to share code also, so that you can develop it yourself and you can also extend its feature according to your need or you can add on your creativity in this tool.
So here is the code
MainForm.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package urlcheckingtool; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import javax.net.ssl.HttpsURLConnection; import javax.swing.SwingUtilities; /** * * @author rajesh kumar sahanee */ public class MainForm extends javax.swing.JFrame { /** * Creates new form MainForm */ public MainForm() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { mainSP = new javax.swing.JSplitPane(); leftJP = new javax.swing.JPanel(); urlsJL = new javax.swing.JLabel(); jScrollPane4 = new javax.swing.JScrollPane(); urlsJTA = new javax.swing.JTextArea(); processJB = new javax.swing.JButton(); processJPB = new javax.swing.JProgressBar(); jPanel1 = new javax.swing.JPanel(); cb301CB = new javax.swing.JCheckBox(); cb403CB = new javax.swing.JCheckBox(); cb404CB = new javax.swing.JCheckBox(); cb500CB = new javax.swing.JCheckBox(); rightSP = new javax.swing.JSplitPane(); jPanel2 = new javax.swing.JPanel(); notFoundUrlsJL = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); notFoundUrlsJTA = new javax.swing.JTextArea(); jPanel3 = new javax.swing.JPanel(); workingUrlsJL = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); workingUrlsJTA = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Url Checking Tool"); leftJP.setMinimumSize(new java.awt.Dimension(260, 0)); leftJP.setPreferredSize(new java.awt.Dimension(200, 435)); urlsJL.setText("Urls"); urlsJTA.setColumns(20); urlsJTA.setRows(5); jScrollPane4.setViewportView(urlsJTA); processJB.setText("Process"); processJB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { processJBActionPerformed(evt); } }); jPanel1.setAutoscrolls(true); cb301CB.setText("301"); cb403CB.setText("403"); cb404CB.setText("404"); cb500CB.setText("500"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(cb301CB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cb403CB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cb404CB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cb500CB) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cb301CB) .addComponent(cb403CB) .addComponent(cb404CB) .addComponent(cb500CB)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout leftJPLayout = new javax.swing.GroupLayout(leftJP); leftJP.setLayout(leftJPLayout); leftJPLayout.setHorizontalGroup( leftJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(leftJPLayout.createSequentialGroup() .addContainerGap() .addGroup(leftJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane4) .addGroup(leftJPLayout.createSequentialGroup() .addComponent(processJB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(processJPB, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addGroup(leftJPLayout.createSequentialGroup() .addComponent(urlsJL) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); leftJPLayout.setVerticalGroup( leftJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftJPLayout.createSequentialGroup() .addContainerGap() .addComponent(urlsJL) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(leftJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(processJPB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(processJB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(6, 6, 6)) ); mainSP.setLeftComponent(leftJP); rightSP.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); jPanel2.setMinimumSize(new java.awt.Dimension(0, 200)); jPanel2.setPreferredSize(new java.awt.Dimension(200, 200)); notFoundUrlsJL.setText("Not Working Urls"); notFoundUrlsJTA.setColumns(20); notFoundUrlsJTA.setRows(5); jScrollPane1.setViewportView(notFoundUrlsJTA); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(notFoundUrlsJL) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(notFoundUrlsJL) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE) .addContainerGap()) ); rightSP.setLeftComponent(jPanel2); workingUrlsJL.setText("Working Urls"); workingUrlsJTA.setColumns(20); workingUrlsJTA.setRows(5); jScrollPane2.setViewportView(workingUrlsJTA); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(workingUrlsJL) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(workingUrlsJL) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE) .addContainerGap()) ); rightSP.setRightComponent(jPanel3); mainSP.setRightComponent(rightSP); getContentPane().add(mainSP, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold> private void processJBActionPerformed(java.awt.event.ActionEvent evt) { if(processJB.getText().equals("Process")) { stop = false; processJB.setText("Stop"); } else { stop = true; processJB.setText("Process"); return; } String urls[] = urlsJTA.getText().split("\n"); notFoundUrls = ""; workingUrls = ""; processJPB.setValue(-1); processJPB.setIndeterminate(true); final ArrayList<String> errorCodes = new ArrayList(); if(cb301CB.isSelected()) { errorCodes.add("301"); } if(cb403CB.isSelected()) { errorCodes.add("403"); } if(cb404CB.isSelected()) { errorCodes.add("404"); } if(cb500CB.isSelected()) { errorCodes.add("500"); } Thread worker = new Thread() { public void run() { for(int i = 0; i < urls.length; i++) { if(stop) { break; } try { if (errorCodes.contains(getResponseCode(urls[i]))) { notFoundUrls += urls[i] + "\n"; } else { workingUrls += urls[i] + "\n"; } SwingUtilities.invokeLater(new Runnable() { public void run() { notFoundUrlsJTA.setText(notFoundUrls); workingUrlsJTA.setText(workingUrls); } }); //Thread.sleep(500); } catch (IOException e) { } } SwingUtilities.invokeLater(new Runnable() { public void run() { processJPB.setIndeterminate(false); processJPB.setValue(100); processJB.setText("Process"); } }); } }; worker.start(); // So we don't hold up the dispatch thread. } public static String getResponseCode(String urlString) throws MalformedURLException, IOException { if(!urlString.startsWith("http://") && !urlString.startsWith("https://")) { urlString = "http://" + urlString; } URL u = new URL(urlString); if (u.getProtocol().equals("https")) { HttpsURLConnection huc = (HttpsURLConnection) u.openConnection(); huc.setRequestMethod("GET"); huc.setConnectTimeout(10000); huc.connect(); return String.valueOf(huc.getResponseCode()); } else { HttpURLConnection huc = (HttpURLConnection) u.openConnection(); huc.setRequestMethod("GET"); huc.setConnectTimeout(10000); huc.connect(); return String.valueOf(huc.getResponseCode()); } } String notFoundUrls = new String(); String workingUrls = new String(); boolean stop = false; // Variables declaration - do not modify private javax.swing.JCheckBox cb301CB; private javax.swing.JCheckBox cb403CB; private javax.swing.JCheckBox cb404CB; private javax.swing.JCheckBox cb500CB; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JPanel leftJP; private javax.swing.JSplitPane mainSP; private javax.swing.JLabel notFoundUrlsJL; private javax.swing.JTextArea notFoundUrlsJTA; private javax.swing.JButton processJB; private javax.swing.JProgressBar processJPB; private javax.swing.JSplitPane rightSP; private javax.swing.JLabel urlsJL; private javax.swing.JTextArea urlsJTA; private javax.swing.JLabel workingUrlsJL; private javax.swing.JTextArea workingUrlsJTA; // End of variables declaration } |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package urlcheckingtool; /** * * @author rajesh kumar sahanee */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainForm().setVisible(true); } }); } } |
Screenshot
NetBeans Project Download
Url Checking Tool
1 file(s) 37.37 KB
Thank you, Please don’t forget to share if you like it
Comments