Last active
December 24, 2017 17:15
-
-
Save durgaswaroop/7a1511bc35c5d339fef866edf6439414 to your computer and use it in GitHub Desktop.
Creating RESTful API's with Spark microframework using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spark.Request; | |
import javax.servlet.MultipartConfigElement; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.Part; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardCopyOption; | |
import java.util.stream.Collectors; | |
import static spark.Spark.*; | |
public class Server { | |
private static String STORAGE = "storage"; | |
public static void main(String[] args) { | |
File storageDir = new File(STORAGE); | |
if (!storageDir.isDirectory()) storageDir.mkdir(); | |
post("/upload", (req, res) -> uploadFile(req)); | |
get("/download/:file", (req, res) -> downloadFile(req.params(":file"))); | |
get("/count", (req, res) -> countFiles()); | |
delete("/delete/:file", (req, res) -> deleteFile(req.params(":file"))); | |
} | |
private static String uploadFile(Request request) { | |
// TO allow for multipart file uploads | |
request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("")); | |
try { | |
// "file" is the key of the form data with the file itself being the value | |
Part filePart = request.raw().getPart("file"); | |
// The name of the file user uploaded | |
String uploadedFileName = filePart.getSubmittedFileName(); | |
InputStream stream = filePart.getInputStream(); | |
// Write stream to file under storage folder | |
Files.copy(stream, Paths.get(STORAGE).resolve(uploadedFileName), | |
StandardCopyOption.REPLACE_EXISTING); | |
} catch (IOException | ServletException e) { | |
return "Exception occurred while uploading file" + e.getMessage(); | |
} | |
return "File successfully uploaded"; | |
} | |
private static String downloadFile(String fileName) { | |
Path filePath = Paths.get(STORAGE).resolve(fileName); | |
File file = filePath.toFile(); | |
if (file.exists()) { | |
try { | |
// Read from file and join all the lines into a string | |
return Files.readAllLines(filePath).stream().collect(Collectors.joining()); | |
} catch (IOException e) { | |
return "Exception occurred while reading file" + e.getMessage(); | |
} | |
} | |
return "File doesn't exist. Cannot download"; | |
} | |
private static int countFiles() { | |
// Count the number of files in the storage folder | |
return new File(STORAGE).listFiles().length; | |
} | |
private static String deleteFile(String fileName) { | |
File file = Paths.get(STORAGE).resolve(fileName).toFile(); | |
if (file.exists()) { | |
file.delete(); | |
return "File deleted"; | |
} else { | |
return "File " + fileName + " doesn't exist"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment