标签:获取 osb sep ddp apt mlu ace ddl n+1
1..文件上传
在springmvc.xml中配置文件上传解析器
<!-- 上传图片配置实现类,id必须为这个 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 上传图片的大小 B 5M 1*1024*1024*5--> <property name="maxUploadSize" value="5000000"/> </bean>
方法一:
@RequestMapping("updateItem")
public String updateItemById(Item item, MultipartFile pictureFile) throws Exception {
// 图片上传
// 设置图片名称,不能重复,可以使用uuid
String picName = UUID.randomUUID().toString();
// 获取文件名
String oriName = pictureFile.getOriginalFilename();
// 获取图片后缀
String extName = oriName.substring(oriName.lastIndexOf("."));
// 开始上传
pictureFile.transferTo(new File("C:/upload/image/" + picName + extName));
// 设置图片名到商品中
item.setPic(picName + extName);
// ---------------------------------------------
// 更新商品
this.itemService.updateItemById(item);
return "forward:/itemEdit.action";
}
方法二:
// public ModelAndView updateItemById(Items items){ @RequestMapping(value = "/updateitem.action") public String updateItem(QueryVo vo, MultipartFile pictureFile) throws Exception { // 产生32位随机数并去掉- String name = UUID.randomUUID().toString().replaceAll("-", ""); // jpg 获取文件拓展名 String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename()); // 保存文件,用UUID产生的唯一名字保存 pictureFile.transferTo(new File("F:\\upload\\" + name + "." + ext)); vo.getItems().setPic(name + "." + ext); // 修改 vo.getItems().setCreatetime(new Date()); itemService.updateItemsById(vo.getItems()); // ModelAndView mav = new ModelAndView(); // mav.setViewName("success"); return "redirect:/itemEdit.action?id=" + vo.getItems().getId(); }
----------------------------------------------------------------------------------------文件下载处理(引用另一篇博客的)--------------------------------------------------------------------------------------
@RequestMapping("file") @Controller public class FileController { /** * 文件上传功能 * @param file * @return * @throws IOException */ @RequestMapping(value="/upload",method=RequestMethod.POST) @ResponseBody public String upload(MultipartFile file,HttpServletRequest request) throws IOException{ String path = request.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); File dir = new File(path,fileName); if(!dir.exists()){ dir.mkdirs(); } //MultipartFile自带的解析方法 file.transferTo(dir); return "ok!"; } /** * 文件下载功能 * @param request * @param response * @throws Exception */ @RequestMapping("/down") public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{ //模拟文件,myfile.txt为需要下载的文件 String fileName = request.getSession().getServletContext().getRealPath("upload")+"/myfile.txt"; //获取输入流 InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); //假如以中文名下载的话 String filename = "下载文件.txt"; //转码,免得文件名中文乱码 filename = URLEncoder.encode(filename,"UTF-8"); //设置文件下载头 response.addHeader("Content-Disposition", "attachment;filename=" + filename); //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 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(); } }
标签:获取 osb sep ddp apt mlu ace ddl n+1
原文地址:http://www.cnblogs.com/qlqwjy/p/7246417.html