简体   繁体   English

Java Web Application:如何获取所选文件的原始路径?

[英]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. 我的jsp页面中有两个字段: <s:file>标记和<s:textfield>标记。

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 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) SelectAction.java(提交表单时将调用方法selectForder()

 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;
      }

 }

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. 您会在StackOverflow和其他地方找到很多有关此问题的讨论(有时参考某些浏览器实施此安全功能的“假路径”症状)。

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. 从技术上讲,这在某些较旧的浏览器中是有可能的(例如,我相信是IE6),但是这是目前相当普遍的安全功能,并且您不该依赖于此信息。

If you still need original path, you can use Internet Explorer. 如果仍然需要原始路径,则可以使用Internet Explorer。

IE 8 (because i have tested this on IE 8), by default sends the full path of the file to the application. IE 8 (因为我已经在IE 8上对其进行了测试),默认情况下会将文件的完整路径发送到应用程序。

您可以在servlet中使用ContextPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM