2012-07-03 59 views
0

我正在春季休眠,我想導入一個excel文件。我想對擴展進行檢查,以便沒有人能夠上傳除excel以外的文件,即將導入限制爲具有以下其中一種擴展名的文件:xls或xlsx。我的代碼是在這裏:Excel文件導入java春季休眠

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; 
    } 
} 
+0

那麼,有什麼問題嗎? –

+0

我需要一個代碼來檢查擴展名是xlsx還是不是,如果是,它會上傳,否則nothng發生..我知道我想說wat我想說 –

回答

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

thry ury很多 –