java - Can't delete file after displaying it in html object using Spring MVC -
plop,
i trying delete file not stored in spring project.
i had mapped ressource folder :
<mvc:resources mapping="/rootfolder/**" location="file:c:/users/mypc/dev/rootfolder/" />
when try delete file never display work fine. problem showing when try delete file displayed once in web app.
even when try delete manually in file system message:
this action can not performed because file open in java (tm) platform se binary
home.jsp
<c:when test="${fileextension == 'application/vnd.ms-excel'}"> <object data="rootfolder/<sec:authentication property="principal.username" />/${filename}" type="${fileextension}" width="100%" style="height:50em"> </object> </c:when> <c:when test="${fileextension == 'application/pdf'}"> <object data="rootfolder/<sec:authentication property="principal.username" />/${filename}" type="${fileextension}" width="100%" style="height:50em"> </object> </c:when> <c:when test="${(fileextension == 'image/png') || (fileextension == 'image/jpg')}"> <img src="rootfolder/<sec:authentication property="principal.username" />/${filename}" width="100%" style="height:50em"/> </c:when>
...
<form name="deletefileform" action="<c:url value="/deletefile"/>" method="post"> <button name="deletefile" type="submit" class="btn btn-danger"> <input id="deletefilename" name="deletefilename" class="hidden" type="text" value="${fileslist}"/> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </button> </form>
deletecontroller.java:
authentication auth = securitycontextholder.getcontext().getauthentication(); string name = auth.getname(); user user = service.getusersfromlogins(name); string filename = request.getparameter("deletefilename"); file folder; file file = new file(user.getusr_folderpath()+"/"+filename); if(file.delete()) { redir.addflashattribute("deletesucced", message.getmessage("billing.message.success.delete", new string[]{file.getname()}, locale.getdefault())); } else { redir.addflashattribute("deletefailed", message.getmessage("billing.message.error.delete", new string[]{file.getname()}, locale.getdefault())); }
downloadfilecontroller:
authentication auth = securitycontextholder.getcontext().getauthentication(); string name = auth.getname(); user user = service.getusersfromlogins(name); string filename = request.getparameter("dlfilename"); inputstream inputstream = null; try { string filepathtobeserved = user.getusr_folderpath()+"/"+request.getparameter("dlfilename"); file filetodownload = new file(filepathtobeserved); inputstream = new autocloseinputstream(new fileinputstream(filetodownload)); response.setcontenttype("application/force-download"); response.setheader("content-disposition", "attachment; filename="+filename); ioutils.copy(inputstream, response.getoutputstream()); response.flushbuffer(); inputstream.close(); } catch (exception e) { system.out.println("request not completed @ moment. please try again."); e.printstacktrace(); } { ioutils.closequietly(inputstream); }
initialisation of fileextension:
if(user.getusr_folderpath() != null) { folder = new file(user.getusr_folderpath()); foldermanager.listfilesforfolder(folder); redir.addflashattribute("fileextension", filemanager.getfiletype(foldermanager.getfiles().get(0))); }
foldermanager:
private arraylist<string> files = new arraylist<string>(); public void listfilesforfolder(final file folder) { (final file fileentry : folder.listfiles()) { if (fileentry.isdirectory()) listfilesforfolder(fileentry); else files.add(fileentry.getname()); } } public list<string> getfiles() { return files; }
filemanager.getfiletype:
public string getfiletype(string filename) { string fileextension = filename.substring(filename.length()-3); if (fileextension.tolowercase().equals("pdf")) return("application/pdf"); else if (fileextension.tolowercase().equals("png")) return("image/png"); else if(fileextension.tolowercase().equals("jpg")) return("image/jpg"); else if (fileextension.tolowercase().equals("xls")) return("application/vnd.ms-excel"); return null; }
thanks :)
Comments
Post a Comment