SpringBoot上传文件
1.pom文件
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.company</groupId>
- <artifactId>uploaddemo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>uploaddemo</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- <relativePath />
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
2.Springboot项目
2.1 application.properties
- server.port=8000
- spring.http.multipart.maxFileSize=10Mb
- spring.http.multipart.maxRequestSize=10Mb
2.2 App.java
- package com.company.uploaddemo;
- import org.springframework.boot.Banner;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- /**
- * Hello world!
- *
- */
- @SpringBootApplication
- public class App {
- public static void main(String[] args) {
- SpringApplication app = new SpringApplication(App.class);
- // 关闭banner
- app.setBannerMode(Banner.Mode.OFF);
- app.run(args);
- }
- }
2.3 Controller
- package com.company.uploaddemo;
- import java.io.File;
- import java.io.IOException;
- import org.springframework.util.FileCopyUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- @RestController
- public class FileUploadController {
- /**
- * 上传文件 测试方法: 测试接口:http://localhost:8000/upload 使用PostMan测试
- *
- * @param file 待上传文件
- * @return
- */
- @RequestMapping(value = "/upload", method = RequestMethod.POST)
- @ResponseBody
- public String handleFileUpload(@RequestParam(value = "file", required = true) MultipartFile file) {
- File fileToSave = null;
- if (!file.isEmpty()) {
- try {
- byte[] bytes = file.getBytes();
- // fileToSave = new File(file.getOriginalFilename());
- fileToSave = new File("C:\\" + file.getOriginalFilename());
- // 自定义存储路径
- FileCopyUtils.copy(bytes, fileToSave);
- } catch (IOException e) {
- e.printStackTrace();
- return "上传失败," + e.getMessage();
- }
- return "上传成功" + fileToSave.getAbsolutePath();
- } else {
- return "上传失败,因为文件是空的.";
- }
- }
- }
SpringBoot上传文件的更多相关文章
- springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...
- springBoot上传文件时MultipartFile报空问题解决方法
springBoot上传文件时MultipartFile报空问题解决方法 1.问题描述: 之前用spring MVC,转成spring boot之后发现上传不能用.网上参考说是spring boot已 ...
- SpringBoot上传文件到本服务器 目录与jar包同级问题
目录 前言 原因 实现 不要忘记 最后的封装 Follow up 前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传 ...
- SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案
SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...
- SpringBoot上传文件到本服务器 目录与jar包同级
前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了. 当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你 ...
- spring-boot上传文件MultiPartFile获取不到文件问题解决
1.现象是在spring-boot里加入commons-fileupload jar并且配置了mutilPart的bean,在upload的POST请求后,发现 multipartRequest.ge ...
- SpringBoot上传文件临时失效问题
线上的系统中不能上传文件了,出现如下错误: org.springframework.web.multipart.MultipartException: Could not parse multipar ...
- springboot上传文件过大,全局异常捕获,客户端没有返回值
最近在项目里进行全局异常处理时,上传文件超过配置大小,异常被捕获,但是接口直接报500错误,且没有任何返回值. 从后台报错日志来看,异常已经被全局异常处理捕获到了,并且也已经完成响应,为什么前端看不到 ...
- Springboot上传文件临时目录无效
一个奇葩问题,虽然解决了,但还是没弄清楚,小记一笔. 年后回来,测试人员对年前的3次迭代的功能进行了回归测试,然后发现所有excel导入的功能都失效了.作为后台开发人员,当然是第一时间打开运行日志排查 ...
随机推荐
- 【原创】LUOGU P1808 单词分类
STL大法好!!! 使用sort()将string排序,map去重并统计即可. 最短代码如下: #include<bits/stdc++.h> using namespace std; s ...
- 2019牛客暑期多校训练营(第三场)F 单调队列
题意 给一个\(n\times n\)的矩阵,找一个最大的子矩阵使其中最大值与最小值的差小于等于\(m\). 分析 枚举子矩阵的上下边界,同时记录每一列的最大值和最小值. 然后枚举右边界,同时用两个单 ...
- nginx 配置文件 2019-12-20
cat /etc/nginx/nginx.conf user nginx; worker_processes ; error_log /var/log/nginx/error.log warn; p ...
- Linux网络编程三、 IO操作
当从一个文件描述符进行读写操作时,accept.read.write这些函数会阻塞I/O.在这种会阻塞I/O的操作好处是不会占用cpu宝贵的时间片,但是如果需要对多个描述符操作时,阻塞会使同一时刻只能 ...
- JavaWeb_(Hibernate框架)Hibernate与c3p0与Dbutils的区别
JavaWeb_(Hibernate框架)使用Hibernate开发用户注册功能 传送门 JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能 传送门 Hiberna ...
- 记一次期待已久的渗透 从phpcms到thinkphp
0X01 前言 这是刚刚开始学习渗透的一个目标吧 这个站从刚开始学的那一天起,就想把他日下来. 可能是自己的信息收集能力太差了吧,导致一直无从下手 没有进展.这是需要慢慢积累的过程.还需努力学习. 0 ...
- js获取高度和宽度
CreateTime--2017年7月24日10:15:47Author:Marydon js获取高度和宽度 参考连接:http://www.cnblogs.com/EasonJim/p/6229 ...
- git 出现错误 Could not resolve host: github.com 或者 gitlab.com 或者gerrit相关( 自有服务 )
原来是因为github.com没有被主机给解析 1.第一步是 ping 你的gitlab 或者 github服务器ip地址 如果每隔几秒有 time = xx.ms 刷新 就证明是通的 2. 编辑 e ...
- scrapy pipeline
pipeline的四个方法 @classmethod def from_crawler(cls, crawler): """ 初始化的时候,用以创建pipeline对象 ...
- logback条件日志配置
logback支持条件日志配置,支持在测试环境和正式环境使用不同的参数启用不同的日志配置,从而避免手动修改日志配置文件.项目除了引入logback的包以外,还需要引入构件org.codehaus.ja ...