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

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

1.文件目录

  

2.控制器程序

package com.jun.file.controller;

import com.jun.file.service.UpLoadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; @RestController
public class UpLoadFileController {
@Autowired
private UpLoadFileService upLoadFileService;
@PostMapping("/upload/img")
public String uploadImg(@RequestParam("file") MultipartFile file){
int count=upLoadFileService.upLoadImg(file);
return "success";
} }

 

3.服务方法

package com.jun.file.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import java.io.File;
import java.io.IOException;
import java.util.UUID; /**
* 这个类用于文件的上传
*/
@Service
public class UpLoadFileService {
public int upLoadImg(MultipartFile file){
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
String name = UUID.randomUUID().toString();
fileName = name+suffixName;
String path = "E:/javaImg/test/";
String path_img = path+fileName;
File dest = new File(path_img);
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}

  

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. ipsec][strongswan] ipsec SA创建失败后的错误处理分析

    〇 ike协商的过程最终是为了SA的建立, SA的建立后, 在底层中管理过程,也是相对比较复杂的. 这里边也经常会出现失败的情况. 我们以strongswan为例, 在strongswan的底层SA管 ...

  2. Linux命令——dumpe2fs

    参考:Linux磁盘管理——Ext2文件系统 简介 dumpe2fs用于查询 Ext 家族 superblock以及GDT(Group Descriptor Table,块组描述符表) 信息. 用法 ...

  3. Andrew Ng机器学习 三:Multi-class Classification and Neural Networks

    背景:识别手写数字,给一组数据集ex3data1.mat,,每个样例都为灰度化为20*20像素,也就是每个样例的维度为400,加载这组数据后,我们会有5000*400的矩阵X(5000个样例),会有5 ...

  4. 史诗级干货-python爬虫之增加CSDN访问量

    史诗级干货-python爬虫之增加CSDN访问量 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  5. django知识点回顾

    1.web应用 本质是基于socket实现的应用程序 浏览器-----------服务器 2.http协议:应用层协议 1.基于TCP协议 2.基于请求响应 3.短连接 4.无状态保存(引入了cook ...

  6. Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'phone' at row 1

    Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'phone ...

  7. 《奋斗吧!菜鸟》第九次团队作业:Beta冲刺

    项目 内容 这个作业属于哪个课程 任课教师链接 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11056511.html 团队名称 奋斗吧!菜鸟 作业学习目标 B ...

  8. 2019-2020-1 20199301《Linux内核原理与分析》第五周作业

    第四章·系统调用的三层机制(上) 本章的重点在于用户态程序如何触发系统调用? 一.用户.内核.中断 IntelX86有四种不同的执行级别.Linux操作系统中只采用了其中的0和3两个特权级别,分别对应 ...

  9. C++中得sort函数的比较函数(第三个参数)为什么要声明为static

    sort中的比较函数compare要声明为静态成员函数或全局函数,不能作为普通成员函数,否则会报错. 因为:非静态成员函数是依赖于具体对象的,而std::sort这类函数是全局的,因此无法再sort中 ...

  10. 写一个python小程序

    在windows环境下进行操作 window+R 输入cmd  创建一个文件夹 mkdir pytxt 创建一个py文件 py.py  用notepad或者记事本等工具进行编辑 或 首先声明pytho ...