javaWeb文件以及图片上传smartupload工具类的使用

相信大家只要接触到数据库操作,尤其是关于商城的类的网站肯定图片上传肯定必不可少!

下面废话不多说,直接进入主题

第一步

首先将smartupload.jar 我这里提供一个连接工具包导入到您的项目直接放到WebRoot/Web-INF/lib下面 然后buid path add to  build path

然后就可以直接在jsp页面中写java代码

 request.setCharacterEncoding("UTF-8");//设置编码集
 SmartUpload su=new SmartUpload();//创建文件上传控件
 su.initialize(pageContext);//控件初始化 

Request suRequest=su.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){
filepath="upload/";
//获取时间戳
String timeStr=System.currentTimeMillis()+"";
//生成随机数
int x=(int)(Math.random()*100);
filepath += timeStr+x+file.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 birthdate=suRequest.getParameter("birth");
        String phone=suRequest.getParameter("phone");
String sex=suRequest.getParameter("sex");
String image=suRequest.getParameter("image");
  UserDao userInfoDao=new UserDaoImpl();
UserInfo userInfo=new UserInfo(username,password,phone,filepath,birthdate,Integer.parseInt(sex));

boolean flag=userInfoDao.InsertInto(userInfo);//插入一条记录
String str="";
if(flag){
str="保存成功";
  // RequestDispatcher  rd=request.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 flag=false;
var imageurl=document.getElementById("image").value;//获取id为image
var regs=/[.png|.jpg|.gif|.PNG|.JPG|.GIF|]{1}$/;//允许以下格式进行上传
flag=regs.test(imageurl);//验证格式是否正确 否则提示文件格式错误
if(imageurl==""||flag){
  flag=true;
}else{
alert("文件格式错误!");

}

效果如图所示

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值