Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

browse folder

<tr align="center">
      <td align="right">Browse a location to save file:<br /> </td>  
      <td> <input type="file" name="file_location" size="chars" /></td>
</tr>
<tr>
      <td align="center" colspan="2">
      <input class="button" value="Submit" type="button" onclick="performAction()" /></td>
</tr>


when i use the above code I was able to select a file
but how to browse a folder instead of file.

using this I can get file location and truncate the last filename to get folder address...
but is it possible to browse folder directly...

by using the above code I got location
C:\\Users\\admin\\Temp\\file1.txt

after using the below code which I wrote on server side  by sending the filelocation...
I got
C:\\Users\\admin\\Temp

int ix = file_location.lastIndexOf('\\');
      if (ix < 0) {
                ix = file_location.lastIndexOf('/');
            }
      if (ix >= 0) {
      filelocation = file_location.substring(0, ix);
      }

Is it possible to directly browse folder (C:\\Users\\admin\\Temp) instead of file (C:\\Users\\admin\\Temp\\file1.txt)
Avatar of rrz
rrz
Flag of United States of America image

I am confused. What are your intentions ?  Are you trying to upload a file from the client to the server ?
>Is it possible to directly browse folder  
No. The idea behind file input in HTML forms is to let users include a file from their system into a form submission.

Once you have the folder location, what are you going to do ?
Avatar of shragi

ASKER

I want provide an option to customer to select a folder to save their file...  

for that I used <input type ="file">

which is selcting file instead of folder....
Avatar of shragi

ASKER

once after having folder location I save the form data in the form of xml and store in a folder location choosen by the customer...

How does this question relate to your other question at
https://www.experts-exchange.com/questions/26795736/store-the-path-from-browse-button.html   
The solution you accepted there was to upload the form data to your server and create a file on the server. Once the file is on server then it can be downloaded by the client into a folder of his choosing on his machine.
Avatar of shragi

ASKER

I forgot to mention an important point....

the change here is I want to provide user to select a location to save
their file on server machine...

when the customer click on browse button he should be
able to see server directories to select a location for saving the file...

is it possible to browse server directories from my workspace....
>is it possible to browse server directories from my workspace....  
What is  "my workspace.... "  ?
Avatar of shragi

ASKER

client machine....

I have many work stations(PC's or client machines) and one tomcat server....

is it possible to browse the server directories from all these machines...

if it is possible how can I browse them...


Where on the server do you want to save the files ?  
Here is first step JSP.
<%@ page import="java.io.File" %>
<html>
<body>
<%
   File appPath = new File(application.getRealPath("/"));

%>
<%=appPath%>
</body>
</html>

Open in new window

 
Do you want to save the files within your web app ?
Avatar of shragi

ASKER

Where on the server do you want to save the files ?  
suppose my project name is "projectX"

when I create a war file and store in webapps to run...

a folder with ProjectX is created...besides root folder...
now the user should be able to see
only projectX folder when he browse ... to save a file...
Avatar of shragi

ASKER

<%
   File appPath = new File(application.getRealPath("/"));

%>
<%=appPath%>


what does those lines actually do....in jsp page. ?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>what does those lines actually do....in jsp page. ?
>File appPath = new File(application.getRealPath("/"));
The getRealPath method returns the  returns the absolute file path on the server's filesystem  
><%=appPath%>  
This is a JSP expression. It just prints the path to the response.
Here is a JSP that might demonstrate what you want to do. Just copy and paste it.
<%@ page import="java.io.File" %>
<%
  String selectedFolder = request.getParameter("selectedFolder");
  if(selectedFolder == null || "Please make a selection".equals(selectedFolder))selectedFolder = "not selected";
%>
<html>
<body>
<form>
<select name="selectedFolder" >
<option value="Please make a selection" >Please make a selection</option>
<%
   File appFolder = new File(application.getRealPath("/"));
   File[] list = appFolder.listFiles();
   for(File f : list){
       if(f.isDirectory() && !f.getPath().endsWith("WEB-INF")){
              String name = f.getName();
              out.print("<option value=\"" + name + "\">" + name + "</option>");
       }
   }
%>
</select>
<input type="submit" />
</form>
Selected folder is <%=selectedFolder%>
</body>
</html>

Open in new window

Avatar of shragi

ASKER

the folder structure
ProjectX is my project main it has src, build, wecontent, content
similarly remaining... folders...


ProjectX: src, build, WebContent, content
WebContent: JSP, Meta-Inf
content: source, old_source

when I run this code I got a drop down list of JSP, Meta-Inf...

that awesome.. but I want to them to choose from content folder...
so instead of JSP and Meta-inf is it possible to display source, old_source

I am confused. Is the content folder in your IDE ?  What IDE are you using ?  
Look on your server. What folders are in the webapps/ProjectX folder ?  
If you can put "content" folder into ProjectX folder then you can try using the following demo JSP.
<%@ page import="java.io.File" %>
<%
  String selectedFolder = request.getParameter("selectedFolder");
  if(selectedFolder == null || "Please make a selection".equals(selectedFolder))selectedFolder = "not selected";
%>
<html>
<body>
<form>
<select name="selectedFolder" >
<option value="Please make a selection" >Please make a selection</option>
<%
   File appFolder = new File(application.getRealPath("/content"));
   File[] list = appFolder.listFiles();
   for(File f : list){
       if(f.isDirectory()){
              String name = f.getName();
              out.print("<option value=\"" + name + "\">" + name + "</option>");
       }
   }
%>
</select>
<input type="submit" />
</form>
Selected folder is <%=selectedFolder%>
</body>
</html>

Open in new window

Avatar of shragi

ASKER

I am using eclipse... I created content folder to store configuration files...

I got the below error while running the above code...
org.apache.jasper.JasperException: Exception in JSP: /jsp/LoadConfig2.jsp:62

59: 						<%
60: 						   File appFolder = new File(application.getRealPath("/Configurations"));
61: 						   File[] list = appFolder.listFiles();
62: 						   for(File f : list){
63: 						      // if(f.isDirectory() && !f.getPath().endsWith("WEB-INF")){
64: 						              String name = f.getName();
65: 						              out.print("<option value=\"" + name + "\">" + name + "</option>");


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

java.lang.NullPointerException
	org.apache.jsp.jsp.LoadConfig2_jsp._jspService(LoadConfig2_jsp.java:124)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.30 logs.

Open in new window

Avatar of shragi

ASKER

File appFolder = new File(application.getRealPath("/content"));


In  Eclipse, you should place your content folder in the WebContent folder.  If you do, then it will in your web app's root folder and
application.getRealPath("/content")  
will point to it.
Avatar of shragi

ASKER

yup got it... thanks rrz...


how can I get the location on server side... in action class..
I mean if I use

request.getAttribute("selectedFolder");

will I get the location... becoz I need to save a file with name new_file.txt at that location...

so String filename = request.getAttribute("selectedFolder") + "\\"  + "new_file.txt";

PrintWriter out1 = new PrintWriter(new FileOutputStream(filename));
            
will it work...i mean will it create new_file at the location selected



can I ask you the other question that I posted...

here we r able to select one of the folder to save our file...
suppose our files are in content folder....and in jsp folder

now if I want to load one of the files in content folder
how can I browse those files.... which are stored ...

>how can I get the location on server side... in action class..
>I mean if I use
>request.getAttribute("selectedFolder");  
I don't use Struts. Does the code automatically take request parameters and put them into request- scope objects ?
If it does, then it should work. I really don't know.  
>so String filename = request.getAttribute("selectedFolder") + "\\"  + "new_file.txt";
No that won't work.
String filename = (String)request.getAttribute("selectedFolder") + "\\"  + "new_file.txt";
or
String filename = (String)request.getAttribute("selectedFolder") + "/new_file.txt";
You don't have to use the escaped backslash.  It should work in Windows
>how can I browse those files.... which are stored ...  
If you create some links to them, then the browser can browse to them.
Avatar of shragi

ASKER

>how can I browse those files.... which are stored ...  
If you create some links to them, then the browser can browse to them.


hey rrz could you please tell me in detail how to create links...
otherwise how about listing all the files in the folders...
just like we listed out the folders jsp, meta-inf, content
list out all files and folders in content...
Here is a demo JSP. Just copy and paste it into your web app. To test you need to have a "content" folder in your web app's root folder(in WebContent in Eclipse).   In the "content" folder have some folders with some test files in each one.
<%@ page import="java.io.File" %>
<%
  String selectedFolder = request.getParameter("selectedFolder");
  if(selectedFolder == null || "Please make a selection".equals(selectedFolder))selectedFolder = "not selected";
%>
<html>
<body>
<form>
<select name="selectedFolder" >
<option value="Please make a selection" >Please make a selection</option>
<%
   File contentFolder = new File(application.getRealPath("/content"));
   File[] list = contentFolder.listFiles();
   for(File f : list){
       if(f.isDirectory()){
              String path = f.getPath();
              out.print("<option value=\"" + path + "\">" + path + "</option>");
       }
   }
   
%>
</select>
<input type="submit" />
</form>
Selected folder path is <%=selectedFolder%><br/>
<%
   String folderName = new File(selectedFolder).getName();
   String requestURL = request.getRequestURL().toString();
   String servletPath = request.getServletPath();
   String folderURL = requestURL.substring(0, requestURL.indexOf(servletPath));
   if(!"not selected".equals(selectedFolder)){
       File[] files = (new File(selectedFolder)).listFiles();
       for(File file : files){
           out.print("<a href=\"" + folderURL + "/content/" + folderName + "/" + file.getName() + "\">" + file.getName() + "</a><br/>");
       }
   }
%>
</body>
</html>

Open in new window

Oops. I should have named folderURL---> appURL   because it is the URL of your web app.
Avatar of shragi

ASKER

here i am providing the customer to choose either to save in content or jsp or meta-inf...

so some one may choose to store in jsp folder also.... so at any point of
time i should be able to browse all three folders...


one more thing.... I am not getting any output for

<select name="selectedFolder" >