springmvc 上传文件
2015-09-28 11:16
561 查看
<pre name="code" class="java">myFile 是form表单 file input框 的name属性值; <input type="file" name="myFile"> @RequestMapping("saveCategory") public String saveCategory(Model model,MedDrugCategory medDrugCategory,HttpServletRequest request,@RequestParam MultipartFile myFile,RedirectAttributes redirectAttributes){ String fileAllName=null; try { InputStream is= myFile.getInputStream(); String fileName=String.valueOf(new Date().getTime());//文件名 时间戳 String nameFile=myFile.getOriginalFilename(); String fileType=nameFile.substring(nameFile.lastIndexOf("."), nameFile.length());//文件类型 fileAllName=fileName+fileType; String path=request.getSession().getServletContext().getRealPath("/")+"image"; File file=new File(path); if(!file.isDirectory() && !file.exists()){ file.mkdirs(); } File saveFile=new File(path+"/"+fileAllName); FileUtils.copyInputStreamToFile(is, saveFile); } catch (Exception e) { e.printStackTrace(); } if(fileAllName != null){ medDrugCategory.setImgUrl("/image/"+fileAllName); } meDrugCategoryService.saveDrugCategory(medDrugCategory); redirectAttributes.addFlashAttribute("messages","数据修改成功!"); return "redirect:"+Global.getAdminPath()+"/medical/symptom/categoryList"; }
相关文章推荐
- JAVA实现json和对象间的相互转换,支持复杂对象格式
- java获取客户端请求IP地址 获取公网ip
- java定时器
- spring3.2.2整合hibernate4.3.8final出错
- Spring Batch Tasklet example(二)
- 解决eclipse快捷方式无法启动
- Win7下安装,配置Java, Ant,Maven
- Java中的23种设计模式
- 类的加载机制和反射
- vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值 ...
- javaweb导入项目名修改
- struts2中的各种各样的内建校验器的小例子
- 单向链表(singleLinkedList) java实现
- java学习笔记--随记
- java设计模式之适配器模式
- spring进阶二
- 【转】java数据库操作
- 【springmvc+mybatis项目实战】杰信商贸-28.POI百万数据打印
- 用java来实现线程之间的wait、notify()通信
- eclipse环境搭建(JDK+虚拟机)