tijuvj
tijuvj

Reputation: 83

Java Web Application:how to get the original path of selected file?

There are two fields in my jsp page: an <s:file> tag and an <s:textfield> tag.

I tried to display the local path of the selected file, in that text field.

But i can't find an exact solution for retrieving the local selected file's path.

Upload.jsp

<body>
    <s:form action="select" method="post" theme="simple" name="f1" enctype="multipart/form-data">
        <s:file name="upload" id="ff" size="40" onchange="javascript:submitForm();"/><br>
        <s:label value="file path"/>
        <s:textfield name="uploadPath" id="path"/><br>
    </s:form>

    <script type="text/javascript">
        function submitForm(){
            var filepath=document.f1.upload.value;
            alert(filepath);
            document.forms[0].submit();
        }
    </script>

</body>

SelectAction.java (method selectForder() will be called when the form submitted)

 import com.opensymphony.xwork2.ActionSupport;
 import java.io.File;
 import javax.servlet.http.HttpServletRequest;
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.interceptor.ServletRequestAware;
 public class SelectAction extends ActionSupport implements ServletRequestAware{

  private File upload;
  private String uploadPath;
  private String uploadFileName; 
  private HttpServletRequest servletRequest;


  public void setUpload(File upload) {
    this.upload = upload;
  }

  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }

  public String getUploadPath() {
    return uploadPath;
  }

  public void setUploadPath(String uploadPath) {
    this.uploadPath = uploadPath;
  }

  public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest=servletRequest;
  }

  public String selectFolder(){

    //I can't get the local path of the selected file with below codes

    System.out.println("1 "+uploadFileName);
    System.out.println("2 "+upload.getAbsoluteFile().getPath());
    System.out.println("3 "+upload.getAbsolutePath());
    uploadPath=uploadFileName;


    try{
    System.out.println("4 "+servletRequest.getRealPath(upload.getPath()));
    System.out.println("5 "+servletRequest.getRealPath(uploadFileName));
    System.out.println("6   "+servletRequest.getSession().getServletContext().getRealPath("/"));

    System.out.println("7 "+ServletActionContext.getServletContext().getRealPath("/files"));


    System.out.println("8 "+SelectAction.class.getProtectionDomain().getCodeSource());


        }
       catch(Exception e)
      {
        System.out.println(e);
        }
       return SUCCESS;
      }

 }

Upvotes: 1

Views: 4335

Answers (3)

Myo
Myo

Reputation: 72

You can use ContextPath in your servlet.

Upvotes: 0

Deepak N
Deepak N

Reputation: 21

If you still need original path, you can use Internet Explorer.

IE 8 (because i have tested this on IE 8), by default sends the full path of the file to the application.

Upvotes: 0

Ganesh Rengarajan
Ganesh Rengarajan

Reputation: 2006

This is not possible for security reasons, sorry.

You'll find plenty of discussion about this on StackOverflow and elsewhere (sometimes in reference to the 'fakepath' symptom of some browser's implementation of this security feature.

Without going into too much detail, the reasoning for this security feature is quite simple; no website should need to (read: be able to) find out anything about a user's file system. As such, when a file is uploaded to a server only the file data should be sent.

Technically, it was possible in some older browsers (eg: IE6 I believe) but is a pretty ubiquitous security feature at this point and you would be ill fated to depend on this information.

Upvotes: 1

Related Questions