当我不发送文件时,如何替换尝试捕获量以不获取错误例外?

发布于 2025-02-06 18:07:55 字数 2524 浏览 2 评论 0原文

我将变量从Ajax发送到Java!在我的AJAX方法中,我发送了2个变量:iDtabprestidpolice! 我会得到错误例外,因为我没有传递文件!即使我不发送文件,我如何才能使此函数起作用并读取变量。 我评论了文件传递变量的尝试/捕获!但是在某些AJAX方法中,我正在使用该文件。

public String upload_via_ajax() {
    
    try {

        String property = "java.io.tmpdir";
        String tempDir = System.getProperty(property);
        String html = "inside upload via ajax";
//          try { 
//              File destFile = new File(tempDir, myFileFileName.substring(0,
//                      myFileFileName.lastIndexOf(".")) + ".csv");
//              FileUtils.copyFile(myFile, destFile);
//
//              getRequest().getSession().setAttribute("filePath",
//                      destFile.getPath());
            //POUR REFERENCE DECLARATION
            
            if(annee != null) {
                TableReferentielDto tableReferentielDto = new TableReferentielDto();
                tableReferentielDto.setId(Long.parseLong(annee));
                TableReferentielDto anneeLabel =(TableReferentielDto) tableReferentielGetByIdCmd
                .execute(tableReferentielDto);
                
                getRequest().getSession().setAttribute("annee",
                        anneeLabel.getLibelle());
            }
            //POUR DATE DEBUT DECLARATION
            if(moisDebutDeclaration != null){

                getRequest().getSession().setAttribute("moisDebutDeclaration",
                        moisDebutDeclaration);
            }
            //POUR DATE FIN DECLARATION
            if(moisFinDeclaration != null){
                getRequest().getSession().setAttribute("moisFinDeclaration",
                        moisFinDeclaration);
            }
            //ID POLICE
            if(idPolice != null){
                getRequest().getSession().setAttribute("idPolice",
                        idPolice);
            }
            if(idTabPrest != null) {
                getRequest().getSession().setAttribute("idTabPrest",
                        idTabPrest);
            }
            if(idTabTarif != null){
                getRequest().getSession().setAttribute("idTabTarif",
                        idTabTarif);
            }
            
            html = "file uploaded";
        

//          } catch (IOException e) {
//
//              html = "error in uploading file";
//              e.printStackTrace();
//          }

        return SUCCESS;
    } catch (Exception e) {
        return SUCCESS;
    }
}

I am sending variables from ajax to java! in my ajax method I am sending 2 variables: idtabPrest and idPolice!
I am getting an error exception because I am not passing the File ! how can I make this function work and read the variables even if i am not sending the File.
I commented the Try/catch of the file to pass the variables! but in some ajax methods I am using the file.

public String upload_via_ajax() {
    
    try {

        String property = "java.io.tmpdir";
        String tempDir = System.getProperty(property);
        String html = "inside upload via ajax";
//          try { 
//              File destFile = new File(tempDir, myFileFileName.substring(0,
//                      myFileFileName.lastIndexOf(".")) + ".csv");
//              FileUtils.copyFile(myFile, destFile);
//
//              getRequest().getSession().setAttribute("filePath",
//                      destFile.getPath());
            //POUR REFERENCE DECLARATION
            
            if(annee != null) {
                TableReferentielDto tableReferentielDto = new TableReferentielDto();
                tableReferentielDto.setId(Long.parseLong(annee));
                TableReferentielDto anneeLabel =(TableReferentielDto) tableReferentielGetByIdCmd
                .execute(tableReferentielDto);
                
                getRequest().getSession().setAttribute("annee",
                        anneeLabel.getLibelle());
            }
            //POUR DATE DEBUT DECLARATION
            if(moisDebutDeclaration != null){

                getRequest().getSession().setAttribute("moisDebutDeclaration",
                        moisDebutDeclaration);
            }
            //POUR DATE FIN DECLARATION
            if(moisFinDeclaration != null){
                getRequest().getSession().setAttribute("moisFinDeclaration",
                        moisFinDeclaration);
            }
            //ID POLICE
            if(idPolice != null){
                getRequest().getSession().setAttribute("idPolice",
                        idPolice);
            }
            if(idTabPrest != null) {
                getRequest().getSession().setAttribute("idTabPrest",
                        idTabPrest);
            }
            if(idTabTarif != null){
                getRequest().getSession().setAttribute("idTabTarif",
                        idTabTarif);
            }
            
            html = "file uploaded";
        

//          } catch (IOException e) {
//
//              html = "error in uploading file";
//              e.printStackTrace();
//          }

        return SUCCESS;
    } catch (Exception e) {
        return SUCCESS;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文