I want to implement an upload component in my servlet file,but it doesn't work.
The code episode seems like follows:
SmartUpload smartUpload=new SmartUpload();
StringBuffer stringBuffer=new StringBuffer();
smartUpload.initialize(config,request, response);
try {
smartUpload.upload();
File file=smartUpload.getFiles().getFile(0);
stringBuffer.append(file.getFileName());
file.saveAs("/upload_resources/"+stringBuffer);
} catch (SmartUploadException e) {
e.printStackTrace();
}
The upload_resource directory is just under the WebRoot directory,the code runs without any errors ,but the file is just not uploaded. By the way , even I changed the line
file.saveAs("/upload_resources/"+stringBuffer);
to
file.saveAs(request.getRealPath("/upload_resources/")+"/"+stringBuffer);
that is to use an absolute path, the file is not uploaded.
Any help will be appreciate. Thanks.
I've never really worked with SmartUpload, but I can tell that you shouldn't be saving uploaded files in the webapp's deploy folder. They may all get lost whenever the webapp get redeployed with the simple reason that the uploaded files are not contained in the original WAR file. So you should not prepare the upload folder in the webapp's deploy folder, but on a fixed path outside the deploy folder.
If SmartUpload is well designed, I'd expect that
file.saveAs("/upload_resources/"+stringBuffer);
will save it to the /uploaded_resources folder on the root of the same disk as where the webserver is started from. So in for example Windows that would be C:\uploaded_resources. Prepare and use that folder instead.
Further there's another potential problem when you're using the MSIE browser. This browser namely incorrectly includes the full client side path in the filename. I'm not sure if SmartUpload handles this properly, but you might want to debug the actual value of file.getFileName() and make sure that it's really only the filename in the form of filename.ext. Otherwise, you'd need to use String#substring() to substring the part after the last / and \.
Related
I need to modify html file that is placed at server folder from my servlet.
No other way than read it by FileInputStream to byte[], convert to String[] splitting lines by "\n", change what I need and then rewrite it.
I don't see.
This is not possible by design. Your server might just have to serve a .WAR file. If the server is not configured to unzip it, your server will have to read all files directly from this archive. You can now guess that you cannot write at this location.
You would need to create some kind of working directory and also serve files from there, too. You can always use this directory as working directory:
File workingDir = (File)servletContext.getAttribute(ServletContext.TEMPDIR);
I need some help here. It is redicolous, but my TomCat just does not take the file I want. I am trying to read some properties from a xml file. When I startup tomcat with the WAR file inside it makes a folder "MyApp".
My program points to MyApp\WebContent\myXmlFile.xml
It used to work fine, but i needed to edit the myXmlFile.xml so I did by adding two more properties, but it just doesnt read the new data. How is this even possible? I keep removing the "MyApp" folder from inside tomcat\webapps\ so it makes a new one, but it just keeps getting other data. My code is refreshed, but this file doesn't.
Some help would be appreciated a lot.
I am running Tomcat 7
More detail:
The myXmlFile.xml is inside the WAR file. I am not modifying it in runtime. It is suppose to be some sort of a configuration file. It reads the file once everytime there is a call done to this function. It just reads the xml files and puts properties inside a string.
When i upload the WAR file. I will first use shutdown.bat, than I will use startup.bat when the WAR file is in webapps.
Try restarting Tomcat. In Linux, type
/etc/init.d/tomcat8 restart
Ensure you don't have any other copy of this file with the old values in another folder. I would try to delete (or rename) myXmlFile.xml from the WAR and see what is generated in the Tomcat deploy folder.
I tried everything to access to CSV file when a run my jar. I put the CSV in resources package in Eclipse, and it's fine when I run the code from there, but it doesn't work when I run the jar from an executable.
ClassLoader c = MyClass.class.getClassLoader();
URL url = c.getResource("com/mysoft/resources/");
String path = URLDecoder.decode(url.getPath(), "utf-8");
File f = new File(path+ "VAL.csv");
if(f.exists())
...
I don't want to put this file out of the jar. I just want read the file. How can I do it?
EDIT: I closed my login of my first post - Access to csv file in jar - and I don't know how to remove it, and I can't respond to comments. So I posted it again here.
Is it possible not to use InputStream ?
Your use of getResource() here is very dubious. I would not rely on getResource() finding a packet/directory at all (after all, one can argue that it should return resources, not namespaces).
Also, never ever interpret the URL returned by getResource() - its inviting trouble times two. The ClassLoader can return you anything, the URL returned may have nothing in common with what you asked for. Constructing something from the URL's String representation has an abhorrent opportunity of failure when the ClassLoader isn't your standard classpath JRE classloader (think of Application Server, WebStart etc.).
Ask directly for the Resource you want:
getResource("com/mysoft/resources/VAL.csv")
And don't create a File, use InputStream. Resources aren't files, you can not access them with File API.
In my application, i got an function where i need to write a file inside webcontent folder, i got a folder called data and i need to write a new name, if i gave the real path( C:\Users\SanWin\workspace\RoleAccessControl\data\ActivityLog.txt) it writes the file but this has to change when the project is to run on different machine, so i used this to get the real path
ServletContext ctx = getServletContext();
String path = ctx.getRealPath("/data/ActivityLog.txt");
System.out.println(path);
when i print i got the following message in console
C:\Users\SanWin\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\RoleAccessControl\data\ActivityLog.txt
i dont know how the system gets this line
.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\
this is in eclipse, i was expecting something like
C:\Users\SanWin\workspace\RoleAccessControl\data\ActivityLog.txt
how to get the real path i want to get rid of the lines (.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps), I looked and did all possible i seen in internet, but failed, sorry if I made an duplicate post,kindly excuse me
Thanks for your time and responses
In the Servers view in Eclipse you can double click your server to open a configuration screen which allows you to set a different deploy path (at least in Tomcat). Note that you can only modify that setting when there is nothing deployed, otherwise you will see the options grayed out.
The .metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ you are getting is because you have the Use workspace metadata option.
Anyway, if you deploy your war manually directly into Tomcat webapps folder, without Eclipse, you will be getting the correct path.
In other words, your code is correct and is giving you the correct path, you are just not liking the path where your app is being deployed.
Hope this helps.
I'm trying to read from a text file in Netbeans. In the top level of my project directory I have foo.txt. Then in my code I have:
File file = new File("foo.txt");
It throws a FileNotFoundException, however. It's a Java web application using Spring and Tomcat, but I'm not sure if those details matter since I'm running the whole thing inside Netbeans. Basically, I just want to know where I need to put the file so Netbeans will read it.
Update - good call guys, it's looking in Tomcat's bin directory. Now this may be a stupid question but, how would I go about getting it to look in my top level project directory? I feel like dropping text files into tomcat's bin would be innapropriate.
You can try printing the absolute path of the File object to see where it is looking on the filesystem.
System.out.println(file.getAbsolutePath());
I would use the following to figure out where to put the file:
System.out.println(System.getProperty("user.dir"));
To directly answer your question, If you're running an application on Tomcat, files will be opened from the current working directory. That will likely be the bin/ folder in your tomcat directory.
You can find out for sure where your program is looking by examining the result of file.getAbsolutePath().
However, for web applications, I would suggest putting files you need to read in your classpath so you don't have to depend on a certain file structure when you deploy your web application.
try System.getProperty("user.dir") to get current working directory