插入图片到SQL数据库,保存图片路径,没有反应
相关代码:
from提交
<form action="/newsSystem/admin/MaAddnews.jsp" method="post" name="from">
<table width="617" height="450" border="0" align="center">
<tr>
<td width="105" height="28"><div align="center">标 题:</div></td>
<td width="496"><div align="left"><input type="text" name="title" width="38%"/>
<font color="red" size="4">(1-20 个汉字)</font>
</div></td>
</tr>
<tr>
<td height="27"><div align="center">类 型:</div></td>
<td><div align="left">
<select name="select">
<%
String sql="select type_id,new_type from type";
Connection conn = DB.getConn();
Statement stmt = DB.stmt(conn);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
%><option value="<%=rs.getInt("type_id")%>"><%=rs.getString("new_type")%></option>
<%} %>
</select>
</div></td>
<% rs.close();
stmt.close();
conn.close(); %>
</tr>
<tr>
<td height="31"><div align="center">内 容:</div></td>
<td><div align="left"><textarea name="text" rows="20" cols="60"></textarea></div></td>
<tr><td height="24" align="center">图 片:</td><td><input type="file" name="pictureid" accept="image"></td>
</tr>
<tr>
<td height="24"><div align="center">作 者:</div></td>
<td><div align="left"><input type="text" name=author ></div></td>
</tr>
<tr><td height="24" align="center">发 表:</td><td><select name="statu"><option value="是">是</option><option value="否">否</option></select></td>
</tr>
<tr>
<td height="24"><input type="submit" value="提交"/ onclick="return check()"> </td>
<td><input type="reset"/></td>
</tr>
</table>
</form>
处理页面:
<%@ page language="java" import="java.util.*,com.jspsmart.upload.*,java.util.Date" pageEncoding="gb2312"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.awt.Color,news.Tool.*"%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head>
<body>
<%
int count = 0;
SmartUpload mySmartUpload = new SmartUpload();
//PageContext pageContext = null;
mySmartUpload.initialize(pageContext);//初始化
// 设定上传限制
// 1.限制每个上传文件的最大长度。
mySmartUpload.setMaxFileSize(1024 * 1024 * 20);
// 2.限制总上传数据的长度。
mySmartUpload.setTotalMaxFileSize(1024 * 1024 * 100);
// 3.设定允许上传的文件(通过扩展名限制)。
mySmartUpload.setAllowedFilesList("JPG,jpg,JPEG,jpeg,GIF,gif,flv,FLV,avi,AVI,bmp,BMP");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
mySmartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
mySmartUpload.upload();//上传文件
//count = mySmartUpload.save("D:/WORKSPACE/NewsSystem/WebRoot/image/");//用来设置上传文件的目的地
System.out.println("----------" + count);(页面指打印出i的值,下面的没有执行,也没有报错!)
for (int i = 0; i<mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
// 重新命名文件名,取得当前上传时间的毫秒数值
String ext = myFile.getFileExt(); //取得文件扩展名
Calendar calendar = Calendar.getInstance();//取得毫秒数
String filename = String.valueOf(calendar.getTimeInMillis());//把毫秒数转换成字符串
System.out.println(filename);
//String filename1 = filename + "." + ext;//重命名(以毫秒的形式命名)
System.out.println(filename);
String fileName=myFile.getFileName();//取得源文件名
fileName=filename+fileName;//再次重命名(以毫秒+源文件名来命名)
System.out.println("--------"+fileName);
myFile.saveAs("E:/program files/workspace/newsSystem/WebRoot/images/" + fileName);
String path = "E:/program files/workspace/newsSystem/WebRoot/images/"+ fileName;
request.setCharacterEncoding("gb2312");
String sql = "insert into news (newsTitle,newsType,newsContent,PhotoFile,newsDatetime,statu,author) values(?,?,?,?,?,?,?)";
String title = request.getParameter("title");
int type = Integer.parseInt(request.getParameter("select"));
String text = request.getParameter("text");
String statu = request.getParameter("statu");
String author=request.getParameter("author");
String Ntime =Time.changeTime(new Date());
String vPhoto = path;
if(fileName.equals("") || fileName == null)
{
vPhoto = "";
}
Connection conn = DB.getConn();
PreparedStatement ps = null;
ps = DB.prepare(conn, sql);
ps.setString(1, title);
ps.setInt(2,type);
ps.setString(3, text);
ps.setString(4, vPhoto);
ps.setString(5, Ntime);
ps.setString(6, statu);
ps.setString(7, author);
ps.executeUpdate();
DB.close(ps);
DB.close(conn);
response.sendRedirect("/newsSystem/admin/addNews.jsp");
}
else{
String sql = "insert into news (newsTitle,newsType,newsContent,PhotoFile,newsDatetime,statu,author) values(?,?,?,?,?,?,?)";
String title = request.getParameter("title");
int type = Integer.parseInt(request.getParameter("select"));
String text = request.getParameter("text");
String statu = request.getParameter("statu");
String author=request.getParameter("author");
String Ntime =Time.changeTime(new Date());
String vPhoto = "";
Connection conn = DB.getConn();
PreparedStatement ps = null;
ps = DB.prepare(conn, sql);
ps.setString(1, title);
ps.setInt(2,type);
ps.setString(3, text);
ps.setString(4, vPhoto);
ps.setString(5, Ntime);
ps.setString(6, statu);
ps.setString(7, author);
ps.executeUpdate();
DB.close(ps);
DB.close(conn);
response.sendRedirect("/newsSystem/admin/addNews.jsp");
}
}
%>
</body>
</html>
[解决办法]
楼主你先打印出来mySmartUpload.getFiles().getCount()看看结果再说啊,怕是永远都是0啊。因为没看到你用request接收什么参数啊?数据库处理最好放到java中去,最好不要在页面写这些代码。