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; } }
小结
顶 1 踩 0
我的同类文章
猜你在找
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场