We help IT Professionals succeed at work.

servlet string local drive

aaronyeo22
aaronyeo22 asked
on
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


Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
Hard to understand what you mean - can you please explain a little bit more clearly?

Author

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.
Top Expert 2016

Commented:
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

Author

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
Top Expert 2016

Commented:
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

Author

Commented:
if i discard the "file" how do i browse it. sorry.
Top Expert 2016

Commented:
>>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

Author

Commented:
no, i just want to let user browsing like txt or pdf through the servlet.
Top Expert 2016

Commented:
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

Author

Commented:
ya. i wanna to create web app to let user browsing the pdf files. please help.
Top Expert 2016

Commented:
http://www.icepdf.org/demos.html

Look at the 'Servlet Demo'

Author

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
Top Expert 2016

Commented:
>>i just want to know the full path of outside web container.

I don't quite understand. Please try to explain more clearly

Author

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
Top Expert 2016

Commented:
You'd simply call the above servlet from your jsp

Author

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.
Top Expert 2016
Commented:
>>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

Author

Commented:
no time to look at.
Top Expert 2016

Commented:
:)