Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting TomCat Path  RSS feed

 
sasi kals
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i want to upload a file to the tomcat webapps i done like below



<%@ page import="java.util.*,java.io.*"%>
<%
String path = request.getParameter("filename");
String newPath = "";
int count = 0;

if (path != null) {
ArrayList arr = new ArrayList();
StringTokenizer st = new StringTokenizer(path, "\\");
while (st.hasMoreTokens()) {
arr.add(count, st.nextToken());
count++;
}
String Path = request.getRealPath("");
System.out.println("------------->" + Path);
newPath = Path + "\\upload\\" + arr.get(count - 1);
int c;
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(newPath);
while ((c = fis.read()) != -1) {
fos.write((char) c);

}
fos.flush();
fos.close();

}
out.println("1.File Uploaded from :: " + path);
out.println("<br>");
out.println("<br>");
out.println("2.Uploaded File is Saved in :: " + newPath);
%>
And this is running well in a single system. but i want to do it with many client.

like when the tomcat is running in one system. now i want to upload a file from some other system to the that system where tom cat is running.please help me ..

thanks in advance..
 
Devaka Cooray
Marshal
Posts: 5576
719
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sasi kals wrote:And this is running well in a single system. but i want to do it with many client.

Could you explain this with more details?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

And welcome to JavaRanch!
 
sasi kals
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sasi kals wrote:Hi i want to upload a file to the tomcat webapps i done like below

<code>

<%@ page import="java.util.*,java.io.*"%>
<%
String path = request.getParameter("filename");
String newPath = "";
int count = 0;

if (path != null) {
ArrayList arr = new ArrayList();
StringTokenizer st = new StringTokenizer(path, "\\");
while (st.hasMoreTokens()) {
arr.add(count, st.nextToken());
count++;
}
String Path = request.getRealPath("");
System.out.println("------------->" + Path);
newPath = Path + "\\upload\\" + arr.get(count - 1);
int c;
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(newPath);
while ((c = fis.read()) != -1) {
fos.write((char) c);

}
fos.flush();
fos.close();

}
out.println("1.File Uploaded from :: " + path);
out.println("<br>");
out.println("<br>");
out.println("2.Uploaded File is Saved in :: " + newPath);
%>
</code>
And this is running well in a single system. but i want to do it with many client.

like when the tomcat is running in one system. now i want to upload a file from some other system to the that system where tom cat is running.please help me ..

thanks in advance..
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost. Reading the link I provided will explain how to do it correctly. Thanks!
 
Tim Holloway
Bartender
Posts: 18720
72
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DON'T DO THIS!!!

You should never upload files into the WAR. A WAR should be considered as read-only. Even systems like Tomcat where a loophole exists that make it physically possible to write into a WAR directory aren't safe, since the option to "explode" a WAR file into its component filesystem objects is exactly that - a server-configurable option that can be turned off if the webmaster so desires. WAR files themselves are ZIP files, and there's not even an API to update elements in ZIP files in Java - you'd have to create a whole new ZIPfile.

Most importantly, when you update a WAR, you generally end up replacing the entire WAR, which typically destroys everything in the old WAR, including the files you'd previously uploaded.

Always upload files to a "permanent" directory that's external to the webapp. In Linux/Unix systems, a popular choice is somewhere under the "/var" directory. Windows hasn't really got a conventional place for such things, so choose your own, just as long as it's completely outside Tomcat.

The fact that the other clients are not on the same computer as the Tomcat server doesn't matter. Your app isn't really directly accessing the client files (actually, it can't!). What really happens is that you instruct your browser to open the client-side file and copy its contents to a URL on the server. The URL handling code on the server then takes the incoming data stream and writes it to a file local to the server - which, again, should not be inside your WAR.

Also note that IE includes the full client-side source path as part of the data that gets uploaded. Which is almost certainly a violation of the defining RFC for HTTP file uploads, and is a security concern as well, since it potentially tells Bad People about the internal organization of your client system. Non-IE browsers usually only send the filename itself and not its parent directory path.

Incidentally, I recommend you use "Unix notation" for filenames in Java. That is, instead of "C:\\myapp\\upload", say "C:/myapp/upload". This helps keep the code more general-purpose and as a side benefit you don't risk forgetting to double up on the "\\"s in a critical place, since "\" is an escape character in Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!