事情是这样的,我做一个生成报表的方法,要求可以下载,可以删除服务器上生成的报表。
但现在下载后服务器文件就无法删除了,不下载直接删除是可以的。。
我用的方法是直接url打开该文件,如<a href='/export/print/1.zip'>1.zip</a>,然后文件就被锁住了。
目前来看是只能强制删除了,谁能提供个强制删除的方法不?或者告诉我有什么其他方法可以达到这一目的?ps:用360解除占用文件的功能,有时可以发现该文件被jdk/bin/javaw.exe占用了,有时却提示没有被占用,反正就是无法删除,在服务器这手动删也删不掉。
但现在下载后服务器文件就无法删除了,不下载直接删除是可以的。。
我用的方法是直接url打开该文件,如<a href='/export/print/1.zip'>1.zip</a>,然后文件就被锁住了。
目前来看是只能强制删除了,谁能提供个强制删除的方法不?或者告诉我有什么其他方法可以达到这一目的?ps:用360解除占用文件的功能,有时可以发现该文件被jdk/bin/javaw.exe占用了,有时却提示没有被占用,反正就是无法删除,在服务器这手动删也删不掉。
<html>
<head>
<%@ page contentType="text/html; charset=UTF-8" deferredSyntaxAllowedAsLiteral="true"%>
<%@ page import="java.net.URLEncoder,java.io.*"%> </head>
<body>
<%
response.reset();//可以加也可以不加
response.setContentType("application/x-download");
String folder=request.getParameter("folder");
String filename=request.getParameter("filename");
String realPath = request.getRealPath("");
String filenamedownload =realPath+File.separator+"export"+File.separator+folder+File.separator+filename;
System.out.println(filenamedownload);
String filenamedisplay =filename;//系统解决方案
//filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
//因为文件名有中文,所以在这需要转为iso8859-1
filenamedisplay = new String(filenamedisplay.getBytes("UTF-8"),"iso8859-1");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay); OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload); byte[] b = new byte[1024];
int i = 0; while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
%>
</body>
</html>