使用消息正文引发不同的消息异常

时间:2018-08-24 07:36:59

标签: ajax jsp spring-boot exception-handling apache-commons

我正在尝试解压缩一个可能为空的zip文件,没有文件和文件夹。它抛出了 UnsupportedOperationException ,因此我使用 throw 关键字进行处理,然后将消息正文提供给jsp页面。

try {
    boolean upload=unzipUploadedFile(saveZip,folderName);

    if(upload)
            return ServiceResponse.createSuccessResponse();
            else
            return ServiceResponse.createFailureResponse("Files cannot be unzipped"); 

        }catch(Exception e) {
            e.printStackTrace();
            return ServiceResponse.createFailureResponse(e.getMessage());
        }

private boolean unzipUploadedFile(File saveZip, String folderName) throws Exception {
         //Open the file
        try(ZipFile file = new ZipFile(saveZip.getCanonicalPath()))
        {

        FileSystem fileSystem = FileSystems.getDefault();
        //Get file entries
        Path inputpath=fileSystem.getPath(file.getName());
        Enumeration<? extends ZipEntry> entries = file.entries();

      //We will unzip files in this folder
            File directory=new File(zipFilePath.concat(folderName));

            if(!directory.exists()) {
                directory.mkdir();
            }

        //Iterate over entries
        while (entries.hasMoreElements())
        {
            ZipEntry entry = entries.nextElement();
            String abc[]=entry.getName().split("/");

            //Else create the file
            if(!entry.isDirectory())
            {
                InputStream is = file.getInputStream(entry);
                BufferedInputStream bis = new BufferedInputStream(is);
                String uncompressedFileName = zipFilePath +folderName+"/"+ abc[abc.length-1];
                Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
                if(Files.notExists(uncompressedFilePath)) 
                Files.createFile(uncompressedFilePath);
                FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
                while (bis.available() > 0)
                {
                    fileOutput.write(bis.read());
                }
                fileOutput.close();
                System.out.println("Written :" + entry.getName());
                is.close();
                bis.close();
            }
        }
        file.close();
        Files.deleteIfExists(inputpath);
        fileSystem.close();

    }catch(UnsupportedOperationException e)
    {
       throw new UnsupportedOperationException("Uploaded zip does not contain proper file or folder");
    }
    catch(Exception e) {
        throw new Exception("File unizipping unsuccessful");
    }
    return true;
}

但是,如果我仍然在其中添加所有文件的zip压缩文件,那仍然可以为我提供上载的zip压缩文件,但在UI端没有正确的文件或文件夹。

我在其中调用控制器代码的Jsp页面

$
            .ajax({
                type : 'POST',
                url : baseUrl + url,
                data : data,
                cache: false,
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function(dataset){
                    console.log(dataset.error);
                    document.getElementById("spinner").style.display = "none";
                    $('#reportError').modal('show');

                     document.getElementById('uploadError').innerHTML="<center>"+dataset.error+"</center>";
                     document.getElementById('error_title').innerHTML="Upload Failure";

                },

                error : function(e){
                    console.log(e);
                }
            });

堆栈跟踪

java.lang.UnsupportedOperationException: Uploaded zip does not contain proper file or folder
        at com.tata.green.controller.GreenBackendServiceController.unzipUploadedF
ile(GreenBackendServiceController.java:580)
        at com.tata.green.controller.GreenBackendServiceController.uploadServerDe
tails(GreenBackendServiceController.java:509)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvok
e(InvocableHandlerMethod.java:205)

0 个答案:

没有答案