Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For those who eat java, think java and drink java

 
Nitin Dubey
Ranch Hand
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
M developing a client server application where the client uploads and downloads several files. M pushing and pulling all the files to and from SQL server database. My client is an application and server side I have Servlets kept in tomcat and M using JDBC ODBC drivers to connect to SQL Server Database.
1. I want all the files to be uploaded to a temporary folder in my server(I may use it in future for scheduling). Now for uploading files to the temporary folder I must have physical path of the server to create File object. The problem is how do I get the physical path of the folder where my servlet is kept without involving any property file. Also suggest me if there is another optimized solution for the same and whether I can create files using URL's.
Ex. I access my servlet by giving http://nitind:8080/examples/servlet/servletName
I want => C:\jakarta-tomcat-3.2.1\webapps\examples\WEB-INF\classes
2. I also want a good BLOB Example for uploading files to the server. I m aware of the methods but m not sure how do i make use of it ..
Waiting for your replies ...

Nitin Dubey
 
hemanth kumar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if i am wrong
u can get the physical path to where ur servlet is stored by
out.println("getPathTranslated: " + request.getPathTranslated());
hemanth
 
hemanth kumar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is also
out.println("getServletPath: " + request.getServletPath());
hemanth
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Nitin,
If your requirement is to upload multiple files at a time then it is better you go for RMI where you will write a file upload code to your client class and the files will be uploaded to your server(in this case RMI server).
Otherwise you can use MultiPartRequest class given in Oreilly Servlets book.For the latest changes visit to Oreilly site where they have given all the classes required to use MultiPartRequest class.If you want to use them for commercial use the Licence agreement says that you should have the Oreilly Servlet book.
I hope this will help.
 
Nitin Dubey
Ranch Hand
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx hemanth and vaibhav for your replies ..
Hemanth I have also tried your suggestion but it didn't work.
Vaibhav I m doing application - servlet communication therefore I cannot use the Oreilly examples u mentioned (I suppose). I m using these examples for extranet and its running successfully.
I made a list of all the methods related to path. But could'nt get the result. Below is the code and output I got..
Source ==>
public class trytest extends HttpServlet{
public void init(ServletConfig cfg){
try{
super.init(cfg);
cfg.getServletContext();
//System.out.println("cfg.getRealPath()" + cfg.getRealPath());
//System.out.println("cfg.getRealPath()" + cfg.getServletC());
}
catch(Exception e){
System.out.println(e);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res){
String str = "";
try{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
System.out.println("getAuthType() " + req.getAuthType() );
System.out.println("getCookies() " + req.getCookies() );
//System.out.println("getDateHeader(String) " + req.getDateHeader(String));
//System.out.println("getHeader(String) " + req.getHeader(String) );
System.out.println("getHeaderNames() " + req.getHeaderNames() );
//System.out.println("getIntHeader(String) " + req.getIntHeader(String) );
System.out.println("getMethod() " + req.getMethod() );
System.out.println("getPathInfo() " + req.getPathInfo() );
System.out.println("getPathTranslated() " + req.getPathTranslated() );
System.out.println("getQueryString() " + req.getQueryString() );
System.out.println("getRemoteUser() " + req.getRemoteUser() );
System.out.println("getRequestedSessionId() " + req.getRequestedSessionId() );
System.out.println("getRequestURI() " + req.getRequestURI() );
System.out.println("getServletPath() " + req.getServletPath() );
//System.out.println("cfg.getRealPath()" + ServletConfig.getRealPath(req.getServerName()));
str = getServletContext().getRealPath("/trytest");
String f = new File(str).getCanonicalPath();
str = System.getProperty("server_root");
System.out.println("Root : "+f);
}
catch(Exception ex){
System.out.println(ex);
}
}
}
Output ==>
getAuthType() null
getCookies() [Ljavax.servlet.http.Cookie;@63b895
getHeaderNames() org.apache.tomcat.util.MimeHeadersEnumerator@5e3974
getMethod() GET
getPathInfo() null
getPathTranslated() null
getQueryString() null
getRemoteUser() null
getRequestedSessionId() null
getRequestURI() /nitin/servlet/trytest
getServletPath() /servlet/trytest
Root : C:\jakarta-tomcat-3.2.1\webapps\nitin\trytest

Actual Path : C:\jakarta-tomcat-3.2.1\webapps\nitin\WEB-INF\classes
waiting for your replies guys,

nitin
 
Nitin Dubey
Ranch Hand
Posts: 126
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
IS IT THAT NO ONE IN JAVARANCH KNOWS ANSWER ?? THAT IS REALLY STRANGE ...
nitin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic