/** * 加载单个功能信息 * * @return */ public String edit() { // 刷新系统菜单 refreshPageData(); category = new CategoryVO(); try { if (null != id && !"".equals(id.trim())) { Category categoryDB = categoryService.getCategoryById(Long.parseLong(id)); category.setId(categoryDB.getId()); category.setName(categoryDB.getName()); category.setPicPath(categoryDB.getPicPath()); category.setName(categoryDB.getName()); } else { this.setMessage("ERROR"); return ERROR; } } catch (CategoryNotFoundException e) { this.setMessage(e.getMessage()); return ERROR; } return SUCCESS; }
/** * 上传图片 * * @return * @throws Exception */ public String uploadImage() { // 得到当前时间自1970年1月1日0时0分0秒开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名。 long now = new Date().getTime(); // 得到保存上传文件的目录的真实路径 String path = ServletActionContext.getServletContext().getRealPath(uploadDir); File dir = new File(path); // 如果这个目录不存在,则创建它。 if (!dir.exists()) { dir.mkdir(); } if (null != fileFileName && !"".equals(fileFileName.trim())) { int index = fileFileName.lastIndexOf('.'); // 判断上传文件名是否有扩展名 if (index != -1) { newFileName = now + fileFileName.substring(index); } else { newFileName = Long.toString(now); } this.setNewFileName(newFileName); BufferedOutputStream bos = null; BufferedInputStream bis = null; // 读取保存在临时目录下的上传文件,写入到新的文件中。 try { FileInputStream fis = new FileInputStream(file); bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(new File(dir, newFileName)); bos = new BufferedOutputStream(fos); byte[] buf = new byte[4096]; int len = -1; while ((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); } } catch (FileNotFoundException e) { this.setMessage(MessageUtil.getMessage("file_not_found_fxception", fileFileName)); return INPUT; } catch (IOException e) { this.setMessage(MessageUtil.getMessage("io_exception", newFileName)); return INPUT; } finally { refreshPageData(); try { if (null != bis) { bis.close(); } if (null != bos) { bos.close(); } } catch (IOException e) { this.setMessage(MessageUtil.getMessage("io_exception", newFileName)); return INPUT; } } } if (null == category) { category = new CategoryVO(); } if (null != category) { category.setName(categoryName); if (null != categoryId) { category.setId(categoryId); setId(categoryId.toString()); } category.setPicPath("/dish/" + newFileName); } this.setMessage(MessageUtil.getMessage("upload_picture_success_msg")); return SUCCESS; }