Upload
Pippo has builtin support for upload. For a perfect running example see UploadDemo
from pippo-demo module.
In what follows I will show you how simple it is to work with uploads.
public static void main(String[] args) {
Pippo pippo = new Pippo();
Application application = pippo.getApplication();
// the following two lines are optional
application.setUploadLocation("upload");
application.setMaximumUploadSize(100 * 1024); // 100k
application.GET("/", routeContext -> routeContext.render("upload"));
application.POST("/upload", routeContext -> {
// retrieves the value for 'file' input
FileItem file = routeContext.getRequest().getFile("file");
try {
// write to disk
// file.write(file.getSubmittedFileName()); // write the file in application upload location
File uploadedFile = new File(file.getSubmittedFileName());
file.write(uploadedFile);
// send response
routeContext.send("Uploaded file to '" + uploadedFile + "'");
} catch (IOException e) {
throw new PippoRuntimeException(e); // to display the error stack as response
}
});
pippo.start();
}
The content for upload template is:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Submit">
</form>
</body>
</html>