servlets - java.io.FileNotFoundException when writing uploaded file to disk via getRealPath() -
glassfish seems adding path want save image file too, there way utilize absolute path servlet gets with
string apppath = request.getservletcontext().getrealpath("");
?
i have spent days trying different methods upload image file , have servlet save disk.
i used example: http://www.codejava.net/java-ee/servlet/how-to-write-upload-file-servlet-with-servlet-30-api
using debug can see file name , path info collected correctly code fails @ `part.write(savepath + file.separator + filename);``
and glassfish's exception study is:
exception java.io.filenotfoundexception: c:\program files\glassfish-3.1.2.2\glassfish\domains\domain1\generated\jsp\com.onemore_onemore-web_war_1.0-snapshot\d:\dropbox\git\brian_jee\onemore\onemore\onemore-web\target\onemore-web-1.0-snapshot\image\screengrab_all_products.jpg (the filename, directory name, or volume label syntax incorrect)
i can see right path part of exception d:\dropbox\git\brian_jee\onemore\onemore\onemore-web\target\onemore-web-1.0-snapshot\image\screengrab_all_products.jpg
jsp
<form action="${pagecontext.request.contextpath}/imageupload" method="post" enctype="multipart/form-data" name="productform" id="productform"> <input type="file" name="file" id="file"> <input type="submit" name="submit" value="submit"></td> </form>
servlet
@webservlet(name = "uploadimageservlet", urlpatterns = {"/imageupload"}) @multipartconfig(filesizethreshold = 1024 * 1024 * 2, // 2mb maxfilesize = 1024 * 1024 * 10, // 10mb maxrequestsize = 1024 * 1024 * 50) // 50mb public class uploadimageservlet extends httpservlet { /** * name of directory uploaded files saved, relative * web application directory. */ private static final string save_dir = "image"; /** * handles file upload */ @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // gets absolute path of web application string apppath = request.getservletcontext().getrealpath(""); // constructs path of directory save uploaded file string savepath = apppath + file.separator + save_dir; // creates save directory if not exists file filesavedir = new file(savepath); if (!filesavedir.exists()) { filesavedir.mkdir(); } (part part : request.getparts()) { string filename = extractfilename(part); part.write(savepath + file.separator + filename); } request.setattribute("message", "upload has been done successfully!"); getservletcontext().getrequestdispatcher("/web-inf/jsp/newproduct.jsp").forward( request, response); } /** * extracts file name http header content-disposition */ private string extractfilename(part part) { string contentdisp = part.getheader("content-disposition"); string[] items = contentdisp.split(";"); (string s : items) { if (s.trim().startswith("filename")) { homecoming s.substring(s.indexof("=") + 2, s.length() - 1); } } homecoming ""; } }
never utilize getrealpath()
you should not save uploaded files in deploy folder. have explained many times before. 1 of explanations can found in answer: uploaded image available after refreshing page.
any jsp/servlet code snippet found on blog/article ever uses getrealpath()
method should taken huge handbag of salt. quality , knowledge of author should questioned. there namely no sensible real world utilize case method.
save uploaded files in fixed path outside deploy folder. see how set folder storing file uploads using commons fileupload.
servlets file-upload filenotfoundexception realpath
No comments:
Post a Comment