Java Groovy Grails
2019. 12. 5. 11:53
def read_image_from_url(){ // this is for img src
//def test_url='http://xxxx/static/distribution_2019-09-08-19-56-36.964038.png'
URL url = new URL(params.url);
try {
InputStream is = url.openStream();
render(file: is, contentType: 'image/png')
} catch (IOException e){
}
}
def read_svg_image_from_url(){ // this is for img src
//def test_url='http://xxxx/static/distribution_2019-09-08-19-56-36.964038.png'
URL url = new URL(params.url);
try {
InputStream is = url.openStream();
render(file: is, contentType: 'image/svg+xml')
} catch (IOException e){
}
}
def read_json_from_url(){
///def JSON_URL='http://xxxx/static/cathode_2019-07-13-10-31-37.063439.json'
URL url = new URL(params.url);
InputStream urlStream = null;
try {
urlStream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream));
JsonSlurper jsonSlurper = new JsonSlurper();
Object result = jsonSlurper.parse(reader);
Map jsonResult = (Map) result;
//println jsonResult
List results = (List) jsonResult.get("Results");
//Map user = (Map) jsonResult.get("user");
//String name = (String) user.get("name");
//Integer age = (Integer) user.get("age");
//List interests = (List) user.get("interests");
render jsonResult as JSON
} finally {
if (urlStream != null) {
urlStream.close();
}
}
}
def png_download(){ // this is for downloading image from outside url
def imageurl = params.url
def fnames = imageurl.split('/')
InputStream input = new URL(imageurl).openStream();
render file: input, contentType: 'image/png', fileName: fnames.last()
//render 'ok'
}
related link: http://grails.asia/grails-render-images-on-the-fly-in-gsp
https://stackoverflow.com/questions/4502278/grails-displaying-created-image-in-gsp