JS技术

Java SSH 项目总结——Struts2上传图片组 - 张文康 廊坊师范学院信息技术提高班 第九期 - 博客频道 -(2)

字号+ 作者:H5之家 来源:H5之家 2015-12-13 09:57 我要评论( )

//验证图片格式function validateFileFormat(){//获取欲上传的文件路径var filepath1 = document.getElementById(file1).value;var d1=/\.[^\.]+$/.exec(filepath1);var filepath2 = document.getElementById(file2

//验证图片格式 function validateFileFormat(){ //获取欲上传的文件路径 var filepath1 = document.getElementById('file1').value;  var d1=/\.[^\.]+$/.exec(filepath1); var filepath2 = document.getElementById('file2').value;  var d2=/\.[^\.]+$/.exec(filepath2); var filepath3 = document.getElementById('file3').value;  var d3=/\.[^\.]+$/.exec(filepath3); var filepath4 = document.getElementById('file4').value;  var d4=/\.[^\.]+$/.exec(filepath4); var filepath5 = document.getElementById('file5').value;  var d5=/\.[^\.]+$/.exec(filepath5); //为了避免转义反斜杠出问题,这里将对其进行转换 var tp =".jpg,.image,.gif,.jpeg,.png,.bmp,.JPG,.GIF,.BMP,.IMAGE,.JPEG,.PNG"; //返回符合条件的后缀名在字符串中的位置 var rs1=tp.indexOf(d1); var rs2=tp.indexOf(d2); var rs3=tp.indexOf(d3); var rs4=tp.indexOf(d4); var rs5=tp.indexOf(d5); //如果返回的结果大于或等于0,说明包含允许上传的文件类型 if(rs1>=0){ if(rs2>=0){ if(rs3>=0){ if(filepath4==""){ if(filepath5==""){ return true; }else{ if(rs5>=0){ return true; }else{   alert("您选择五类型的扫描件不是有效的图片格式!");   return false; } } }else{ if(rs4>=0){ if(filepath5==""){ return true; }else{ if(rs5>=0){ return true; }else{   alert("您选择五类型的扫描件不是有效的图片格式!");   return false; } } }else{   alert("您选择四类型的扫描件不是有效的图片格式!");   return false; } } }else{   alert("您选择三类型的扫描件不是有效的图片格式!");   return false; } }else{   alert("您选择二类型的扫描件不是有效的图片格式!");   return false; }  }else{   alert("您选择一类型的扫描件不是有效的图片格式!");   return false;   } } //上传文件验证 function fileValidate(){ var fileName1=document.getElementById('file1').value; var fileName2=document.getElementById('file2').value; var fileName3=document.getElementById('file3').value; var fileName4=document.getElementById('file4').value; var fileName5=document.getElementById('file5').value; if(!fileName1) { alert("请选择上传文件:一类"); return false; }else{ document.getElementById('materialType1').value="1"; } if(!fileName2) { alert("请选择上传文件:二类"); return false; }else{ document.getElementById('materialType2').value="2"; document.getElementById('materialTypeName2').value="中华人民共和国组织机构代码"; } if(!fileName3) { alert("请选择上传文件:三类"); return false; }else{ document.getElementById('materialType3').value="3"; document.getElementById('materialTypeName3').value="法定代表人身份证"; } if(fileName4) { document.getElementById('materialType4').value="4"; document.getElementById('materialTypeName4').value="劳务派遣经验许可证"; //alert(document.getElementById('materialTypeName4').value) } if(fileName5) { document.getElementById('materialType5').value="5"; document.getElementById('materialTypeName5').value="其它"; //alert(document.getElementById('materialTypeName5').value) } return true; } //上传图片 function validate(){ //验证图片格式是否正确 var flag=validateFileFormat(); if(flag==true){ //alert("图片格式是正确,结果是:"+flag); //图片上传的参数准备 var flagss=fileValidate(); if(flagss==true){ //alert("图片上传最后的验证,结果是:"+flagss); //alert("文件要上传了"); //文件表单提交 document.fileForm.action="companyMaterial_add.action"; document.fileForm.submit(); } } }
 

 

 

 

action中的方法

 

 

package cn.bjjczb.jyzgcx.view.action; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import cn.bjjczb.jyzgcx.base.BaseAction; import cn.bjjczb.jyzgcx.domain.CompanyInfo; import cn.bjjczb.jyzgcx.domain.CompanyMaterial; import cn.bjjczb.jyzgcx.domain.DictionaryTitle; import cn.bjjczb.jyzgcx.domain.ReplyResult; @Controller @Scope("prototype") public class CompanyMaterialAction extends BaseAction<CompanyMaterial> { private List<File> uploads; // 上传多个文件的集合文本 private List<String> uploadsContentType; // /多个上传文件的类型集合 private List<String> uploadsFileName; // 多个上传文件的文件名集合 private List<String> materialTypeName1; private List<String> materialType1; /** 添加*/ public String add() throws Exception { //从session中得到上传材料的用户的信息 CompanyInfo returncompanyInfo = this.getCompanyInfo(); String comId=returncompanyInfo.getOrganizationCode(); String socId=returncompanyInfo.getSocialNumber(); CompanyInfo companyInfo=new CompanyInfo(); //得到第一类型的材料类别和名称 List<String> materialTypeList=this.getMaterialType1(); List<String> materialTypeNameList=this.getMaterialTypeName1(); //打印前台表单提交过来的文件数组的属性 System.out.println(uploadsFileName); System.out.println(materialTypeList); System.out.println(materialTypeNameList); // 把上传的文件放到指定的路径下 String path = ServletActionContext.getServletContext().getRealPath("/uploadImages"); // 写到指定的路径中 File file = new File(path); // 如果指定的路径没有就创建 if (!file.exists()) { file.mkdirs(); } // 把得到的文件的集合通过循环的方式读取并放在指定的路径下 System.out.println(uploads.size()); for (int i = 0; i < uploads.size(); i++) { try { Date date=new Date(); //list集合通过get(i)的方式来获取索引 System.out.println(uploadsFileName.get(i)); SimpleDateFormat stingDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String wordfileadd = stingDateFormat.format(date)+socId+uploadsFileName.get(i); FileUtils.copyFile(uploads.get(i), new File(file, wordfileadd)); } catch (IOException e) { e.printStackTrace(); } } //保存材料到数据库 String fileFolder = "\\"+"jyzgcx"+"\\"+"uploadImages"+"\\"; int n=uploadsFileName.size(); //调用方法保存材料到数据库 this.DoSaveAll(companyInfo, materialTypeList, materialTypeNameList, uploadsFileName, fileFolder, n); System.out.println("~~~~~~~~~~~~~~上传成功!"); return "repeatLogin"; } //保存图片到数据库 public void DoSaveAll(CompanyInfo companyInfo,List<String> materialTypeList,List<String> materialTypeNameList,List<String> uploadsFileName,String fileFolder,int s){ Date date=new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String createTime = formatter.format(date); CompanyMaterial companyMaterial = null; CompanyInfo returncompanyInfo = this.getCompanyInfo(); String comId=returncompanyInfo.getOrganizationCode(); String socId=returncompanyInfo.getSocialNumber(); SimpleDateFormat stingDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String wordfileadd = stingDateFormat.format(date)+socId; for (int i = 0; i < s; i++) { companyMaterial=new CompanyMaterial(); companyMaterial.setCompanyInfo(companyInfo); companyMaterial.setState("1"); companyMaterial.setCreateDate(createTime); companyMaterial.setMaterialType(materialTypeList.get(i)); companyMaterial.setMaterialTypeName(materialTypeNameList.get(i)); companyMaterial.setFileName(wordfileadd+uploadsFileName.get(i)); companyMaterial.setFileUrl(fileFolder+wordfileadd+uploadsFileName.get(i)); companyMaterialService.save(companyMaterial); } } public List<String> getMaterialTypeName1() { return materialTypeName1; } public void setMaterialTypeName1(List<String> materialTypeName1) { this.materialTypeName1 = materialTypeName1; } public List<String> getMaterialType1() { return materialType1; } public void setMaterialType1(List<String> materialType1) { this.materialType1 = materialType1; } public List<File> getUploads() { return uploads; } public void setUploads(List<File> uploads) { this.uploads = uploads; } public List<String> getUploadsContentType() { return uploadsContentType; } public void setUploadsContentType(List<String> uploadsContentType) { this.uploadsContentType = uploadsContentType; } public List<String> getUploadsFileName() { return uploadsFileName; } public void setUploadsFileName(List<String> uploadsFileName) { this.uploadsFileName = uploadsFileName; } }

 

小结

 



  • 上一篇Java SSH 项目总结——Ajax+Json 级联菜单
  • 下一篇Java SSH 项目总结——JS实现图片悬浮
  • 顶 1 踩 0

    我的同类文章

    猜你在找

    查看评论

    * 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • 老生常谈,JavaScript闭包中的this对象

      老生常谈,JavaScript闭包中的this对象

      2016-02-26 10:21

    • AngularJS使用HTML5摄像头拍照

      AngularJS使用HTML5摄像头拍照

      2016-02-23 09:42

    • 学习JavaScript之this,call,apply

      学习JavaScript之this,call,apply

      2016-01-28 20:45

    • JavaScript复习笔记--字符串

      JavaScript复习笔记--字符串

      2016-01-27 17:16

    网友点评
    /