快手流量推广网站,成都软件外包开发,哈尔滨建站模板搭建,wordpress 博客模板相信大家只要接触到数据库操作#xff0c;尤其是关于商城的类的网站肯定图片上传肯定必不可少#xff01;
下面废话不多说#xff0c;直接进入主题
第一步
首先将smartupload.jar 我这里提供一个连接工具包导入到您的项目直接放到WebRoot/Web-INF/lib下面 然后buid path …相信大家只要接触到数据库操作尤其是关于商城的类的网站肯定图片上传肯定必不可少
下面废话不多说直接进入主题
第一步
首先将smartupload.jar 我这里提供一个连接工具包导入到您的项目直接放到WebRoot/Web-INF/lib下面 然后buid path add to build path
然后就可以直接在jsp页面中写java代码 request.setCharacterEncoding(UTF-8);//设置编码集SmartUpload sunew SmartUpload();//创建文件上传控件su.initialize(pageContext);//控件初始化 Request suRequestsu.getRequest();//获取请求信息
su.setCharset(UTF-8);//设置编码集//上传到SmartUpload
su.setAllowedFilesList(gif,jpg,png);//设置允许上传文件格式图片gif,jpg,png
su.setDeniedFilesList(jsp,asp,html);//设置禁止上传文件格式
su.upload();//上传
File file su.getFiles().getFile(0);//得到单个上传文件的信息
String filepath upload/;
//设置文件在服务器的保存位置
if(file.getSize()!0){
filepathupload/;
//获取时间戳
String timeStrSystem.currentTimeMillis();
//生成随机数
int x(int)(Math.random()*100);
filepath timeStrxfile.getFileName();
file.setCharset(UTF-8);//设置文件的编码集file.saveAs(filepath, SmartUpload.SAVE_VIRTUAL);//文件另存为}else {out.print(你还没有上传图片);
}String username suRequest.getParameter(name);
String password suRequest.getParameter(pwd);
//username.getBytes(utf-8);
//String password2 request.getParameter(password2);
String birthdatesuRequest.getParameter(birth);String phonesuRequest.getParameter(phone);
String sexsuRequest.getParameter(sex);
String imagesuRequest.getParameter(image);UserDao userInfoDaonew UserDaoImpl();
UserInfo userInfonew UserInfo(username,password,phone,filepath,birthdate,Integer.parseInt(sex));boolean flaguserInfoDao.InsertInto(userInfo);//插入一条记录
String str;
if(flag){
str保存成功;// RequestDispatcher rdrequest.getRequestDispatcher(lognIn.jsp);
//rd.forward(request, response);
}else{
str保存失败;
}
%保存结果:%str %
UserDao //插入用户信息
boolean InsertInto(UserInfo userInfo);UserDaoImpl 接口实现类Override
public boolean InsertInto(UserInfo userInfo) {
boolean flag false;// 标签记录插入结果
Connection conn null;
Statement stmt null;
PreparedStatement pstmt null;
ResultSet rs null;
try {
conn db.testconn();// 获取连接
pstmt conn.prepareStatement(insert into userInfo(U_Name,U_Pwd,U_Birthday,U_Phone,U_sex,U_ImageUrl)values(?,?,?,?,?,?););// 加载sql
// String sql
// insert into userInfo(U_Name,U_Pwd,U_Birthday,U_Phone)values(?,?,?,?);;
pstmt.setString(1, userInfo.getU_Name());// 为占位符赋值
pstmt.setString(2, userInfo.getU_Pwd());
pstmt.setString(3, userInfo.getU_Birthday());// 为占位符赋值
pstmt.setString(4, userInfo.getPhone());
pstmt.setInt(5, userInfo.getSex());
pstmt.setString(6, userInfo.getImageUrl());
int rowN pstmt.executeUpdate();// 执行sql
if (rowN 1) {// 执行成功时
flag true;
}
// System.out.println(sql:sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {// 关闭资源
db.closeAll(conn, stmt, null);
}
return flag;
} 图片的js验证代码 function varImage(){
var flagfalse;
var imageurldocument.getElementById(image).value;//获取id为image
var regs/[.png|.jpg|.gif|.PNG|.JPG|.GIF|]{1}$/;//允许以下格式进行上传
flagregs.test(imageurl);//验证格式是否正确 否则提示文件格式错误
if(imageurl||flag){flagtrue;
}else{
alert(文件格式错误!);}效果如图所示