日期:2014-05-17  浏览次数:20670 次

好吧!直接问问题!input标签怎么得到图片绝对路径!

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>tupian</title>
</head>
<body>
<form name="upform" action="index.jsp" method="get">
<input type="file" name="file1" />
<br />
<input type="submit" value="Submit" />
<br />
<input type="reset" />
</form>

<%
if (request.getParameter("file1") != null) {
String url=String.valueOf(request.getParameter("file1"));
String[] ss = url.split(".");
if (1==1) {
out.println("<img src='" + request.getParameter("file1")
+ "'/>");
} else {
out.print("你选择的不是图片格式");
}
}
%>
</body>
</html>


点击提交的时候得到的是文件名,我去,怎么得到文件绝对路径!求了!
jsp input string file html

------解决方案--------------------
加上basePath就是绝对路径。
------解决方案--------------------
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>


额。。你这段代码是做什么用的。。输出下path,basePath就知道了
------解决方案--------------------
out.println("<img src='<%=basePath%>" + request.getParameter("file1")
                            + "'/>")
------解决方案--------------------
out.println("<img src='"<%=basePath%> + request.getParameter("file1")
                            + "'/>")
------解决方案--------------------
这为什么不用javascript呢?我document.getElementById("image").value明明就是绝对路径啊
------解决方案--------------------
直接拖不就好了
------解决方案--------------------
LZ要的是图片在客户端的本地路径,比如,你选择了一张C:\abc\xyz.jpg,要的就是C:\abc\xyz.jpg?

如果是这个,答案是基本不可能。你只能尝试JS读取file控件的value属性。

版本越高的浏览器,越可能返回一个半截的或者完全虚拟的路径。因为这个涉及到浏览器的安全性。另一个与此有关的现象是,你无法对file控件的value进行写操作。
---------------------------------
如果你要取的是服务器上面的路径http://....
话说,人家上传了,你保存的代码呢???!!!