图片上传

来源:互联网 发布:色诺芬经济金融数据库 编辑:程序博客网 时间:2024/05/22 02:29

新手,图片上传功能的完整方法链

control层:

 @RequestMapping("uploadImage.do")        public String importExcel(@RequestParam MultipartFile uploadfile,                 HttpServletRequest request, HttpServletResponse response,String id) {        //获取输入的文件名            String fileName = uploadfile.getOriginalFilename();            if (StringUtils.isEmpty(fileName)) {                System.out.println("未上传文件名为空");            }            //获取路径            String tempPath = request.getSession().getServletContext().getRealPath("/")                    + "upload\\ysdwanimal";            File dir = new File(tempPath);                     if (!tempPath.endsWith(File.separator)) {                tempPath = tempPath + File.separator;            }            if (!dir.exists()) {                dir.mkdirs();            }                       System.out.println(tempPath+fileName);            //检查是否重复上传            YsdwAnimal ysdwAnimal = ysdwAnimalService.getEntryById(id);            if(ysdwAnimal == null){            return "uploadfail";            }            //重写数据库储存路径            String lj=request.getScheme() + "://" + request.getServerName()    + ":" + request.getServerPort() + "/whzhly/upload/ysdwanimal/"+id+".jpg";            //保存路径            ysdwAnimal.setLujing(lj);            ysdwAnimalService.saveOrUpdateYsdwAnimal(ysdwAnimal);            //获取完整的图片名            String newFile = tempPath + id+".jpg";                     File file = new File(newFile);            //图片复制到指定文件夹中,实现路径            try {                FileCopyUtils.copy(uploadfile.getBytes(), file);                                System.out.println("成功上传");            } catch (IOException e) {                e.printStackTrace();                return "uploadfail";            }             return "uploadsuccess";        }}

0 0