From 4072d028a385284558905fcee8bf6b39854a024d Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 21 Jul 2020 00:12:17 +0900 Subject: [PATCH] Implement /math/pi endpoint --- bin/.gitignore | 2 + .../java/com/example/HelloController.java | 14 ----- .../java/com/example/demo/Controller.java | 60 +++++++++++++++++++ 3 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 bin/.gitignore delete mode 100644 src/main/java/com/example/HelloController.java create mode 100644 src/main/java/com/example/demo/Controller.java diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..7eed456 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/main/ +/test/ diff --git a/src/main/java/com/example/HelloController.java b/src/main/java/com/example/HelloController.java deleted file mode 100644 index c80f2f2..0000000 --- a/src/main/java/com/example/HelloController.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.demo; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class HelloController { - - @GetMapping("/") - public String helloWorld() { - return "Hello from Spring!"; - } - -} \ No newline at end of file diff --git a/src/main/java/com/example/demo/Controller.java b/src/main/java/com/example/demo/Controller.java new file mode 100644 index 0000000..0f4a7c7 --- /dev/null +++ b/src/main/java/com/example/demo/Controller.java @@ -0,0 +1,60 @@ +package com.example.demo; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.ConnectException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.util.HashMap; + +import javax.imageio.ImageIO; + +@RestController +public class Controller { + + public static void downloadFileFromUrl(String url, String file) throws IOException{ + File filer = new File(file); + filer.createNewFile(); + + URL website = new URL(url); + HttpURLConnection connection = (HttpURLConnection) website.openConnection(); + /*for (String s : connection.getHeaderFields().keySet()) { + System.out.println(s+": "+connection.getHeaderFields().get(s)); + }*/ + connection.setRequestMethod("GET"); + //connection.setRequestProperty("Content-Type", "application/json"); + try { + ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream()); + FileOutputStream fos = new FileOutputStream(file); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + } catch (ConnectException e) { + System.out.println("Failed to connect, moving on..."); + } + } + + @GetMapping("/math/pi") + public String piDisplay() { + return Double.toString(Math.PI); + } + + @GetMapping("/image") + public HashMap helloWorld(@RequestParam("url") String url){ + try { + downloadFileFromUrl("http://pbs.twimg.com/media/EdKE8xzVcCEf1qd.jpg","temp"); + BufferedImage img = ImageIO.read(new File("temp")); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + +} \ No newline at end of file