![](/img/trans.png)
[英]Upload a file from client side to server side with ajax and spring portlet
[英]not able to delete file in server from client side?
我能夠獲取目錄的文件名,但無法刪除它們
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.util.* ,java.text.* , java.io.*,java.io.File" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String files;
boolean issuccess=true;
//out.println("hi ");
String strDirectoy3=config.getServletContext().getRealPath("/");
//File file = new File("D:\\Test\\hireminibusandcoach.co.uk\\ROOT\\administrator"); FileInputStream in = new FileInputStream("c:/a.txt");
try{
File file = new File(strDirectoy3);
File[] listOfFiles = file.listFiles();
for (int i = 0; i <2; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
out.println(listOfFiles[i].toString());
issuccess=new File(files).delete();
}
}
out.println(" /n Deletion "+issuccess);
}catch(Exception e){
out.println(e);
}
%>
</body>
</html>
首先,JSP不是執行此類操作的好地方。 然后第二件事是您正在使用JDK7 。 最好切換到NIO.2 API
,以防舊File API失敗時拋出異常。 因此,您應該嘗試這樣做,以使其類似於NIO.2 API
如下所示:
try
{
Path path = file.toPath();
Files.delete(path);
}
catch (NoSuchFileException x)
{
System.err.format("%s: no such" + " file or directory%n", path);
}
catch (DirectoryNotEmptyException x)
{
System.err.format("%s not empty%n", path);
}
catch (IOException x)
{
// File permission problems are caught here.
System.err.println(x);
}
請確保運行Web服務器的用戶有權刪除這些文件。 canWrite()方法返回哪個父目錄?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.