zoukankan      html  css  js  c++  java
  • 上传图片demo

    页面:

    js:

     后台:

    @RequiresPermissions("pointwall:upload:edit")
    	@RequestMapping(value = "saveImage")
    	@ResponseBody
    	public String saveImage(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request)
    			throws IOException {
    		// 上传的图片只允许是 png 或者jpg 中的格式
    		if (file.getOriginalFilename().contains(".png") || file.getOriginalFilename().contains(".jpg")) {
    			// 根据相对路径转化为真实路径
    			String rootpath = request.getSession().getServletContext().getRealPath(File.separator);// 获得web应用的绝对路径
    			File createFile = new File(rootpath + "/image/");
    			if (!createFile.exists()) {// 判断文件是否存在如果不存在则自动创建文件夹
    				createFile.mkdir();
    			}
    			//String uuid = IdGen.uuid() + "_";// 随机生成一个唯一性的id 确保apk文件重名
    			File f = new File(rootpath + "/image/"+ file.getOriginalFilename());
    			if(f.exists()){//上传的文件已经存在,则提示用户重新上传 apk 或者重命名
    				return "文件已经存在,请重新上传或者重命名" ;
    			}
    			else{
    				System.out.println(rootpath);
    				file.transferTo(f); // 将上传的文件写入到系统中
    				return "/image/" + file.getOriginalFilename();
    			}			
    		} else {
    
    			return "上传文件失败";
    		}
    	}
    

      

  • 相关阅读:
    嵌入式开发杂谈
    C#连接数据库
    C软件机密解密之动态跟踪
    navicat连接mysql8报错
    tomcat 服务版本内存设置
    python2/python3 升级pi版本
    各种源文件和目录
    Day 2 : 变量、JAVA基本类型、运算符和表达式1
    猜字母游戏
    Day 1 : 行业概述、JAVA开发环境
  • 原文地址:https://www.cnblogs.com/ipetergo/p/7255563.html
Copyright © 2011-2022 走看看