81,092
社区成员
发帖
与我相关
我的任务
分享
String path = super.getServletContext().getRealPath("/Images/touxiang");
//声明SmartUpload类对象
SmartUpload su = new SmartUpload();
//初始化的方法必须先执行
su.initialize(config,request,response);
try {
su.setAllowedFilesList("jpg");//设置允许上传文件格式(图片)gif,jpg,png
su.setDeniedFilesList("jsp,asp,html,gif,jpeg,png,bmp");//设置禁止上传文件格式
su.setMaxFileSize(210000); //限制每个上传文件的最大长度。
su.upload();
File myfile = su.getFiles().getFile(0);
String ext=su.getFiles().getFile(0).getFileExt();//获取文件后缀
//时间方式命名
String tempname=session.getAttribute("id").toString();
String lastName=tempname +"."+ ext;//完整文件名
myfile.saveAs( path +"/"+ lastName);
Class.forName(className);
conn=DriverManager.getConnection(url,user,password);
Statement stmt= conn.createStatement();
String sql2="select pictouxiang_name from pictouxiang where pictouxiang_employee_ID='"+ session.getAttribute("id").toString() +"'";
ResultSet rs=stmt.executeQuery(sql2);
String onName="";
if(rs.next())
{
onName=rs.getString(1);
}
if (onName=="")
{
String sql="insert into pictouxiang(pictouxiang_employee_ID,pictouxiang_name) values ('"+ session.getAttribute("id").toString() +"','"+ lastName +"')";
st = (Statement) conn.createStatement();// 创建用于执行静态sql语句的Statement对象
int count = st.executeUpdate(sql);// 执行插入操作的sql语句,并返回插入数据的个数
}
else{
String sql1="update pictouxiang set pictouxiang_name='"+ lastName +"' where pictouxiang_employee_ID='"+ session.getAttribute("id").toString() +"'";
st = (Statement) conn.createStatement();// 创建用于执行静态sql语句的Statement对象
int count = st.executeUpdate(sql1);// 执行插入操作的sql语句,并返回插入数据的个数
}
conn.close();//关闭数据库连接
out.println("<script>window.alert('上传成功!');window.location.href='Employee_touxiang.jsp';</script>");
}
catch (Exception e){
out.println("<script>window.alert('只允许上传200k以内且为JPG格式的图片');window.location.href='Employee_touxiang.jsp';</script>");
}