MULTIPART UPLOAD WORKS
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package io.github.lumijiez.example;
|
||||
|
||||
import io.github.lumijiez.core.config.ServerConfig;
|
||||
import io.github.lumijiez.core.http.HttpFileItem;
|
||||
import io.github.lumijiez.core.http.HttpMultipartData;
|
||||
import io.github.lumijiez.core.http.HttpServer;
|
||||
import io.github.lumijiez.core.http.HttpStatus;
|
||||
import io.github.lumijiez.core.ws.WebSocketConnection;
|
||||
@@ -10,6 +12,9 @@ import io.github.lumijiez.example.daos.ProductDao;
|
||||
import io.github.lumijiez.example.models.Product;
|
||||
import io.github.lumijiez.core.logging.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
ProductDao productDao = new ProductDao();
|
||||
@@ -32,6 +37,42 @@ public class Main {
|
||||
res.sendResponse(HttpStatus.OK, "All good, lil bro");
|
||||
});
|
||||
|
||||
server.POST("/upload", (req, res) -> {
|
||||
HttpMultipartData multipartData = req.getMultipartData();
|
||||
|
||||
String description = multipartData.getField("description");
|
||||
String category = multipartData.getField("category");
|
||||
|
||||
HttpFileItem uploadedFile = multipartData.getFile("file");
|
||||
if (uploadedFile != null) {
|
||||
String fileName = uploadedFile.getFileName();
|
||||
String contentType = uploadedFile.getContentType();
|
||||
byte[] fileContent = uploadedFile.getContent();
|
||||
|
||||
File uploadDir = new File("uploads");
|
||||
if (!uploadDir.exists()) {
|
||||
uploadDir.mkdirs();
|
||||
}
|
||||
|
||||
Logger.info("START UPLOAD", fileName);
|
||||
File destination = new File(uploadDir, fileName);
|
||||
uploadedFile.saveTo(destination);
|
||||
Logger.info("DONE UPLOAD", fileName);
|
||||
res.sendResponse(HttpStatus.OK, "Uploaded: " + fileName);
|
||||
// res.sendJson(HttpStatus.OK, Map.of(
|
||||
// "message", "File uploaded successfully",
|
||||
// "fileName", fileName,
|
||||
// "size", fileContent.length,
|
||||
// "description", description
|
||||
// ));
|
||||
Logger.info("START UPLOAD", fileName);
|
||||
} else {
|
||||
res.sendJson(HttpStatus.BAD_REQUEST, Map.of(
|
||||
"error", "No file provided"
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
server.GET("/user", (req, res) -> {
|
||||
Product product = productDao.getProductById(5);
|
||||
res.sendJson(HttpStatus.OK, product);
|
||||
|
||||
Reference in New Issue
Block a user