@SuppressWarnings({"unchecked"})
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
    Date dataAtual = new Date(System.currentTimeMillis());
    String data = sd.format(dataAtual);
    data = data.replace("/", "");
    fileSavePath = "C:\\entregas";
    if (!(new File(fileSavePath)).exists()) {
      (new File(fileSavePath)).mkdir();
    }

    String resp = "";
    Aluno a = new Aluno();
    boolean erro = false;
    List<FileItem> items;
    resp += "Arquivo carregado:";
    try {
      int cont = 0;
      items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
      Trabalho t = null;
      for (FileItem item : items) {
        if (item.isFormField()) {
          String nomeCampo = item.getFieldName();
          if (nomeCampo.equalsIgnoreCase("ra")) {
            a.setRa(item.getString());
          } else {
            if (nomeCampo.equalsIgnoreCase("trabalhoselecionado")) {
              if (item.getString().trim().equals("")) {
                resp = "Selecione um trabalho";
                erro = true;
                request.setAttribute("message", resp);
                getServletContext()
                    .getRequestDispatcher("/entregatrabalhos.jsp")
                    .forward(request, response);
              }
              t = new Trabalho();
              t.setCodigo(Integer.parseInt(item.getString()));
              TrabalhoDao tDao = new TrabalhoDao();
              t = tDao.consultaTrabalho(t);
            }
          }
          if (t != null && !a.getRa().equals("")) {
            fileSavePath += File.separator + t.getDisciplina().getPasta();
            File newDir = new File(fileSavePath);
            if (!newDir.exists()) {
              newDir.mkdir();
            }
            fileSavePath += File.separator + t.getTitulo();
            newDir = new File(fileSavePath);
            if (!newDir.exists()) {
              newDir.mkdir();
            }
            fileSavePath += File.separator + a.getRa();
            newDir = new File(fileSavePath);
            if (!newDir.exists()) {
              newDir.mkdir();
            }
          }
        } else {
          if (!erro) {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
              cont++;
              String filename = FilenameUtils.getName(item.getName());
              String extensao = filename.substring(filename.lastIndexOf("."), filename.length());
              inputStream = item.getInputStream();
              StringBuffer nomeArquivo = new StringBuffer();
              nomeArquivo.append(a.getRa());
              nomeArquivo.append("_");
              nomeArquivo.append(data);
              nomeArquivo.append(extensao);
              File arquivo = new File(fileSavePath, nomeArquivo.toString());
              if (arquivo.exists()) {
                arquivo.delete();
              }
              outputStream = new FileOutputStream(arquivo);

              int read = 0;
              byte[] bytes = new byte[1024];

              while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
              }
              resp += cont + ". " + filename;
            } finally {
              if (inputStream != null) {
                try {
                  inputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
              if (outputStream != null) {
                try {
                  outputStream.flush();
                  outputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
          }
        }
      }
    } catch (FileUploadException | SQLException e) {
      if (erro) {
        resp = "Erro ao carregar arquivo.";
      } else {
        resp = e.getMessage();
      }
    }
    if (!erro) {
      request.setAttribute("message", resp);
      getServletContext().getRequestDispatcher("/entregatrabalhos.jsp").forward(request, response);
    }
  }