?
Solved

Servlets with binary data

Posted on 2003-03-13
13
Medium Priority
?
266 Views
Last Modified: 2013-11-24
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
Comment
Question by:tpass001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8132389
I think you'd have to do this using a signed applet in some way.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 252 total points
ID: 8132792
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8133892
>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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
LVL 3

Expert Comment

by:msterjev
ID: 8134644
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135366
tpass001 please confirm or refute the fact the file listing is of files in an arbitrary directory on the *client* machine

0
 
LVL 3

Expert Comment

by:msterjev
ID: 8135409
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
 

Author Comment

by:tpass001
ID: 8137166
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8137751
>>
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
 

Author Comment

by:tpass001
ID: 8138273
>>
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
 
LVL 2

Assisted Solution

by:functionpointer
functionpointer earned 248 total points
ID: 8140660
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
 

Expert Comment

by:CleanupPing
ID: 9058705
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
 
LVL 35

Expert Comment

by:girionis
ID: 9762777
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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

765 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