package com.fuzamei.web; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.ConstraintViolation; import javax.validation.Valid; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.alibaba.fastjson.JSON; import com.fuzamei.constant.HintMSG; import com.fuzamei.constant.Role; import com.fuzamei.entity.BusinessCheck; import com.fuzamei.entity.UserDetail; import com.fuzamei.service.AttachmentService; import com.fuzamei.service.BusinessDeclareService; import com.fuzamei.service.UserAuthoricationService; import com.fuzamei.utils.JSONUtil; import com.fuzamei.utils.PageDTO; import com.fuzamei.utils.RelativePathUtil; import com.fuzamei.utils.ValidationUtil; @Controller @RequestMapping("/business_declare") public class BusinessDeclareAction { @Autowired private BusinessDeclareService businessDeclareService; @Autowired private UserAuthoricationService userAuthoricationService; @Autowired private AttachmentService attachmentService; private static final String SP=File.separator; private static final int ROW_NUM = 10; // 分页每页显示数据的数量 /** * * @Title: uploadValuationPOIByBank * @Description: TODO(业务申报功能,需要完成【业务核准表】【尽职调查报告】【产品核准表】【基础性资料】4个文件的上传,插入业务申报表1条数据,插入附件表4条数据,还有插入操作历史1条数据) * 前段以【表单】的形式传过来一组数据: * businessApproval:对应业务核准表的文件 * responsibleInvestigation:对应尽职调查报告文件 * productApproval:产品核准表文件 * basicData:基础性资料文件 * businessName:业务名称 * @param @param files * @param @param request * @param @param businessName 由经办行写入的业务名称,例如:嘉实活期基金 * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月19日 上午11:23:27 * @throws */ @RequestMapping(value="/uploadBusinessMaterialsByBranchBank",method=RequestMethod.POST) @ResponseBody private Map uploadValuationPOIByBank(@RequestParam("businessApproval") MultipartFile file1, @RequestParam("responsibleInvestigation") MultipartFile file2, @RequestParam("productApproval") MultipartFile file3, @RequestParam("basicData") MultipartFile file4, HttpServletRequest request, @RequestParam("businessName") String businessName, @RequestParam("userId") Object uId) { try { //首先校验用户权限问题 int userId=ValidationUtil.checkAndAssignInt(uId); UserDetail userDetail = userAuthoricationService.checkUserAuthority(userId,Role.TRAN_BRANCH_BANK);//查看用户非空和权限(只有【经办支行】有这个权限操作) //首先保证文件不能为空 if(file1.isEmpty()||file2.isEmpty()||file3.isEmpty()||file4.isEmpty()){ throw new RuntimeException("上传文件不能为空"); } String filename1 = file1.getOriginalFilename(); //业务核准表的文件名 String filename2 = file2.getOriginalFilename(); //尽职调查表的文件名 String filename3 = file3.getOriginalFilename(); //产品核准表的文件名 String filename4 = file4.getOriginalFilename(); //基础资料的文件名 //判断上传文件格式是否正确 if(!(filename1.endsWith(".xls")||filename1.endsWith("xlsx"))&& !(filename2.endsWith(".xls")||filename2.endsWith("xlsx"))&& !(filename3.endsWith(".xls")||filename3.endsWith("xlsx"))&& !(filename4.endsWith(".xls")||filename4.endsWith("xlsx"))){ throw new RuntimeException("上传文件格式不对"); } //四个文件名不能有相同的两个文件名(暂时考虑是将四个文件放在一个文件夹下面) if(filename1.equals(filename2)|| filename1.equals(filename3)|| filename1.equals(filename4)|| filename2.equals(filename3)|| filename2.equals(filename4)|| filename3.equals(filename4)){ throw new RuntimeException("文件名不能相同"); } //确定好上传的文件的顶级父级路径=======================================================================>>待定TODO String path = request.getServletContext().getRealPath(""); //SP是系统分隔符 String relativePath = RelativePathUtil.formatPath("/"+userId, ""); //生成文件的相对保存路径 Integer businessId=businessDeclareService.generateBusinessId(); //新创建的业务申报生成的不重复id号=============================>>生成id号的机制要改TODO String bank=userDetail.getOrganization_name(); //银行名称,但是表的字段是银行id?????。。。。。。。=======>>暂且按照当前用户的组织名进行录入TODO businessName=ValidationUtil.checkBlankAndAssignString(businessName); //校验前端传过来的业务名称并进行赋值 String busineeApprovalName=filename1; //业务员核准表的文件名 String urlOfBusineeApproval=relativePath+filename1; //业务核准表的url地址 String responsibleInvestigationName=filename2; //尽职调查表的文件名 String urlOfResponsibleInvestigation=relativePath+filename2; //尽职调查表的url地址 String productApprovalName=filename3; //产品核准表的文件名 String urlOfProductApproval=relativePath+filename3; //产品核准表的url地址 String basicDataName=filename4; //基础性资料的文件名 String urlOfBasicData=relativePath+filename4; //基础性资料的url地址 //一次性返回4个不同的id号,且不会和数据库中的重复 List attachmentIds = attachmentService.generateMultiAttachmentIds(4); Integer busineeApprovalId=attachmentIds.get(0); //业务核准表的id号=====================================>>生成id号的机制要改TODO Integer responsibleInvestigationId=attachmentIds.get(1); //尽职调查表id号=====================================>>生成id号的机制要改TODO Integer productApprovalId=attachmentIds.get(2); //产品核准表的id号=====================================>>生成id号的机制要改TODO Integer basicDataId=attachmentIds.get(3); //基础性资料的id号=====================================>>生成id号的机制要改TODO //--------------------------------------------------------------------------------------------------------------- //处理整个业务生报表的一些信息 Map map =new LinkedHashMap(); //这个map用于存储所有要去service的数据 map.put("userDetail", userDetail); //用户详细信息 map.put("businessId", businessId); //将业务id活♥ map.put("bank", bank); //先将申报的支行名字写活♥ map.put("businessName", businessName); //业务名称 //--------------------------------------------------------------------------------------------------------------- //处理businessApproval(业务核准表)文件信息 map.put("busineeApprovalId", busineeApprovalId); //将业务核准表的id写活 map.put("busineeApprovalName",busineeApprovalName); //业务核准表名称,用于存在attachment中的附件名 map.put("urlOfBusineeApproval", urlOfBusineeApproval); //文件下载相对路径url //--------------------------------------------------------------------------------------------------------------- //处理responsibleInvestigation(尽职调查报告)文件信息 map.put("responsibleInvestigationId", responsibleInvestigationId); //将尽职调查报告的id先写活♥ map.put("responsibleInvestigationName",responsibleInvestigationName); //业务核准表名称,用于存在attachment中的附件名 map.put("urlOfResponsibleInvestigation", urlOfResponsibleInvestigation); //文件下载相对路径url //--------------------------------------------------------------------------------------------------------------- //处理productApproval(产品核准表)文件信息 map.put("productApprovalId", productApprovalId); //将产品核准表的id先写活♥ map.put("productApprovalName",productApprovalName); //业务核准表名称,用于存在attachment中的附件名 map.put("urlOfProductApproval", urlOfProductApproval); //文件下载相对路径url //--------------------------------------------------------------------------------------------------------------- //处理basicData(基础性资料)文件的上传功能 map.put("basicDataId", basicDataId); //将业务核准表的id先写活♥ map.put("basicDataName",basicDataName); //业务核准表名称,用于存在attachment中的附件名 map.put("urlOfBasicData", urlOfBasicData); //文件下载相对路径url //--------------------------------------------------------------------------------------------------------------- //将经办支行提交的所有数据信息通过map传入service层 businessDeclareService.insertInformationIntoTableByBranchBank(map); //--------------------------------------------------------------------------------------------------------------- String pathFile1=path+relativePath+filename1; String pathFile2=path+relativePath+filename2; String pathFile3=path+relativePath+filename3; String pathFile4=path+relativePath+filename4; File newFile1=new File(pathFile1); File newFile2=new File(pathFile2); File newFile3=new File(pathFile3); File newFile4=new File(pathFile4); //==============文件上传开始================ //文件夹和文件最后进行创建和创建,不然一旦事物报错,这些文件就变成垃圾数据了 File dir=new File(path+relativePath); if(!dir.exists()){ dir.mkdirs(); } file1.transferTo(newFile1); file2.transferTo(newFile2); file3.transferTo(newFile3); file4.transferTo(newFile4); //==============文件上传结束================ return JSONUtil.getJsonMap(200, true, HintMSG.UPLOAD_SUCCESS,null); //返回给前端一个map进行前段提示 } catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.UPLOAD_FAIL+":"+e.getMessage(),null); //返回给前端一个map进行前段提示 } } /** * * @Title: queryBusinessDeclareInformation * @Description: TODO(业务申报的查看,将所有信息返还给界面) * @param @param data * 前端传来的数据json格式为 * { * "userId":XXX, * "businessName":"", * "page":"", * "startTime":"", * "endTime":"" * } * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月19日 下午4:01:02 * @authority: 经办支行,二级支行,省分行 */ @RequestMapping(value="/queryBusinessDeclareInformation",method=RequestMethod.POST) @ResponseBody private Map queryBusinessDeclareInformation(@RequestBody String data){ try { System.out.println("查询业务申请信息...."); @SuppressWarnings("unchecked") Map map = JSON.parseObject(data, Map.class); //将前端传过来的json数据转化为map对象,并将map对象传入service层进行数据校验 //首先校验用户权限问题 int userId=ValidationUtil.checkAndAssignInt(map.get("userId")); //查看用户非空和权限(只有【经办支行,二级支行和省分行】有这个权限操作) UserDetail userDetail = userAuthoricationService.checkUserAuthority(userId,Role.TRAN_BRANCH_BANK,Role.SEC_BRANCH_BANK,Role.BUSINESS_DEP_OF_PBB); int page = ValidationUtil.checkMinAndAssignInt(map.get("page"), 1); // 默认页是第一页 String businessName=ValidationUtil.checkBlankStringAndAssignEmptyIfIsBlank(map.get("businessName")); //业务名默认为空 long startTime = ValidationUtil.checkAndAssignDefaultLong(map.get("startTime"), 0L); //开始时间默认0 long endTime = ValidationUtil.checkAndAssignDefaultLong(map.get("endTime"), Long.MAX_VALUE); //结束时间默认Long最大值 Map mapToService = new LinkedHashMap(); mapToService.put("userDetail", userDetail); mapToService.put("startPage", (page - 1) * ROW_NUM); mapToService.put("businessName", businessName); //如果时间顺序错位,要重新进行排位 if (startTime <= endTime) { mapToService.put("startTime", startTime); mapToService.put("endTime", endTime); } else { mapToService.put("startTime", startTime); mapToService.put("endTime", Long.MAX_VALUE); } mapToService.put("rowNum", ROW_NUM); // 默认每页显示数据是10条,可根据需求修改分页数量 PageDTO pageDto = businessDeclareService.queryBusinessDeclareInformation(map); return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, pageDto); } catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null); } } /** * * @Title: queryBusinessDeclareInformation * @Description: TODO(审核记录的查询,当用户点击查看按钮后,不仅显示了业务申报的信息,还显示了审核记录的信息,这里业务申报的信息直接从前端获取,审核记录根据前端提供的业务id号business_id来进行查询) * 这个信息要去业务审核总表中去查询 * 前端传过来的json格式 { "businessId":32323 "userId":xxx } * @param @param data * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月19日 下午7:46:44 * @authority: 经办支行,二级支行,省分行 */ @RequestMapping(value="/queryBusinessCheckInformation",method=RequestMethod.POST) @ResponseBody private Map queryBusinessCheckInformation(@RequestBody String data){ try { System.out.println("查询业务审核总表信息...."); @SuppressWarnings("unchecked") Map map = JSON.parseObject(data, Map.class); //将前端传过来的json数据转化为map对象,并将map对象传入service层进行数据校验 //首先校验用户权限问题 int userId=ValidationUtil.checkAndAssignInt(map.get("userId")); //查看用户非空和权限(只有【经办支行,二级支行和省分行】有这个权限操作) UserDetail userDetail = userAuthoricationService.checkUserAuthority(userId,Role.TRAN_BRANCH_BANK,Role.SEC_BRANCH_BANK,Role.BUSINESS_DEP_OF_PBB); Integer businessId=ValidationUtil.checkAndAssignInt(map.get("businessId")); //被审核的业务id号需要先被进行校验 Map mapToService = new LinkedHashMap(); mapToService.put("userDetail", userDetail); mapToService.put("businessId", businessId); List businessCheckList = businessDeclareService.queryBusinessCheckInformation(mapToService); return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, businessCheckList); } catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null); } } /** * * @Title: businessDeclareCheckedBy * @Description: TODO(审核意见的提交) * @param @param data * 数据json包含 * { * "checkResult":结果(同意5或者不同意6---数字以状态表为准), * "checkOpinion":审核意见, * "businessId":业务id, * "userId":XXX * } * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月19日 下午8:52:35 * @throws */ @RequestMapping(value="/businessDeclareCheckedBy",method=RequestMethod.POST) @ResponseBody private Map businessDeclareCheckedBy(@RequestBody String data){ try { System.out.println("添加到业务审核总表信息...."); @SuppressWarnings("unchecked") Map map = JSON.parseObject(data, Map.class); //将前端传过来的json数据转化为map对象,并将map对象传入service层进行数据校验 //首先校验用户权限问题 int userId=ValidationUtil.checkAndAssignInt(map.get("userId")); //查看用户非空和权限(只有【二级支行和省分行】有这个权限操作) UserDetail userDetail = userAuthoricationService.checkUserAuthority(userId,Role.SEC_BRANCH_BANK,Role.BUSINESS_DEP_OF_PBB); //审核结果进行校验并赋值 Integer checkResult = ValidationUtil.checkAndAssignInt(map.get("checkResult")); if(!(checkResult==5||checkResult==6)){ throw new RuntimeException("审核结果参数非法"); } //审核意见进行校验并赋值 String checkOpinion = ValidationUtil.checkBlankAndAssignString(map.get("checkOpinion")); //被审核的业务id号需要先被进行校验 Integer businessId=ValidationUtil.checkAndAssignInt(map.get("businessId")); Map mapToService = new LinkedHashMap(); mapToService.put("userDetail", userDetail); mapToService.put("checkResult", checkResult); mapToService.put("checkOpinion", checkOpinion); mapToService.put("businessId", businessId); businessDeclareService.businessDeclareCheckedBy(mapToService); return JSONUtil.getJsonMap(200, true, HintMSG.OPERATION_SUCCESS, null); } catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.OPERATION_FAIL+":"+e.getMessage(), null); } } /** * * @Title: uploadContractByProvincialBranchBank * @Description: TODO(省分行业务部门上传合同的功能实现) * 前端需要传递过来的数据(以表单形式)有以下几个:contract:合同文件 * businessId:业务id号 * * * @param @param file * @param @param request * @param @param businessId * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月20日 下午4:21:51 * @throws */ @RequestMapping(value="/uploadContractByProvincialBranchBank",method=RequestMethod.POST) @ResponseBody private Map uploadContractByProvincialBranchBank(@RequestParam("contract") MultipartFile file, HttpServletRequest request, @RequestParam("businessId") Object bId, @RequestParam("userId") Object uId) { try { //首先校验用户权限问题 int userId=ValidationUtil.checkAndAssignInt(uId); UserDetail userDetail = userAuthoricationService.checkUserAuthority(userId,Role.BUSINESS_DEP_OF_PBB);//查看用户非空和权限(只有【经办支行】有这个权限操作) //首先保证文件不能为空 if(file.isEmpty()){ throw new RuntimeException("上传文件不能为空"); } //合同文件名 String filename = file.getOriginalFilename(); //判断上传文件格式是否正确 if(!(filename.endsWith(".xls")||filename.endsWith("xlsx"))){ throw new RuntimeException("上传文件格式不对"); } //确定好上传的文件的顶级父级路径=======================================================================>>待定TODO String path = request.getServletContext().getRealPath(""); //SP是系统分隔符 String relativePath = RelativePathUtil.formatPath("/"+userId, ""); //生成文件的相对保存路径 Integer businessId = ValidationUtil.checkAndAssignInt(bId); //校验业务id并赋值 Integer attachmentId = attachmentService.generateAtachmentId(); //生成不重复的附件id号 Map mapToService =new LinkedHashMap(); //这个map用于存储所有要去service的数据 mapToService.put("businessId", businessId); //将业务id先写死,到时候还要再进行修改。。。。。TODO mapToService.put("contractParty", "建行省分行&信托公司"); //合同的签约方。。。TODO。。。。【这里先写死,可能通过上传的合同文件获取签约方的信息】 // mapToService.put("uploadDepartment", userDetail.getOrganization_name()); //上传部门按照组织名来写 // mapToService.put("uploadPerson", userDetail.getPerson_name()); //上传人员写活♥ mapToService.put("contractId", attachmentId); //将合同的id写活♥ mapToService.put("contractName",filename); //合同名称,用于存在attachment中的附件名 mapToService.put("url", relativePath+filename); //将文件下载路径url通过map传到service层进行校验 mapToService.put("hash", "to be calculated"); //文件hash值先写死,到时候需要从服务器上的文件获取 mapToService.put("userDetail", userDetail); //直接将用户详细信息传到service层 businessDeclareService.uploadContractByProvincialBranchBank(mapToService); //将合同信息插入到【合同提交记录表contract_record、合同状态跟踪表contract_status_tracking、附件表attachment】 String pathFile=path+relativePath+filename; File newFile = new File(pathFile); //==========文件上传开始============ File dir=new File(path+relativePath); if(!dir.exists()){ dir.mkdirs(); } file.transferTo(newFile); //将---合同---文件上传到指定的文件夹下 //==========文件上传结束============ return JSONUtil.getJsonMap(200, true, HintMSG.UPLOAD_SUCCESS, null); }catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.UPLOAD_FAIL+":"+e.getMessage(), null); } } /** * @Title: download * @Description: TODO(实现所有上传的文件的下载功能)=====>>仅局限于业务审核表,尽职调查表,产品核准表和基础性资料这4个文件的下载 * 前段以【表单】的形式传过来一组数据: * @param url:下载地址 * userId:用户id号 * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月21日 下午2:05:13 * @throws */ @RequestMapping(value="/downloadBusinessDeclareFiles") @ResponseBody private Map downloadBusinessDeclareFiles(HttpServletRequest request,HttpServletResponse response, @RequestParam("url") String url, @RequestParam("userId") Object uId) { try { int userId=ValidationUtil.checkAndAssignInt(uId); userAuthoricationService.checkUserAuthority(userId,Role.TRAN_BRANCH_BANK,Role.SEC_BRANCH_BANK,Role.BUSINESS_DEP_OF_PBB); //查看用户非空和权限(经办支行,二级分行,省分行都有这个权限操作) url=ValidationUtil.checkBlankAndAssignString(url); //对传过来的url进行非空校验 String fileName = request.getServletContext().getRealPath("")+url; //SP是系统分隔符 //如果文件不存在直接抛出异常 if(!new File(fileName).exists()){ throw new RuntimeException(HintMSG.FILE_NOT_FOUND); } InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); String filename = url.substring(url.lastIndexOf("/")+1); //截取文件名 filename = URLEncoder.encode(filename,"UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.setContentType("multipart/form-data"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); int len = 0; while((len = bis.read()) != -1){ out.write(len); out.flush(); } out.close(); bis.close(); return JSONUtil.getJsonMap(200, true, HintMSG.DOWNLOAD_SUCCESS, null); //返回给前端一个map进行前段提示 } catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.DOWNLOAD_FAIL+":"+e.getMessage(), null); //返回给前端一个map进行前段提示 } } /** * @Title: download * @Description: TODO(实现托管合同的下载功能)=====>>仅局限于托管合同这1个文件的下载 * 前段以【表单】的形式传过来一组数据: * @param url:下载地址 * userId:用户id号 * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2017年12月21日 下午2:05:13 * @throws */ @RequestMapping(value="/downloadContractFiles") @ResponseBody private Map downloadContractFiles(HttpServletRequest request,HttpServletResponse response, @RequestParam("url") String url, @RequestParam("userId") Object uId) { try { int userId=ValidationUtil.checkAndAssignInt(uId); userAuthoricationService.checkUserAuthority(userId,Role.BUSINESS_DEP_OF_PBB); //查看用户非空和权限(在业务申报的模块中只有省分行才有权限下载这个合同) url=ValidationUtil.checkBlankAndAssignString(url); //对传过来的url进行非空校验 String fileName = request.getServletContext().getRealPath("")+url; //SP是系统分隔符 //如果文件不存在直接抛出异常 if(!new File(fileName).exists()){ throw new RuntimeException(HintMSG.FILE_NOT_FOUND); } InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); String filename = url.substring(url.lastIndexOf("/")+1); //截取文件名 filename = URLEncoder.encode(filename,"UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.setContentType("multipart/form-data"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); int len = 0; while((len = bis.read()) != -1){ out.write(len); out.flush(); } out.close(); bis.close(); return JSONUtil.getJsonMap(200, true, HintMSG.DOWNLOAD_SUCCESS, null); //返回给前端一个map进行前段提示 } catch (Exception e) { return JSONUtil.getJsonMap(500, false, HintMSG.DOWNLOAD_FAIL+":"+e.getMessage(), null); //返回给前端一个map进行前段提示 } } @RequestMapping(value="/test") @ResponseBody public Map test(@RequestBody String data){ try { List generateMultiAttachmentIds = attachmentService.generateMultiAttachmentIds(4); return JSONUtil.getJsonMap(200, true, "成功",generateMultiAttachmentIds); } catch (Exception e) { return JSONUtil.getJsonMap(200, true, "失败:"+e.getMessage(),null); } } /** * 上传文件功能模块 * @param files * @param request * @return * @throws IllegalStateException * @throws IOException */ /* @RequestMapping(value="/upload",method=RequestMethod.POST) @ResponseBody public String upload(@RequestParam("file") MultipartFile[] files,HttpServletRequest request) throws IllegalStateException, IOException{ String path = request.getSession().getServletContext().getRealPath("content"); File dir=new File(path); if(!dir.exists()){ dir.mkdirs(); } for (MultipartFile file : files) { String filename = file.getOriginalFilename(); String pathFile=path+"/"+filename; File newFile=new File(pathFile); file.transferTo(newFile); } return "upload success"; } */ /** * 文件下载功能 * @param request * @param response * @throws IOException */ /* @RequestMapping(value="/download") public void download(HttpServletRequest request,HttpServletResponse response) throws IOException{ String fileName = request.getSession().getServletContext().getRealPath("content")+"/"+"demo.sql"; InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); String filename = "下载文件.jpg"; filename = URLEncoder.encode(filename,"UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.setContentType("multipart/form-data"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); int len = 0; while((len = bis.read()) != -1){ out.write(len); out.flush(); } out.close(); bis.close(); } */ /** * * @Title: download * @Description: TODO(文件下载的另一种方法) * @param @param request * @param @return * @param @throws IOException 设定文件 * @return ResponseEntity 返回类型 * @author ylx * @date 2017年12月19日 上午10:21:33 * @throws */ /* @RequestMapping(value = "/download2",produces = "application/octet-stream;charset=UTF-8") public ResponseEntity download(HttpServletRequest request) throws IOException { // 指定文件,必须是绝对路径 File file = new File(request.getSession().getServletContext().getRealPath("content")+"/"+"demo.sql"); // 下载浏览器响应的那个文件名 String dfileName = URLEncoder.encode("下载文件.jpg","UTF-8");; // 下面开始设置HttpHeaders,使得浏览器响应下载 HttpHeaders headers = new HttpHeaders(); // 设置响应方式 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 设置响应文件 headers.setContentDispositionFormData("attachment", dfileName); // 把文件以二进制形式写回 return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } */ }