簡體   English   中英

無法使用JSP刪除FILE

[英]Can't delete FILE with JSP

我有一個帶有此代碼的JSP

 File folder = new File("C:/Servidorweb/apache-tomcat-6.0.37/webapps/facturacion_sms/archivos");
        File[] listOfFiles = folder.listFiles();

        //Inicializa array contenedor de nombres de archivos 
        String[] array = new String[listOfFiles.length];




        for (int i = 0; i < listOfFiles.length; i++)  //Recorrido a lista de archivos , se almacenan en array
        {
            if (listOfFiles[i].isFile()) 
            {
                array[i] = listOfFiles[i].getName();
                //out.println(array[i]);
            }
        }

        for(int j = 0; j < array.length ;j++)
        {
             String archivostr =  "C:/Servidorweb/apache-tomcat-6.0.37/webapps/facturacion_sms/archivos/" + array[j];

             out.println(archivostr);

                File archivo = new File(archivostr);

                try
                {
                archivo.delete();
                }
                 catch (Exception e)
              {
                out.println("An exception occurred: " + e.getMessage());
              }


        }

一切在我的本地Web服務器上都可以正常運行,但是當我在Red Hat中運行JSP時, archivo.delete(); 不起作用。 我擁有所有.jar文件,並且路徑都很好(我將其上載到其他服務器時更改了路徑,並且我知道路徑中沒有錯誤,因為我在循環中將其打印了出來)。

另外,它不會引發任何異常。

Web服務器是TOMCAT6,OS RedHat Local Web服務器是TOMCAT6和Windows

有任何想法嗎?

Everything works fine in my local web server but when I run the JSP in Red Hat the archivo.delete(); doesn't work.

這是因為,當您切換到其他操作系統時,文件系統會發生變化,並且訪問文件的方式也會發生變化,如果您這樣硬編碼路徑,則無法訪問其他操作系統上的文件

我認為您的分隔符確實有問題。

String archivostr =  "C:/Servidorweb/apache-tomcat-6.0.37/webapps/facturacion_sms/archivos/" + array[j];

在上面的代碼上,只需嘗試使用像

String archivostr =  "C:"+File.separator+"Servidorweb"+File.separator+"apache-tomcat-6.0.37"+File.separator+"webapps"+File.separator+"facturacion_sms"+File.separator+"archivos"+File.separator+array[j];

使用File.separator而不是斜杠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM