servlet string local drive

how do i use servlet to string local drive c:
like i have one abc.txt located in c:/a/abc.txt;

i want to print out the result of full directory of abc.txt

thanks


aaronyeo22Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
Hard to understand what you mean - can you please explain a little bit more clearly?
0
aaronyeo22Author Commented:
i want use servlet to string abc.txt directory. if i stored my abc.txt in my drive c:/a/abc.txt.

how do i get the abc.txt classpath.
0
CEHJCommented:
Access the file by full path
File f = new File("c:/a/abc.txt");

// Maybe

InputStream in = new FileInputStream("c:/a/abc.txt");

Open in new window

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

aaronyeo22Author Commented:
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="NewServlet" method="get">
            <input type="file" name="aaa"/>
            <input type="submit"/>
        </form>
    </body>
</html>



 public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {

                String aaa;
                aaa = req.getParameter("aaa");

               System.out.println(aaa);
      }

after i browse the abc.txt the result return that's only a file name. I want the full directory of this particular file. like c:/a/abc.txt

sorry
0
CEHJCommented:
The input tag of type 'file' doesn't work like that. Since the path of the file that's uploaded has no relevance on the server, it's discarded, leaving only the name
0
aaronyeo22Author Commented:
if i discard the "file" how do i browse it. sorry.
0
CEHJCommented:
>>if i discard the "file" how do i browse it. sorry.

The 'browsing' (for the file) happens on the client. What happens at the server is the upload is received
0
aaronyeo22Author Commented:
no, i just want to let user browsing like txt or pdf through the servlet.
0
CEHJCommented:
You need to understand what happens with a browser and servlet. If there is an input element of type file, the purpose is to upload a file to the servlet.

Why would a user use a server to browse a file that's on the local machine anyway?

The only possible reason i can think of for doing that is if the user hasn't got an app to view the file uploaded. Then the server (somehow) could display the content, having interpreted it
0
aaronyeo22Author Commented:
ya. i wanna to create web app to let user browsing the pdf files. please help.
0
CEHJCommented:
http://www.icepdf.org/demos.html

Look at the 'Servlet Demo'
0
aaronyeo22Author Commented:
sorry. that's not suit for me. can please let me know how to do it in jsp servlet. i just want to know the full path of outside web container. that's it. thanks
0
CEHJCommented:
>>i just want to know the full path of outside web container.

I don't quite understand. Please try to explain more clearly
0
aaronyeo22Author Commented:
really sorry.

i just want a jsp have a browse button and let user to browse any file like pdf and my servlet able to read any pdf file browse by user. thats it.

thanks
0
CEHJCommented:
You'd simply call the above servlet from your jsp
0
aaronyeo22Author Commented:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

      private static final long serialVersionUID = 1L;

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            performTask(request, response);
      }

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                  IOException {
            performTask(request, response);
      }

      private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                  IOException {

            String pdfFileName = "pdf-test.pdf";
            String contextPath = getServletContext().getRealPath(File.separator);
            File pdfFile = new File(contextPath + pdfFileName);

            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
            response.setContentLength((int) pdfFile.length());

            FileInputStream fileInputStream = new FileInputStream(pdfFile);
            OutputStream responseOutputStream = response.getOutputStream();
            int bytes;
            while ((bytes = fileInputStream.read()) != -1) {
                  responseOutputStream.write(bytes);
            }

      }

}


like the above code. how do i let the users browse any pdf  file to open it.
0
CEHJCommented:
>>like the above code. how do i let the users browse any pdf  file to open it.

That code does something *completely* different. It sends a pdf file from the server to the client. (And it's not a jsp)

I've already answered your question above
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aaronyeo22Author Commented:
no time to look at.
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.