
java上传图片,路径没问题,但是跳转到receivePicture.jsp页面后,到所在文件夹下却找不到图片,代码下
index.jsp:<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%Stringpat...
index.jsp:
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="/jsp_practive/receivePicture.jsp" method="post" ENCTYPE="multipart/form-data">
选择要上传的图片:<br/>
<input type="file" name="pic"/><br/>
<input type="submit" value="提交"/><br/>
</form>
</body>
</html>
receivePicture.jsp:
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'receivePicture.jsp' starting page</title>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="smart" scope="page" class="org.lxh.smart.SmartUpload"/>
<%
smart.initialize(pageContext);//上传初始化
smart.upload();//准备上传
smart.save("/savePic");//保存上传的图片的路径
%>
</body>
</html> 展开
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="/jsp_practive/receivePicture.jsp" method="post" ENCTYPE="multipart/form-data">
选择要上传的图片:<br/>
<input type="file" name="pic"/><br/>
<input type="submit" value="提交"/><br/>
</form>
</body>
</html>
receivePicture.jsp:
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'receivePicture.jsp' starting page</title>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="smart" scope="page" class="org.lxh.smart.SmartUpload"/>
<%
smart.initialize(pageContext);//上传初始化
smart.upload();//准备上传
smart.save("/savePic");//保存上传的图片的路径
%>
</body>
</html> 展开
1个回答
展开全部
smart.save("/savePic")的路径有点不对,可能在你的tomcat/bin下,最好是这样:path=this.getServletContext().getRealPath("upload")+
java.io.File.separator+
UUID.randomUUID().toString()+file.getFileName();
file.saveAs(path);
你先new 一个file ,打印看看它的绝对路径,看看行不行。
万恶的xxx 上不了jspsmartupload的官网。看不到API。
赶脚用apache 的commons-fileupload的较多吧
一下是在网上copy的,最开始学文件上传的时,用过smartupload 现在在没用了
save
作用:将全部上传文件保存到指定目录下,并返回保存的文件个数。
原型:public int save(String destPathName)
和public int save(String destPathName,int option)
其
中,destPathName为文件保存目录,option为保存选项,它有三个值,分别是SAVE_PHYSICAL,SAVE_VIRTUAL和
SAVE_AUTO。(同File类的saveAs方法的选项之值类似)SAVE_PHYSICAL指示组件将文件保存到以操作系统根目录为文件根目录的
目录下,SAVE_VIRTUAL指示组件将文件保存到以Web应用程序根目录为文件根目录的目录下,而SAVE_AUTO则表示由组件自动选择。
注:save(destPathName)作用等同于save(destPathName,SAVE_AUTO)。
java.io.File.separator+
UUID.randomUUID().toString()+file.getFileName();
file.saveAs(path);
你先new 一个file ,打印看看它的绝对路径,看看行不行。
万恶的xxx 上不了jspsmartupload的官网。看不到API。
赶脚用apache 的commons-fileupload的较多吧
一下是在网上copy的,最开始学文件上传的时,用过smartupload 现在在没用了
save
作用:将全部上传文件保存到指定目录下,并返回保存的文件个数。
原型:public int save(String destPathName)
和public int save(String destPathName,int option)
其
中,destPathName为文件保存目录,option为保存选项,它有三个值,分别是SAVE_PHYSICAL,SAVE_VIRTUAL和
SAVE_AUTO。(同File类的saveAs方法的选项之值类似)SAVE_PHYSICAL指示组件将文件保存到以操作系统根目录为文件根目录的
目录下,SAVE_VIRTUAL指示组件将文件保存到以Web应用程序根目录为文件根目录的目录下,而SAVE_AUTO则表示由组件自动选择。
注:save(destPathName)作用等同于save(destPathName,SAVE_AUTO)。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询