boolean isValidUrl(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection()
connection.setRequestMethod('HEAD')
if (connection.getResponseCode() == 200) {
return true
}
}
catch(final MalformedURLException e) { }
catch(final IOException e) { }
return false
}
Showing posts with label file. Show all posts
Showing posts with label file. Show all posts
Friday, January 29, 2016
HEAD HTTP Request
Tuesday, January 5, 2016
GZip HTTP Request/Response
import java.util.zip.GZIPInputStream
String pageUrl = "http://www.google.com"
BufferedInputStream buff = pageUrl.toURL().newInputStream(requestProperties: ['Accept-Encoding': 'gzip,deflate'])
BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(buff)))
String resp = ""; reader.eachLine{ line -> resp += line }
Friday, October 24, 2014
Easy peasy file/URL binary downloading with Ant get
def ant = new AntBuilder()
ant.get(src: 'http://groovy.codehaus.org/a.jpg', dest: /C:\scrap\a.jpg/, skipexisting: 'true')
//or
File destFile = new File(dest)
if (destFile.exists()) return
destFile.withOutputStream { it << src.toURL().openStream() }
Wednesday, January 15, 2014
csv-2-List for groovycsv
@Grab('com.xlson.groovycsv:groovycsv:1.0')
import com.xlson.groovycsv.CsvParser
// a normally parsed csv can only be searched through ONCE
// this is used to create a "re-searchable" object (List of Maps)
List csv2List( String filePath, boolean shouldTrim = true ) {
new File( filePath ).withReader { r ->
new CsvParser().parse( r ).with { csv ->
return csv.collect { line ->
line.columns.collectEntries { c, v ->
[ c, (shouldTrim ? line[ c ].trim() : line[ c ]) ]
}
}
}
}
}
Wednesday, January 25, 2012
File/Directory Traverse
//helpful file properties: http://groovy.codehaus.org/JN2015-Files
import groovy.io.FileType
def walkFiles = {filepath, filterOnly, onFind, onEnd = {} ->
try {
File f = new File(filepath)
f.traverse([type:FileType.FILES, nameFilter:filterOnly], onFind)
onEnd()
}
catch (FileNotFoundException e) { println "ERROR: invalid file/directory"}
}
def pf = { file ->
if (file.name.contains("a"))
println file.name
}
walkFiles("C:\\123", ~/.*\.ico/, pf)
Thursday, November 24, 2011
Read Lines in Zip File's (File) Entries
//to read a file:
import java.io.*;
import java.util.*;
import java.util.zip.*;
String fname = "C:\\test.zip"
ZipFile zf = new ZipFile(fname)
zf.entries().each { entry ->
if (!entry.directory) {
String entryFileName = entry.name
println entryFileName
InputStream inp = zf.getInputStream(entryFileName);
inp.eachLine { line -> println line }
}
}
//to write a file:
new AntBuilder().zip(destfile:"zipped.zip", basedir=".", excludes="lib .svn plugins *.class")
//http://groovy-almanac.org/creating-a-zipfile-with-antbuilder/
Subscribe to:
Posts (Atom)