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. 前向星&链式前向星

    参考博文: https://blog.csdn.net/acdreamers/article/details/16902023 前向星 len[i]以i为起点的边在数组中的存储长度 head[i]以i ...

  2. 环境部署(八):jenkins配置邮件通知

    完成基于jenkins的持续集成部署后,任务构建执行完成,测试结果需要通知到相关人员.这篇博客,介绍如何在jenkins中配置邮件通知的方法... 一.安装邮件插件 由于Jenkins自带的邮件功能比 ...

  3. 在Sublime中配置JsFormat

    JsFormat配置文件: { // exposed jsbeautifier options "indent_with_tabs": false, // 保留换行符 " ...

  4. js超时处理

    var now = new Date(); var exitTime = now.getTime() + numberMillis; //numberMillis毫秒超时参数 while (true) ...

  5. linux内存源码分析 - 零散知识点

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 直接内存回收中的等待队列 内存回收详解见linux内存源码分析 - 内存回收(整体流程),在直接内存回收过程中, ...

  6. 第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约

    智能合约是以太坊的核心之一,用户可以利用智能合约实现更灵活的代币以及其他DApp.不过在深入讲解如何开发智能合约之前,需要先介绍一下以太坊中用于开发智能合约的Solidity语言,以及相关的开发和测试 ...

  7. [C#]实现任何数据库类型的DbHelper帮助类

    本文章为原创内容,如需转载,请注明作者及出处,谢谢! 一.在System.Data.Common命名空间下,存在这样的一个类: // // 摘要: // 表示一组方法,这些方法用于创建提供程序对数据源 ...

  8. Java类的加载及实例的创建

    java中class.forName()和classLoader都可用来对类进行加载.class.forName()前者除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的sta ...

  9. flask 更新数据库

    在做项目的过程中,我们都遇到过,经常需要修改我们数据库的字段,在flask中,是通过ORM(对象关系映射)来创建数据库的,表--->model class,字段---->属性 在flask ...

  10. Python_匿名函数_47

    匿名函数 Eva_J 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 #这段代码 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc ...