java - Create a new file at webcontent or web-inf in a Spring MVC Application -
@requestmapping(value = "/upload", method = requestmethod.post) public @responsebody string upload(multiparthttpservletrequest request, httpservletresponse response) { iterator<string> itr = request.getfilenames(); string filename=itr.next(); multipartfile file = request.getfile(filename); if (!file.isempty()) { try { file filenew=new file(file.getoriginalfilename()); byte[] bytes = file.getbytes(); bufferedoutputstream stream = new bufferedoutputstream(new fileoutputstream(filenew)); stream.write(bytes); stream.close(); system.out.println(filenew.getabsolutepath()); return "you uploaded " + filename + " " + filename + "-uploaded !"; } catch (exception e) { return "you failed upload " + filename + " => " + e.getmessage(); } } else { return "you failed upload " + filename + " because file empty."; } }
file gets created @
f:\springsource-tool-suite-2.8.1.release-e3.7.1-win32\springsource\sts-2.8.1.release\
to make long story short, create new file @ webcontent or web-inf.
thanks!!
can autowired
servletcontext
in controller, real path method@autowired
servletcontext servletcontext;
or vzamanillo's answer
request.getsession().getservletcontext().getrealpath("/web-inf/");
Comments
Post a Comment