springboot图片上传

  • 图片上传
    • 1.技术
    • 2.前端
    • 3.js
    • 4.工具类
    • 5.controller
    • 6.补充说明
    • 7.遗留问题

图片上传

1.技术

springboot+jquery

2.前端

<div class="form-group">    <label class="col-sm-3 control-label">头像:</label><div class="col-sm-8"><input id="photo" name="photo" class="form-control" type="file"></div>
</div>

3.js

 var photo=$("#photo").val();if(photo!=null && photo!=""){// 构建数据var data = new FormData()data.append('name', $('[name=photo]').val())data.append('file', $('[name=photo]')[0].files[0]) // file 对象$.ajax({cache : false,type : "POST",url : "/upload",data :data,processData: false,//数据不被转换为字符串contentType: false,//上传文件时使用,避免 JQuery 对其操作async : false,dataType:"json",error : function(request) {},success : function(data) {if (data.code == 0) {photo = data.fileName;} }});}

4.工具类

package com.bootdo.common.utils;import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;public class FileUtil {public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {File targetFile = new File(filePath);if (!targetFile.exists()) {targetFile.mkdirs();}FileOutputStream out = new FileOutputStream(filePath + fileName);out.write(file);out.flush();out.close();}public static boolean deleteFile(String fileName) {File file = new File(fileName);// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除if (file.exists() && file.isFile()) {if (file.delete()) {return true;} else {return false;}} else {return false;}}public static String renameToUUID(String fileName) {return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);}
}

5.controller

package com.bootdo.util;import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Controller
@RequestMapping("")
public class uploadController extends BaseController {private String pathName="D://springboot//tomcat//upload/";@ResponseBody@PostMapping("/upload")R upload(MultipartFile file, HttpServletRequest request) {String fileName = file.getOriginalFilename();fileName = FileUtil.renameToUUID(fileName);try {FileUtil.uploadFile(file.getBytes(), pathName, fileName);return R.ok().put("fileName","/upload/"+fileName);} catch (Exception e) {e.printStackTrace();}return R.error();}
}

6.补充说明

ps:文件之前是放在tomcat临时文件下的,但是每次服务重启就不是之前的tomcat临时文件了,所以之前上传到服务器的图片,再查找路径就不对,就会找不到图片,所以,后来改成放在本地路径,并把资源映射改到本地路径:

package com.bootdo;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/**
* 资源映射路径
*/
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/springboot/tomcat/upload/");}
}

ps:添加之后想让图片显示在列表:
获取url路径

var href = location.host;
if(href.indexOf("http://")<0 && href.indexOf("https://")<0){href="http://"+href;
}

bootstrapTable放formatter

{field : 'photo', title : '头像' ,formatter : function(value, row, index) {if(value!=null && value!= ""){return "<img style='width: 70px;height: 30px;' src='"+href+value+"' alt=''>"}}
},

7.遗留问题

还有一个大问题,点击修改的时候如何给文件赋值,等我再找找吧

bootdo图片上传相关推荐

  1. html显示数据库图片django,django将图片上传数据库后在前端显式的方法

    1.使用ImageField先安装pillow模块 pip install pillow 2.在app的models中设置 class Image(models.Model): pic_name=mo ...

  2. yii2框架原生的结合框架使用的图片上传

    首先我们要从model层开始写起,主要是为了创建验证规则,还有图片上传的路径以及图片的命名规则(UploadForm.php) 接下来我们要在控制器层写好业务逻辑,就是什么情况下直接在调用model层 ...

  3. java flex 图片上传_flex上传图片到java服务器

    今天弄flex上传图片到java,现在弄成功,中间也经常一点小波折,现记录一下.重点在java侧的实现. flex侧:文件上载到在url参数中传递的URL.该URL必须是配置为接受上载的服务器脚本.F ...

  4. js表单提交,支持图片上传,包含后端php代码

    微信小程序开发交流qq群   581478349    承接微信小程序开发.扫码加微信. <html><head><meta http-equiv="Conte ...

  5. ckeditor finder php,CKEDITOR CKFINDER的图片上传配置(C#/asp.net/php)

    CKEDITOR+CKFINDER的图片上传配置(C#/asp教程.net/php教程) php keditor的代码全部重写,但里面没有了上传功能,只是一个纯粹的文件在线编辑器,如果需要上传图片,还 ...

  6. 图片上传(加水印、缩略图、远程保存)的简单例子

    图片上传(加水印.缩略图.远程保存)的简单例子(应用于51aspx.com) 该源码下载地址:http://51aspx.com/CV/ImageUpload 今天看到xiongeee发的文章使用使用 ...

  7. 使用summernote实现复制图片即可实现图片上传

    个人资源与分享网站:http://xiaocaoshare.com/ 1.页面结构 <script src="../statics/js/plugins/summernote/summ ...

  8. js实现图片上传本地预览

    演示地址:https://xibushijie.github.io/static/uploadImg.html <!DOCTYPE> <html><head>< ...

  9. SpringBoot 2.0 多图片上传加回显

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 本文来源:http://r6f.cn/crEY 这两天公司 ...

  10. alert()的功能_前端实现简单的图片上传小图预览功能

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

最新文章

  1. java 热替换 匿名类_Java 类的热替换
  2. idea设置打开文件窗口个数
  3. python循环写入csv文件_Python3.5想把抓到的股票信息以循环方式存入到csv文件中怎么做...
  4. 程序员面试不完全指南
  5. Zookeeper和kafka在linux环境下的安装
  6. 机器学习(六)——SVM(4)、学习理论
  7. 转:org.apache.maven.archiver.MavenArchiver.getManifest错误
  8. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)
  9. php 读写远程文件内容,php获取远程文件内容的函数
  10. 华为Android10版怎么截屏,安卓手机截图方法 华为手机如何截图 - 云骑士一键重装系统...
  11. 洛谷P1217回文质数
  12. html5 星际摩托,HTML5 星际陨石环绕动效
  13. flymcu烧录stm32L151问题
  14. 2010.11.03_ximo_过VMP加壳程序的自效验(vmp 2.06)
  15. c# xaml语言教程,Xamarin XAML语言教程XAML文件结构与解析XAML
  16. Xcode13.3 13.2以及Flutter新版本的稳定性问题
  17. java增大字體_往JRE里增加字体
  18. 【让你从0到1学会C语言】指针/数组传参以及static关键字
  19. 念悠文化:微博运营怎么做?微博运营的几个方法
  20. ssl证书是什么,ssl证书有什么作用

热门文章

  1. Python简单实现图书管理系统
  2. ccs6.0 破解版安装教程 Code Composer Studio安装教程
  3. 信息安全技术网络安全等级保护基本要求
  4. Powerbuilder连接互联网数据库:DataWindowHTTP(dwhttp)
  5. Linux多线程编程
  6. php滑动门效果,js实现简洁的TAB滑动门效果代码
  7. 麦咖啡企业版McAfee VirusScan Enterprise v8.8授权版
  8. ORB-SLAM2代码详解
  9. windows中文件夹打包成Jar包 cmd命令
  10. 使用快捷工具搜狗词库转txt和mmseg