java - Why are only SOME of the files and folders not deleted? -
java - Why are only SOME of the files and folders not deleted? -
i generating files servlet (output.txt
) , xsl file (the folder generatedapps
, content) , pack files , folders generated xsl transformation zip archive. after client downloads zip archive want delete it, other files , folders.
this code:
inputstream inputstream = new bytearrayinputstream(request.getparameter("content") .getbytes("utf-8")); seek { transformerfactory tfactory = new net.sf.saxon.transformerfactoryimpl(); transformer transformer = tfactory.newtransformer(new streamsource( getservletcontext().getresourceasstream("lib/generategeddyjscode.xsl"))); fileoutputstream fos = new fileoutputstream("output.txt"); transformer.transform(new streamsource(inputstream), new streamresult(fos)); string appname = new scanner(new file("output.txt")).usedelimiter("\\z").next(); zipusingjavautil zipper = new zipusingjavautil(); zipper.zipfiles("generatedapps/" + appname, "generatedapps/" + appname + "archive.zip"); string path = "generatedapps/"; string filename = appname + "archive.zip"; file f = new file(path + filename); response.setcontenttype("application/zip"); response.setcontentlength((int)f.length()); response.addheader("content-disposition","attachment;filename=\"" + filename + "\""); byte[] arbytes = new byte[32768]; fileinputstream = new fileinputstream(f); servletoutputstream op = response.getoutputstream(); int count; while ((count = is.read(arbytes)) > 0) { op.write(arbytes, 0, count); } op.flush(); op.close(); is.close(); fos.close(); inputstream.close(); file tobedel = new file("generatedapps/"); generategeddyjscode.removedirectory(tobedel); file output = new file("output.txt"); output.delete();
the method removedirectory
following:
public static void removedirectory(file dir) { if (dir.isdirectory()) { file[] files = dir.listfiles(); if (files != null && files.length > 0) { (file afile : files) { removedirectory(afile); } } } else { dir.delete(); } }
the problem in end zip archive deleted (btw, archive within generatedapps
). tried close streams, because i've read not closed streams prevent deletion.
how can delete remaining files , folders?
==================================== edit ========================================
my updated deletion method:
public static void removedirectory(file dir) { if (dir.isdirectory()) { file[] files = dir.listfiles(); if (files != null && files.length > 0) { (file afile : files) { removedirectory(afile); } } else if (files.length == 0) { dir.delete(); } } dir.delete(); }
still not working.
==================================== edit 2======================================== added:
inputstream instream = getservletcontext().getresourceasstream("lib/generategeddyjscode.xsl"); streamsource instreamsource = new streamsource(instream); transformer transformer = tfactory.newtransformer(instreamsource);
and later instream.close()
. still not working.
you can check whether zip utility closing files properly... have access zipusingjavautil class source?
each entry in zip file has closed. every file input stream opened zip writing process should closed.check below sample: http://www.journaldev.com/957/java-zip-example-to-zip-single-file-and-a-directory-recursively
java tomcat servlets stream tomcat6
Comments
Post a Comment