我的jsp页面中有两个字段:一个<s:file>
标签和一个<s:textfield>
标签。
我试图在该文本字段中显示所选文件的本地路径。
但我找不到检索本地选定文件路径的确切解决方案。
上传.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(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;
}
}