Solved

browse folder

Posted on 2011-02-14
24
3,198 Views
Last Modified: 2012-05-11
<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)
0
Comment
Question by:shragi
  • 12
  • 12
24 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 34890093
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 ?
0
 

Author Comment

by:shragi
ID: 34890124
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....
0
 

Author Comment

by:shragi
ID: 34890131
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...

0
 
LVL 27

Expert Comment

by:rrz
ID: 34890334
How does this question relate to your other question at
http://www.experts-exchange.com/Programming/Languages/Java/Q_26795736.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.
0
 

Author Comment

by:shragi
ID: 34890574
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....
0
 
LVL 27

Expert Comment

by:rrz
ID: 34890711
>is it possible to browse server directories from my workspace....  
What is  "my workspace.... "  ?
0
 

Author Comment

by:shragi
ID: 34890753
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...


0
 
LVL 27

Expert Comment

by:rrz
ID: 34890815
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 ?
0
 

Author Comment

by:shragi
ID: 34890978
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...
0
 

Author Comment

by:shragi
ID: 34891102
<%
   File appPath = new File(application.getRealPath("/"));

%>
<%=appPath%>


what does those lines actually do....in jsp page. ?
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 34891205
Try this JSP to get started.
<%@ page import="java.io.File" %>
<html>
<body>
<%
   File appPath = new File(application.getRealPath("/"));
   File[] list = appPath.listFiles();
   for(File f : list){
       if(f.isDirectory() && !f.getPath().endsWith("WEB-INF"))out.print(f.getPath() + "<br/>");
   }
%>
</body>
</html>

Open in new window

But don't you have any folder names in mind ? Do you have any further restrictions other than not listing  WEB-INF  ?
0
 
LVL 27

Expert Comment

by:rrz
ID: 34891244
>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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:rrz
ID: 34891377
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

0
 

Author Comment

by:shragi
ID: 34891538
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

0
 
LVL 27

Expert Comment

by:rrz
ID: 34891736
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

0
 

Author Comment

by:shragi
ID: 34891879
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

0
 

Author Comment

by:shragi
ID: 34891888
File appFolder = new File(application.getRealPath("/content"));


0
 
LVL 27

Expert Comment

by:rrz
ID: 34892265
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.
0
 

Author Comment

by:shragi
ID: 34892426
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 ...

0
 
LVL 27

Expert Comment

by:rrz
ID: 34892661
>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.
0
 

Author Comment

by:shragi
ID: 34892840
>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...
0
 
LVL 27

Expert Comment

by:rrz
ID: 34893119
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

0
 
LVL 27

Expert Comment

by:rrz
ID: 34893144
Oops. I should have named folderURL---> appURL   because it is the URL of your web app.
0
 

Author Comment

by:shragi
ID: 34894065
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" >

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
IT Company 5 69
strCopies  challenge 17 73
eclipse package explorer vs project explorer view 2 29
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now