0

Please have a look at he following code

JSP

<%-- 
    Document   : index
    Created on : Nov 27, 2012, 1:11:48 PM
    Author     : Yohan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <div>
  <div>Content for New Div Tag Goes Here</div>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
<p>&nbsp;</p>
  <div>
      <form method="post" action="FileSelector" enctype="multipart/form-data">
      Select File: <input type="file" name="location"/></div>
        <br>
        <input type="submit" value="Submit"/>
</form>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>


</div>
    </body>
</html>

Servlet

package importWizard;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

public class FileSelector extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
    {
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
    {
        PrintWriter pw = response.getWriter();

        File location = (File)request.getParameter("location");


        pw.write(location);
    }
}

As you can see, I am unable to send the file from JSP to Servlet. I don't need to send the file, but at least the complete location of the file (It is only sending the file name). How can I send the file or the complete location of the file from JSP to servlet?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – Rakesh Patel Nov 27 '12 at 09:42
  • What exactly is the purpose of knowing the absolute client side file location? What are you planning to do with this information which is in real world applications usually completely useless as the server can't access the client's disk file system directly anyway. – BalusC Nov 27 '12 at 15:11
  • He probably want to keep a track of the source file, we see often the complete file path inside footer of Word documents for example. Maybe if we can know the final purpose we will be able to give better advices. – Alexandre Lavoie Nov 27 '12 at 17:22
  • @BalusC: We shifted this to a Desktop program so this issue is solved. But I am still interested in this issue. What we need to do is, read the txt,csv,xml and Excel files from the user, load them to array lists, display data, ask the user for delimiter, and create tables according to the data. In other words this is a KPI. However I asked lot of questions about this and found out using java web for this is a very hard task to fit all the requirements, so we are now in desktop app, as we planned at the first time, and that's the technology the whole team is capable of rather than the web – PeakGen Nov 28 '12 at 03:56
  • It's hard in web because the client has to send the file's contents itself instead of that the server has to grab the file's contents itself based on a path provided by the client (if that were possible, it would have been a huge security hole). Indeed, a client side application (or desktop program as you call it yourself) would be capable of this. If you would still insist in having a web application, a (signed) applet or webstart application would be one of the solutions you've had in mind. – BalusC Nov 28 '12 at 11:30
  • Yes, we are thinking about web start. I might post a question regarding web start JDBC as well :) Thanks for the reply @BalusC – PeakGen Nov 28 '12 at 12:21
  • @BalusC: What about providing your final comment as an answer? – PeakGen Nov 28 '12 at 16:25

1 Answers1

2

I don't need to send the file, but at least the complete location of the file (It is only sending the file name). How can I send the file or the complete location of the file from JSP to servlet?

That's not possible using standard HTML <input type="file"> element. It sends merely the entire file contents along with the filename as that's basically the only way for the server to get the file contents. The server has namely no direct access to the client's local disk file system, so the client's absolute disk file system path as sole information would have been useless. Note that the MSIE browser will due to a security bug send the full absolute client side disk file system path along instead of only the filename, but that's thus not how things are supposed to work.

If you really need to have only the client's absolute disk file system path, then your best bet is to create a (signed) applet or webstart application which obtains it by JFileChooser and finally embed it in the web page.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555