• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Servlets with binary data

This is more of a three part question

What I want to do is
1. Show a form to the user through a browser where they would type in the drive and a fully addressable directory for eg: d:\programs\folder1 or /usr/local/folder1.
2. Use servlets to go into this directory. Get the listing of all the files in the directory, and then display this list on the browser.
3. This list should be a hot list to those files and on clicking the files should present them the file. Thus the mime type should be properly determined.

This address could be on a shared drive or fixed drive or in Linux's case a linked drive through Samba.

I am using Tomcat and have a moderate amount of knowledge on servlets.

Thanks for the replies in advance to a very ambiguous question.

TP.
0
tpass001
Asked:
tpass001
  • 3
  • 2
  • 2
  • +4
2 Solutions
 
CEHJCommented:
I think you'd have to do this using a signed applet in some way.
0
 
TimYatesCommented:
If you mean that the files are on the LOCAL directory, then:

1) This either has to be a html "FILE" type of input, or a signed applet, an unsigned applet cannot see your local disk

2) This is impossible, the servlet would have to have the file-list uploaded to it (or just use the signed applet to browse this directory and show it to the user via a JFileChooser?)

3) Errr....I think you must mean the directory from (1) is on the server, as otherwise this makes no sense ;-)

so...

Files on Remote server:

Why not just create a symbolic link from inside the webapps directory of tomcat to a specified directory?  Turn on Folder Browsing, and you're away...

Or install ftp, and use that...

Seems to be what you are trying to do...

Or maybe CVS with the web frontend?
0
 
functionpointerCommented:
>1. 2. & 3.

This is simple enough. Use a form to post the input from the user as a form parameter (I think it make alot more sense to at least show the user the drives they have available ). The doPost method looks for the parameter, gets a list of the files in the requested directory. Then, read the directory, make your links like "<a href="/file.ext"> and send the page to the client. Finally, store the requested directory as a String in the client session, something like: request.getSession(true).setAttribute( "directory", requestedDirectory );  

Clicking on the link will generate an HTTP GET, so have your doGet method use request.getPathInfo() and parse off the file name and ext.  Set the response Content-Type with an internal mapping of extensions:mime-types. Pull the directory from the session using request.getSession(), put the file name on it, FileInputStream the file, write it to the ServletOutputStream.

Easy as pi ;)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
msterjevCommented:
The problem of design pattern for downloading and representing the file browsing capabilities is not a problem(HTML front end). Determining the MimeType is a problem. You can't determine it programatically.The solution is to fill the hashtable with the known file extensions mapped to the properly chosen MimeType. If the requested file extension is not into the hashtable you can simple choose the MimeType: application/octet-stream. At this moment I have  the ASP version of the similar scenario. The ideas are the same and I can post them if you are interested!
0
 
CEHJCommented:
tpass001 please confirm or refute the fact the file listing is of files in an arbitrary directory on the *client* machine

0
 
msterjevCommented:
CEHJ read:

2. Use servlets to go into this directory. Get the listing of all the files in the directory, and then display this list on the browser.
0
 
tpass001Author Commented:
Thanks all for your responses. The file listing has to come from the remote server.

If I had to get the files from the client, I guess webdav could be a solution.

TimYates:
Files on Remote server:

Why not just create a symbolic link from inside the webapps directory of tomcat to a specified directory?  Turn on Folder Browsing, and you're away...

*** this sounds good, but do u have an example of how to do this. I am assuming this would be either in the web.xml of the webapp or server.xml. Would this solution be app server independant. In this solution would I get the file listing as the one you see on the jakarta sites when u download files. That is definately unsightly.



0
 
CEHJCommented:
>>
Show a form to the user through a browser where they would type in the drive and a fully addressable directory for eg: d:\programs\folder1 or /usr/local/folder1.
>>

How would a client know anything about the disk drives on a remote machine unless this structure is displayed first?
0
 
tpass001Author Commented:
>>
Show a form to the user through a browser where they would type in the drive and a fully addressable directory for eg: d:\programs\folder1 or /usr/local/folder1.
>>

How would a client know anything about the disk drives on a remote machine unless this structure is displayed first?

I would provide a list of files. This is not a complete application, part of a framework, so once I get this working, I can integrate it.

I tried this:

/////////////////////////
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Servlet1</title></head>");
    out.println("<body bgcolor=\"#ffffff\">");
    out.println("<p>The servlet has received a GET. This is the reply.</p>");

    File file = new File("C:\\temp");
    File[] files = file.listFiles();
    for (int i = 0; i < files.length; i++) {
      out.println("<a href=\"C:\\temp\\"+files[i].getName()+"\">"+files[i].getName() + " - " );
      if (files[i].isDirectory()) {
        out.println("Directory<br/>");
      }
      else {
        out.println("File<br/>");
      }
    }
    out.println("</body></html>");

  }
///////////////////////////
This is not working becasue the hrefs goto c:\temp rather than http://localhost:8080/etcc.....

Can you help me out by telling me how I could achieve that.

Thanks.

TP
0
 
functionpointerCommented:
just list all your available file directories in a properties file, put it in your WEB-INF and read it when you init the servlet.

directory.1=C:\myFiles
directory.2=X:\etc\init.d
directory.3=K:\WINDOWS

like i said, you have to abstract you files from your directories.
let the user pick a directory.
PUT THIS IN THE SESSION
display the files.
fetch the file based on the directory you set in the session.

your alternative is to fully qualify the links AS YOUR SERVER sees them, not as the client sees them. so when the client sends a GET to you with a parameter like "<input type='hidden' name='dir' value='K:\directory\'>" you actually get something you can use.

String dirStr= request.getParameter( "dir" );
String fileStr = request.getParameter( "file" );
File file = new File( dir + File.seperator + fileStr );

Is there some reason why you don't want to do this???

>>out.println("<a href=\"C:\\temp\\"+files[i].getName()+"\">"+files[i].getName() + " - " );
This is going to reference a file on the CLIENT machine.

if you insist on using a anchor tag, instead of a onClick="form.submit();", then encode the url to point to YOUR server.  Get the contextual path from the ServletContext and write the url properly into something you can work with.
0
 
CleanupPingCommented:
tpass001:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between TimYates and functionpointer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now