jspsmartupload上传图片成功,但是图片显示不出来,只显示一个小图标
upload.jsp<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%
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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script language="javascript">
function check()
{
if(document.formAdd.fujian.value=="")
{
alert("请选择文件");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="upload_re.jsp?Result=<%=request.getParameter("Result")%>" name="formAdd" method="post" enctype="multipart/form-data">
<input type="file" name="fujian" id="fujian" onKeyDown="javascript:alert('此信息不能手动输入');return false;" />
<input type="submit" value="提交" onClick="return check()"/>
</form>
</body>
</html>
upload_re.jsp
page contentType="text/html;charset=gb2312" language="java" import="com.jspsmart.upload.*"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="java.util.*"%>
<%
String path = request.getContextPath();
%>
<%
String newFile1Name=null;
String file_name=null;
SmartUpload mySmartUpload = new SmartUpload();
//初始化上传
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try
{
mySmartUpload.setAllowedFilesList("jpg,Jpg,JPG,GIF,gif,Gif,png");
mySmartUpload.upload();
}
catch (Exception e)
{
out.println("<script language=javascript>alert('上传格式错误!'); history.back(-1);</script>");
return;
}
try
{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (myFile.isMissing())
{
out.println("<script language=javascript>alert('必须选择图片!'); history.back(-1);</script>");
return;
}
else
{
int file_size = myFile.getSize(); //取得文件的大小 (单位是b)
file_name=myFile.getFileName();
System.out.println("文件大小:"+file_size+"文件名称:"+file_name);
//if (file_size > 10*1024*1024)
//{
//out.println("<script language=javascript>alert('上传图片大小应控制在10K~1M之间!'); history.back(-1);</script>");
//return;
//}
//else
//{
newFile1Name=new Date().getTime()+file_name.substring(file_name.indexOf("."));
System.out.println("新文件名称:"+newFile1Name);
String saveurl = request.getSession().getServletContext().getRealPath("upload");
saveurl = saveurl+"/"+newFile1Name;
myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);
// }
}
}
catch (Exception e)
{
e.toString();
}
%>
<script language="javascript">
var str=location.toString()
var Result=((((str.split('?'))[1]).split('='))[1]);
//window.opener.Form1(Result).focus();
window.parent.document.getElementById(Result).value="upload/<%= newFile1Name%>";
//window.opener=null;
document.write("上传成功");
//window.close();
</script>
显示图片的jsp页面
<td width='90'><img src="E:/workspace/qiche/<%=tupian%>" width=50 height=80 /></td>
用的是相对路劲
--------------------编程问答--------------------
<img src="E:/workspace/qiche/<%=tupian%>" width=50 height=80 />
改
<img src="http://127.0.0.1:端口号/工程名/qiche/<%=tupian%>" width=50 height=80 />
必须构造出一个 HTTP请求可以访问的 URL ,你这样写路径肯定不对,但是后台处理是可以这样写的。 --------------------编程问答-------------------- 图片存放在工程路径目录下,不要用绝对路径,不安全。
补充:Java , Web 开发