/**第二种新增/修改方式:不用通过po类传值,省略写设置新增/修改的值(即:ps.set某类型(1,值)等等所有的设置sql的?号语句的值)
* 在ps=con.prepareStatement() 和ps.executeUpdate()中间调用该方法
* @param request HttpServletRequest对象
* @param response HttpServletResponse对象
* @param ps PreparedStatement对象
* @param it0 若为0,则是新增,参数str则是新增的SQL语句,若不为0,则参数str则是修改的SQL语句
* @param str SQL语句:新增语句格式,例:“insert into bas_driver(driverName,sex,phone) values(?,?,?);修改语句格式,例:“update bas_driver set driverName=?,sex=? where driverID=?”
* @param path 为”“时,则是新增/修改图片已图片的格式存进数据库,否则,path为路径,即数据库的图片存的是路径
* @param fileItems 为null时,为母表新增,当要多表新增时,把第一次新增的表用这个工具后返回的值再传进此参数(fileItems)内,即可多表新增
* 因为.parseRequest(request)赋值给fileItemsfileUpload后,它生命就结束了(没有值。所以只能再声明一个变量(fileItems)存放它的值)
* @param id 为第一张表新增时产生的id,传给其子表,所以,子表的新增sql语句,最后的字段为母表的主键
* 例子表sql"insert into sys_insurance(filingdate,caobaoID,insurancenumber,jiaoqiangshi,jiaoqiangzhong,carmessageID) values(?,?,?,?,?,?)”;:
* carmessageID 放在最后,是母表新产生的id(若sql语句那?已经赋值,则不用传)
* @提示 1.必须引入comons-fileupload.jar和commons-io.jar
* @提示 2.from表单加属性enctype=“multipart/form-data”
* @提示 3.from表单intput的name名称必须和新增/修改的SQL语句的字段名相同(若为修改,from表单设置隐藏域放着要修改的id,name名与修改哪条数据字段id相同)
* */
public static List FileUpload2(HttpServletRequest request,HttpServletResponse response,PreparedStatement ps,int it0,String str,String path,List fileItems,int id){
if(fileItemsnull){
//获取表单input里的值
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload fileUpload=new ServletFileUpload(factory);
try {
fileItems=fileUpload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
}
//判断是新增还是修改,截取所有的字段名
String[] strs;
if(it00){//新增
int it=str.indexOf("(");
int it2=str.indexOf(")");
//截取"(“与”)“之间的字符串
String string=str.substring(it+1,it2);
//分割字符串
strs=string.split(”,");
}else{//修改
int it=str.lastIndexOf(" “,str.indexOf(”="));
str=str.substring(it+1);
str=str.replace(",", “”);
str=str.replace(" where “, “”);
str=str.replace(”?", “”);
strs=str.split("=");
}
String name = null;
String value;
int i=0;
//遍历字段名,若字段名与input的name名相同,则获取该input值,把值设置进sql语句的?号里
for (String str3 : strs) {
i++;
for (FileItem fileItem : fileItems) {
if(fileItem.isFormField()){//判断input是否普通表单类型
try {
name=fileItem.getFieldName();
value=fileItem.getString("UTF-8");
if(str3.equals(name)){
if(value.equals("")){
ps.setObject(i, null);//若类型是日期/Boolean为""时,无法设置""进去,能设置null
}else{
ps.setObject(i, value);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}else{//不是普通类型则input是文件类型,执行文件的操作
name=fileItem.getFieldName();
String name2=fileItem.getName();
if(str3.equals(name)){
if(fileItem.getSize()>0){//判断该文件有没有上传
InputStream in;
try {
in = fileItem.getInputStream();
if(path.equals("")){//path等于""说明数据库存的是图片
ps.setBinaryStream(i, in);
}else{//否则说明数据库存的是图片的路径
File file=new File(path);
if(!file.exists()){
file.mkdir();
}
SimpleDateFormat dateFormat3=new SimpleDateFormat("yyyyMMddHHmmss");
String path2=file+"\\"+dateFormat3.format(new Date())+name2;
OutputStream out=new FileOutputStream(path2);
byte[] byt=new byte[1024];
int len=0;
while((len=in.read(byt))!=-1){
out.write(byt,0,len);
}
out.close();
ps.setObject(i, path2);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}else{//没有上传,则设置为null
try {
ps.setObject(i, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
if(id!=0){
try {
ps.setInt(strs.length, id);
} catch (SQLException e) {
e.printStackTrace();
}
}
return fileItems;
}