关于文件上传,在spring cloud会再经过配置文件的处理,在spring boot则不需要,在这里写一个文件上传的接口。

  单文件上传,如果以后写多文件上传再进行补充。

1.文件目录

  

2.控制器程序

  1. package com.jun.file.controller;
  2.  
  3. import com.jun.file.service.UpLoadFileService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.multipart.MultipartFile;
  9.  
  10. @RestController
  11. public class UpLoadFileController {
  12. @Autowired
  13. private UpLoadFileService upLoadFileService;
  14. @PostMapping("/upload/img")
  15. public String uploadImg(@RequestParam("file") MultipartFile file){
  16. int count=upLoadFileService.upLoadImg(file);
  17. return "success";
  18. }
  19.  
  20. }

 

3.服务方法

  1. package com.jun.file.service;
  2.  
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.web.multipart.MultipartFile;
  5.  
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.UUID;
  9.  
  10. /**
  11. * 这个类用于文件的上传
  12. */
  13. @Service
  14. public class UpLoadFileService {
  15. public int upLoadImg(MultipartFile file){
  16. String fileName = file.getOriginalFilename();
  17. String suffixName = fileName.substring(fileName.lastIndexOf("."));
  18. String name = UUID.randomUUID().toString();
  19. fileName = name+suffixName;
  20. String path = "E:/javaImg/test/";
  21. String path_img = path+fileName;
  22. File dest = new File(path_img);
  23. if(!dest.getParentFile().exists()){
  24. dest.getParentFile().mkdirs();
  25. }
  26. try {
  27. file.transferTo(dest);
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. return 0;
  32. }
  33. }

  

4.测试

  

 

004 springboot文件上传的更多相关文章

  1. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  2. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  3. SpringBoot 文件上传临时文件路径问题

    年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...

  4. springboot文件上传下载简单使用

    springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...

  5. springboot 文件上传大小配置

    转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...

  6. SpringBoot文件上传下载

    项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...

  7. Springboot 文件上传(带进度条)

    1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  8. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  9. SpringBoot文件上传异常之提示The temporary upload location xxx is not valid

    原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...

随机推荐

  1. 【转】C语言宏定义的几个坑和特殊用法

    总结一下C语言中宏的一些特殊用法和几个容易踩的坑.由于本文主要参考GCC文档,某些细节(如宏参数中的空格是否处理之类)在别的编译器可能有细微差别,请参考相应文档. 宏基础 宏仅仅是在C预处理阶段的一种 ...

  2. C实现除法

    C实现除法 来源 Leetcode上的一个题,做完后感觉很有意义,因而记录. 实际上自己也查阅了不少的实现除法的方式,最后还是感觉这个方法是最好的,没有别的原因,就是快. 需要注意的一些点 正整数之间 ...

  3. yum源仓库的三种搭建方式

    yum源的三种搭建方式 一.  本地yum仓库的搭建 1.1.获取软件包资源 将iso镜像挂载在本地目录中,此次挂载目录为/var/www/html/repo/,此目录本身不存在,需要创建.软件宝资源 ...

  4. java计算两个经纬度之间的距离

    /** * 计算点 是否在一个固定点的半径范围内 * @2016年10月20日 * @param a 经度1 已知 * @param b 纬度1 已知 * @param x 经度2 * @param ...

  5. 堆(python)

    # -*- coding:utf-8 -*- class Array(object): def __init__(self, size=32): self._size = size self._ite ...

  6. 转 C#关于DateTime得到的当前时间的格式和用法

    DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.T ...

  7. (java)selenium webdriver爬虫学习--爬取阿里指数网站的每个分类的top50 相关数据;

    主题:java 爬虫--爬取'阿里指数'网站的每个分类的top50 相关数据: 网站网址为:http://index.1688.com/alizs/top.htm?curType=offer& ...

  8. vs2010出现红色波浪错误提示但运行通过

    1.环境:xp+VS2010 2.问题:突然发现编辑器里的代码在没有错误的地方提示很多不存在的错误,显示红色波浪线,但能顺利编译运行. 3.解决: 对于32位机来说,先查看注册表: [HKEY_CLA ...

  9. myaliyun ECS 启动内外穿透工具shell脚本

    #!/bin/bash tbp=/usr/local/ngrok cmd=$1 if [[ -z $cmd ]];then echo "err!usage{sh $0 1|0 to star ...

  10. 布鲁克斯法则 (Brooks's Law)

    软件开发后期,添加人力只会使项目开发得更慢. 这个定律表明,在许多情况下,试图通过增加人力来加速延期项目的交付,将会使项目交付得更晚.布鲁克斯也明白,这是一种过度简化.但一般的推理是,新资源的增加时间 ...