Upload Hình Vào Thư Mục Có Đường Dẫn Tương Đối

 public void handleFileUpload(FileUploadEvent event) {
        try {
//đường dẫn đi vào thư mục img
            String dirPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/img");
            String d = dirPath.substring(0, 4);
            File targetFolder = new File(dirPath);
            OutputStream out;
//inputstream để đọc đường dẫn
            try (InputStream inputStream = event.getFile().getInputstream()) {
                out = new FileOutputStream(new File(targetFolder,
                        event.getFile().getFileName()));
                setA("img/" + event.getFile().getFileName());
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = inputStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
            }
            out.flush();
            out.close();
        } catch (IOException e) {
        }
    }

Comments

Bài đăn phổ biến

Tính Bình Phương 1 Số Trong Python

Deploy website lên VPS hoặc Hosting với Gitlab CI/CD

Đổi tên "READ MORE" trong bài post

MỞ CAMERA VÀ CHỤP ẢNH BẰNG PHP