package org.springboot.controller;

 import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springboot.constant.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import java.io.File;
import java.io.IOException; /**
* @Auther:GongXingRui
* @Date:2018/12/25
* @Description: 文件下载
**/
@Controller
public class FileUpload {
Logger logger = LogManager.getLogger(FileUpload.class); @GetMapping("/upload")
public String upload() {
return "upload";
} @PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
} String fileName = file.getOriginalFilename();
// String filePath = "C:/Temp/upload";
String filePath = Constant.CONFIG_PROPERTIES.getProperty("upload.path");
File dest = new File(filePath + File.separator + fileName);
//判断文件是否已经存在
if (dest.exists()) {
logger.info("文件已经存在 -> " + dest.getPath());
}
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
logger.info("目录不存在,自动创建 -> " + dest.getParentFile().getPath());
}
try {
file.transferTo(dest);
logger.info("上传成功");
return "上传成功";
} catch (IOException e) {
logger.error(e.toString(), e);
}
return "上传失败!";
}
}

SpringBoot之文件上传的更多相关文章

  1. SpringBoot图文教程4—SpringBoot 实现文件上传下载

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...

  2. SpringBoot 整合文件上传 elment Ui 上传组件

    SpringBoot 整合文件上传 elment Ui 上传组件 本文章记录 自己学习使用 侵权必删! 前端代码 博主最近在学 elment Ui 所以 前端使用 elmentUi 的 upload ...

  3. springboot+web文件上传和下载

    一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...

  4. SpringBoot(3) 文件上传和访问

    springboot文件上传 MultipartFile file,源自SpringMVC MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutS ...

  5. SpringBoot的文件上传

    先在src/main/resources下新建一个static目录用以存放html页面,简单的html页面如下 <!DOCTYPE html> <html> <head& ...

  6. springBoot的文件上传功能

    知识点: 后台:将上传的图片写入指定服务器路径,保存起来,返回上传后的图片路径(在springBoot中,参考博客:http://blog.csdn.net/change_on/article/det ...

  7. SpringBoot下文件上传与下载的实现

    原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...

  8. Angular14 利用Angular2实现文件上传的前端、利用springBoot实现文件上传的后台、跨域问题

    一.angular2实现文件上传前端 Angular2使用ng2-file-upload上传文件,Angular2中有两个比较好用的上传文件的第三方库,一个是ng2-file-upload,一个是ng ...

  9. springboot 修改文件上传大小限制

    springboot 1.5.9文件上传大小限制spring:http:multipart:maxFileSize:50MbmaxRequestSize:50Mb springboot 2.0文件上传 ...

  10. SpringBoot实现文件上传

    前言参考:快速开发第一个SpringBoot应用 这篇文章会讲解如何使用SpringBoot完成一个文件上传的过程,并且附带一些SpringBoot开发中需要注意的地方 首先我们写一个文件上传的htm ...

随机推荐

  1. 联想Y700安装显卡驱动和CUDA8.0

    一显卡驱动安装 1 安装驱动安装软件 2 自动检测下载对应版本驱动 3下载完成后,点击开始,解压出来临时安装文件 由于是临时安装文件,退出就会关闭 4所以在没有关闭情况下,打开重新打开一次setup, ...

  2. day25 Python

    一.day24复习 class school: x=1 #__init__初始化函数,用来帮类实例化一个具体的对象 def __init__(self,name,addr): #前面的Name是一个需 ...

  3. day08--文件操作(2)

    一.with open(): 形式:with open('文件路径(文件名)','文件操作方式','字符编码方式')as 文件别名: with open操作可以将文件的内存释放交给with 管理,wi ...

  4. 用return关键字实现1——100累加求和,返回总和并接收输出

    package com.Summer_0419.cn; /** * @author Summer * 用return关键字实现1——100累加求和,返回总和并接收输出 */ public class ...

  5. linux日志:syslogd和klogd及syslog

    一. 日志守护进程 syslogd和klogd是很有意思的守护进程,syslogd是一个分发器,它将接收到的所有日志按照/etc/syslog.conf的配置策略发送到这些日志应该去的地方,当然也包括 ...

  6. 将Oracle 12c的某用户数据迁移至OracleXE的用户

    前言:OracleXE全称为oracle database 11g express edition .Oracle Database 11g Express Edition是 Oracle 数据库的免 ...

  7. Visual Studio Package 插件开发之自动生成实体工具(Visual Studio SDK)

    前言 这一篇是VS插件基于Visual Studio SDK扩展开发的,可能有些朋友看到[生成实体]心里可能会暗想,T4模板都可以做了.动软不是已经做了么.不就是读库保存文件到指定路径么…… 我希望做 ...

  8. Hadoop重新格式化HDFS的方法

    1.查看hdfs-site.xml: <property> <name>dfs.name.dir</name> <value>/home/hadoop/ ...

  9. .net core2 笔记

    资源: https://github.com/aspnet/home https://github.com/dotnet/cli https://www.cnblogs.com/billyang/p/ ...

  10. Centos7 下SVN迁移

    SVN迁移需要做如下操作: 1. 将原来的Repository导出 . #svnadmin dump 原有repos的目录路径 > dumpfile (不同服务器安装目录不同,根据具体情况调整) ...