图片上传到服务器,数据库记录路径<%@ page contentType="text/html; charset=gb2312" language="java" import="com.jspsmart.upload.*,java.sql.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<%
try{mySmartUpload.initialize(pageContext);String path="no photo";
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()){
path="/news/newsimages/"+myFile.getFileName();
myFile.saveAs(path);}
String cmd="insert into news(content,topic,dateandtime,image,hits) values('";int j=1;
java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();
while (e.hasMoreElements()) { String key = (String)e.nextElement();
String[] values = mySmartUpload.getRequest().getParameterValues(key);
for(int i = 0; i < values.length; i++) {
if(j!=1) cmd+=values[i]+"','";
}
j++;
}
java.util.Date d=new java.util.Date();
java.text.SimpleDateFormat dformat=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateandtime=dformat.format(d);
cmd+=dateandtime+"','"+path+"',0)"; //inert into news
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433","sa","sa");
DatabaseMetaData conMD = con.getMetaData();
con.setCatalog("bbs");
Statement stmt = con.createStatement();
stmt.executeUpdate(cmd);
stmt.close();
con.close();
out.println("<A href='../index.jsp'>返回首页</a>");
}
catch(Exception e)
{out.println(e);
}
%>
</body>
</html>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<%
try{mySmartUpload.initialize(pageContext);String path="no photo";
mySmartUpload.upload();
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()){
path="/news/newsimages/"+myFile.getFileName();
myFile.saveAs(path);}
String cmd="insert into news(content,topic,dateandtime,image,hits) values('";int j=1;
java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();
while (e.hasMoreElements()) { String key = (String)e.nextElement();
String[] values = mySmartUpload.getRequest().getParameterValues(key);
for(int i = 0; i < values.length; i++) {
if(j!=1) cmd+=values[i]+"','";
}
j++;
}
java.util.Date d=new java.util.Date();
java.text.SimpleDateFormat dformat=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateandtime=dformat.format(d);
cmd+=dateandtime+"','"+path+"',0)"; //inert into news
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433","sa","sa");
DatabaseMetaData conMD = con.getMetaData();
con.setCatalog("bbs");
Statement stmt = con.createStatement();
stmt.executeUpdate(cmd);
stmt.close();
con.close();
out.println("<A href='../index.jsp'>返回首页</a>");
}
catch(Exception e)
{out.println(e);
}
%>
</body>
</html>
解决方案 »
- DWZ分页问题
- 急哦!!Struts中Ibatis框架问题,我写了一个UserDao接口和一个UserDaoImp实现方法,现在dao_config.xml总是报错,而且很奇怪啊?请各位指点啊?
- jsp 中如何取得客户端真实ip地址
- jsp中怎么判断密码中不能含有空格,帐号只能由字母、数字、汉字、下划线“_”组成呢
- 关于输出流的问题
- 这个应该怎么写
- 当can Grow(maximum number of lines 设为0)水晶报表所有字段怎么使它在运行时高度一致
- Jsp网络在线考试系统源码分享
- 传递参数时可以选择参数吗?
- 在取出时能不能用 rs.getDate()? 还是用rs.getString()?
- 页面初始化时间问题,高手请进,高分相送,在线等
- 有没有人用过Jive?Tomcat总提示说:java.lang.ClassNotFoundException: Unable to load
<form action="upload.jsp" method=post>
<input type=file>
<input type=submit>
</form>//upload.jsp
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
mySmartUpload.save("/");
%>
文件就上传到了tomcat的root目录下(假设你没改动服务器配制)。
com.oreilly.servlet.MultipartRequest mr = new com.oreilly.servlet.MultipartRequest(request,filedir,5*1024*1024,"GB2312");
一句话搞定。
jsp源代码如下:
<HTML>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>上传文件</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF" text="#000000">
<p> </p>
<form name="form" enctype="multipart/form-data" method="post" action="/court/UploadFiles" onSubmit = "return onBeforeSubmit()">
<table class="normal" width="456" border="0" cellspacing="1" cellpadding="0" align="center" bordercolor="#0099CC" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#6699CC" colspan="2" nowrap><font class="normal">请选择上传文件</font></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="32" width="67" nowrap>
<div align="center"><font class="normal">文件描述</font></div>
</td>
<td height="32" width="386" nowrap>
<input class='normal' type="text" name="WJMS" maxlength="200" size="40">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="32" width="67" nowrap>
<div align="center"><font class="normal">文号</font></div>
</td>
<td height="32" width="386" nowrap>
<input class='normal' type="text" name="WH" maxlength="200" size="20">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="26" width="67" nowrap>
<div align="center"><font class="normal">上传文件</font></div>
</td>
<td height="26" width="386" nowrap>
<input class='normal' type="file" name="FILE" size="20" maxlength="200">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="29" colspan="2">
<div align="center">
<input class='normal' name = "rs_add" type = "image" src="/court/renshi/images/rs_scwj.gif" >
</div>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
免费的cos.jar加到classpath里面就可以了。
还有特别注意的是<form name="form" |||enctype="multipart/form-data" |||