This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
hi everyone.. i would like to know how to save the file path in mysql from a servlet. The following code is what i have done while uploading a file to my web app and save the file path in mysql :
and this is the method i called to insert into database :
the database table is files which contains three fields : file_id(integer auto increement), file_name(varchar), file_path(varchar)
But when i save this way , the file name gets saved fine , but the file path is saved in the following format : java.io.FileOutputStream@106dc2d This is not what i want , i want the path as C://myfolder/etc.... i mean the real address to where the file is uploaded.. PLEASE HELP ..
As a general rule, I recommend using the "Unix" format for filesystem paths, even when the OS is Windows. It's more portable, and the backslash has another and nastier meaning in Java, so the forward slashes are safer. In a database, however, this isn't always desirable, since non-java apps might want to use that path as well. Windows apps will sometimes honor filepaths in Unix format, but the rules are not real clear on when.
Also, the java.io.File class has some vary useful functions for putting together and pulling apart filename paths., You can use them instead of brute-force string operations and they're much tidier. It took me quite a while to realize that "File" in java actually refers more to the filename path than it does to the data (or directory) behind that path.
Customer surveys are for companies who didn't pay proper attention to begin with.
Sir , thank you for your kind response, but the getAbsolutePath() method does not work. when i write the above code , error message shows up saying "cannot find symbol ,symbol : method getAbsolutePath() , location : class.java.lang.String"
so putting getAbsolutePath() on a String Object will and does fail as you found out.
It has to be done earlier, not in the insertfile method.
From your commented code I noticed:
Notice filepath = fileOut.toString(). This simply invokes toString() on the FileOutputStream Object which is why you are getting:
java.io.FileOutputStream@106dc2d and not the physical filepath as a String.
Also, isn't rootPath + "/" what you want to save as the file path?
This is why it's better to keep the code samples short: so that sloppy people like me won't mis-read them. The getAbsolutePath() method has to be invoked on a java.io.File object. If you're using a String (brute force), the method does not exist for a java.lang.String and will therefore fail.
Also, speaking of sloppy scanning, it appears to my sloppy scan like you may be attempting to locate the filesystem path of your webapp with the possible intention of storing files in it. Do not do that. The webapp (WAR) should be treated as read-only. Failing to observe this restriction will bite you sooner or later. Also, the getRealPath() method can return null if the WAR wasn't unpacked (exploded) by the appserver (and there's no guarantee that it will be). So keep your workfiles, uploaded files, and so forth out of the WAR and out of the webapp server. Put them in an external directory where they'll be safe.
For read-only files, you can put them in the WAR, but a better way of accessing them is using the getResource or getResourceAsStream methods.