springMVC4--文件上传CommonsMultipartFile ----------------------------------文件下载----------------------------------------------------------- //dto public class MailFileUploadDto { private String m_id ; //与tb_mail_info表的m_id对应 private String a_id ; //主键,操作类型1018 private String oldname ; //原始文件名 private String newname ; //新文件名 private String dir ; //新文件所在路径 private Date create_time ; //创建时间 ………………………………………………………………………………………………………………………………………………… } //controller /**邮件附件上传 * @param file * @param vo * @param response * @throws Exception */ @RequestMapping(value="/mailUpload") public void mailUpload(@RequestParam(name="file")CommonsMultipartFile file, MailFileUploadDto vo,HttpServletResponse response)throws Exception{ if(file==null||file.getSize()==0){ throw new BusinessException("UPLOAD", "无上传文件或文件大小为0"); } String filename = file.getOriginalFilename(); String suffix = filename.substring(filename.lastIndexOf(".")+1); //获取文件上传类型 if( (!"doc".equals(suffix)) && (!"docx".equals(suffix)) && (!"xls".equals(suffix)) && (!"xlsx".equals(suffix))&& (!"ppt".equals(suffix)) && (!"pptx".equals(suffix)) && (!"rar".equals(suffix)) && (!"zip".equals(suffix))&& (!"pdf".equals(suffix))&& (!"exe".equals(suffix))){ throw new BusinessException("UPLOAD", "附件类型为:.doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.exe,.rar或者 .zip文件(要上传其他类型文件时,可压缩上传)"); } boolean flag=mailService.checkFileSl(vo); if(!flag){ throw new BusinessException("UPLOAD", "一次最多只能上传6个附件!"); } List<MailFileUploadDto> list=mailService.upload(file,vo); response.setContentType("text/html;charset=utf-8"); Writer writer = response.getWriter(); writer.write("{\"success\":true,\"data\":"+JsonUtils.getJsonData(list)+"}"); writer.flush(); } //impl @Override public List<MailFileUploadDto> upload(CommonsMultipartFile file, MailFileUploadDto vo) throws Exception { // TODO Auto-generated method stub TimeContext<?> timeContext = AccessContextManagerUtils.application().get(TimeContext.class); Date d = new Date(timeContext.getDatabaseTime()); String m_id=""; // Long file_size=file.getSize(); if(vo.getM_id()==null || StringUtils.isBlank(vo.getM_id())){ String sequence="SELECT FUNC_GENERATE_LSH('1017') FROM dual "; //tb_mail_file表m_id m_id=jdbcTemplate.queryForObject(sequence, String.class); vo.setM_id(m_id); } String filename = file.getOriginalFilename(); String suffix = filename.substring(filename.lastIndexOf(".")+1); //获取文件上传类型 String newfilename = "Mail_"+DateUtils.formatDate(d, "yyyyMMddHHmmssSSS") + "." + suffix; String newpath = PropUtils.getProperty("mailUploadPath"); String newfile = newpath + newfilename; //文件上传路径 File savedir=new File(newpath); if(!savedir.exists()){ savedir.mkdir(); } FileInputStream fis = null; FileOutputStream fos = null; //写文件 try { fos = new FileOutputStream(newfile); fos.write(file.getBytes()); } catch (FileNotFoundException e) { throw new BusinessException("args", "文件不存在"); } catch (IOException e) { throw new BusinessException("args", "文件上传失败"); } finally{ if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } String a_id = "SELECT FUNC_GENERATE_LSH('1018') FROM dual"; //tb_mail_file表a_id a_id=jdbcTemplate.queryForObject(a_id, String.class); String insert="INSERT INTO tb_mail_file (m_id, a_id, oldname, newname, dir, create_time) " + "VALUES ('"+vo.getM_id()+"', '"+a_id+"','"+filename+"','"+newfilename+"','"+newfile+"',sysdate)"; jdbcTemplate.update(insert); return findMailUploadFile(vo); } ----------------------------------下载文件----------------------------------------------------------- //control /** * 下载文件 * @param @return 设定文件 * @return RecordView 返回类型 * @throws */ @RequestMapping(value="/downloadMailFile") //Download mail attachments public ResponseEntity<byte[]> downloadMailFile(MailFileUploadDto vo,HttpSession session) throws IOException{ if(vo.getA_id()==null||"".equals(vo.getA_id())){ throw new BusinessException("lsh", "参数错误"); } MailFileUploadDto dto = mailService.downloadMA(vo); byte [] body = null; ServletContext servletContext = session.getServletContext(); String newFileName = PropUtils.getProperty("mailUploadPath")+dto.getNewname(); InputStream in = new FileInputStream(newFileName); body = new byte[in.available()]; in.read(body); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(dto.getOldname(),"UTF-8")); HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode); return response; }
springMVC4--文件上传CommonsMultipartFile
猜你喜欢
转载自blog.csdn.net/tane_1018/article/details/78472211
今日推荐
周排行