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

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


0
aaronyeo22
Asked:
aaronyeo22
  • 9
  • 9
1 Solution
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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
 
aaronyeo22Author Commented:
no time to look at.
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now