0

Подскажите пожалуйста как можно удалить директорию уже после удаления файла который в ней лежал. Сам файл удаляю в конце кода, но нужно и дропнуть саму папку в которой он был.

Вот пример моего кода:

 response.setContentType("text/html");
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/application/octet-stream");

    String filePath = "D:/file/ПЛ5.111.008 СБ/ПЛ5.111.008 СБ [1].tif";
    String test = request.getParameter("test");

    simpleDLL sdll = simpleDLL.INSTANCE;
    File file = new File(filePath);
    String getFileName = file.getName();
    getFileName = getFileName.replace(" ", "");
    String URLEncodedFileName = URLEncoder.encode(getFileName, "UTF-8");

    response.setHeader("Content-Disposition",
            "attachment;filename*=UTF-8''" + URLEncodedFileName);

    FileInputStream fileIn = new FileInputStream(file);
    ServletOutputStream out = response.getOutputStream();

    byte[] outputByte = new byte[4096];//copy binary contect to output stream
    while (fileIn.read(outputByte, 0, 4096) != -1) {
        out.write(outputByte, 0, 4096);
    }

    fileIn.close();
    out.flush();
    out.close();

    request.setAttribute("test", test);
    RequestDispatcher view = request.getRequestDispatcher("/index.jsp");
    file.delete();
4
  • попробовать подняться вверх по директории и удалить
    – Dred
    5 авг 2019 в 7:54
  • @Dred Я пробовал просто создать еще один Файл, туда вбил путь к директории но она не удаляется. Делал это после удаления файла, директория была пуста.
    – Sorpok
    5 авг 2019 в 8:03
  • 1
    Вот, посмотрите
    – Dred
    5 авг 2019 в 8:17
  • Спасибо за пример, в итоге получилось по другому, сделал ответом.
    – Sorpok
    5 авг 2019 в 8:37

1 ответ 1

0

В итоге делаю вот так:

 File dir = new File("D:/file/ПЛ5.111.008 СБ/ПЛ5.111.008 СБ [1].tif");
    String dirFileName = dir.getName();
    int sizeFilename = dirFileName.length() + 1;
    String path = "D:/file/ПЛ5.111.008 СБ/ПЛ5.111.008 СБ [1].tif";
    path = path.substring(0, path.length() - sizeFilename );
    File finalDir = new File(path);
    String[] entries = finalDir.list();
    for (String s : entries) {
        File currentFile = new File(finalDir.getPath(), s);
        currentFile.delete();
    }
    finalDir.delete();
2
  • у вас получается хардкод. У вас всегда одно и тоже имя получается будет?
    – Dred
    5 авг 2019 в 9:03
  • Нет, имя я буду получать из строки переданной через dll. Это пока тестовый проект. Проверить как оно работает. Код я уже поправил, путь один раз задаю, а дальше уже с ним играюсь.
    – Sorpok
    5 авг 2019 в 9:52

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.