Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.8 kB
2
Indexable
Never
package Servlet;

import java.io.IOException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.annotation.MultipartConfig;
import java.io.InputStream;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import DAOs.ProductDAO;
import Models.Product;

@WebServlet("/upload")
@MultipartConfig
public class ImageServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public ImageServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("addP.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String txtProID = request.getParameter("txtProID");
        String txtProName = request.getParameter("txtProName");
        String txtProQuantity = request.getParameter("txtProQuantity");
        String txtProPrice = request.getParameter("txtProPrice");
        String txtProDes = request.getParameter("txtProDes");

        int proQuantity = 0;
        long proPrice = 0;

        if (txtProQuantity != null && !txtProQuantity.isEmpty()) {
            proQuantity = Integer.parseInt(txtProQuantity);
        }

        if (txtProPrice != null && !txtProPrice.isEmpty()) {
            proPrice = Long.parseLong(txtProPrice);
        }

        String ImageFolderPath = "upload";
        String HardPath = getServletContext().getRealPath("/");
        String uploadFilePath = HardPath + File.separator + ImageFolderPath;

        File fileSaveDirectory = new File(uploadFilePath);
        if (!fileSaveDirectory.exists()) {
            fileSaveDirectory.mkdirs();
        }

        Part filePart = request.getPart("fileProImg");

        String extension = filePart.getContentType();
        extension = extension.substring(extension.lastIndexOf("/") + 1);
        String imageName = txtProID + "." + extension;

        try (InputStream fileContent = filePart.getInputStream()) {
            File file = new File(uploadFilePath, imageName);

            try (OutputStream outputStream = new FileOutputStream(file)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = fileContent.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        String relativeImagePath = ImageFolderPath + "/" + imageName;
        request.setAttribute("imagePath", relativeImagePath);

        // Tạo đối tượng Product từ các thông tin đã nhận
        Product product = new Product();
        product.setPro_id(txtProID);
        product.setPro_name(txtProName);
        product.setPro_quan(proQuantity);
        product.setPro_price(proPrice);
        product.setPro_pic(relativeImagePath);
        product.setPro_des(txtProDes);

        // Thêm sản phẩm vào cơ sở dữ liệu
        ProductDAO productDAO = new ProductDAO();
        productDAO.addProduct(product);

        request.setAttribute("message", "Product added successfully");
        request.getRequestDispatcher("addP.jsp").forward(request, response);
    }
}