flower
flower

Reputation: 2242

How to get the tomcat web absolute path in java from one application to the other application in centos?

My tomcat have two web application called webstock and webstockapi.And both of then have a fold called imgfold. Each time I called the api and I upload the image file to the path 'webstockapi/imgfold',I have to copy them to the other path 'webstock/imgfold'.How to get the web absolute path of webstock when I called the api of webstockapi.In fact my web absolute path is "/application/tomcat/webapps/webstockapi" and "/application/tomcat/webapps/webstock". Now I have think a way,but it is complex,and it must have a simple way.

//get the imgfold path when I call the webstockapi application
String uploadDir = session.getServletContext().getRealPath("/imgfold");
//my complex way to get the other appliction path
String destinationPicUrl=new File(session.getServletContext().getRealPath("/")).getParent()+
                     File.separatorChar +"webstock"+ File.separatorChar +"imgfold";

How can I get the web absolute path according to the publish web application name directly ?

Upvotes: 0

Views: 679

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48057

It's bad practice to upload into a webapplication's folder - if for nothing else than making backups incredibly hard. As soon as you update your webapp, all of the content will be deleted and replaced with your updated webapp. Also, no application server guarantees that a web application is even stored in a writeable folder. For security reasons alone this is a bad idea (For example: If someone uploads JSP files into a folder within the web application, tomcat will happily execute any uploaded code in that JSP)

Configure both applications to store data in a folder separate from the application. That folder can be part of your backup strategy. If both point to the same folder, there's no more need to copy files back and forth. Then write a simple download servlet that makes the images available to the world, without the risk of server-side code execution. There are plenty of samples for Download servlets everywhere - choose one that's hardened against traversing random folders and just returns data from the configured one.

Upvotes: 1

Related Questions