0
votes

I am working on spring hibernate and I want to import an excel file. I want to make a check on extensions so that no one can upload a file other than excel i.e. restrict the import to files with one of these extensions: xls or xlsx. My code is here:

public class ImportCandidatesFormController extends BNUAbstractFormController {

    private ImportCandidatesBL importCandidatesBL;
    private ExcelReader reader;

    @Override
    protected ModelAndView processFormSubmission(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException arg3)
            throws Exception {

        FileUploadVO vo = (FileUploadVO) command;
        MultipartFile file = vo.getFile();

        System.out.println("File Uploaded: " + file.getOriginalFilename());
        boolean isSuccessful = importCandidatesBL.importAndSaveCandidates(
                file.getInputStream(),
                SessionUtil.getCurrentUser(request.getSession()));

        return new ModelAndView(new RedirectView("importCandidates.do?s=1"));
    }

    public ImportCandidatesBL getImportCandidatesBL() {
        return importCandidatesBL;
    }

    public void setImportCandidatesBL(ImportCandidatesBL importCandidatesBL) {
        this.importCandidatesBL = importCandidatesBL;
    }
}
1
i need a code which will check either the extension is xlsx or not ,.,,, if it is it will upload otherwise nothng happens..u know wat i m trying to say - Irfanz Sheikh

1 Answers

0
votes
String lowercaseFileName = file.getOriginalFilename().toLowerCase();
if (!(lowerCaseFileName.endsWith(".xls") || lowerCaseFileName.endsWith(".xlsx"))) {
    // reject file
}