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导入的功能都失效了.作为后台开发人员,当然是第一时间打开运行日志排查 ...
随机推荐
- 燕化迷你ACDP程序FEM / BDC
带有BMW FEM / BDC模块的Mini ACDP可通过ICP或OBP模式支持FEM / BDC IMMO键编程.与传统的接线方式相比,它们有什么区别? 方法1:通过其他设备通过焊接进行FEM / ...
- vue 剪切图片上传头像,使用 cropperjs 实现
我使用的是vue,移动端的项目. 官网地址:cropperjs GitHub地址:https://github.com/fengyuanchen/cropperjs/blob/master/READM ...
- Monkey初步使用
版权声明: 本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有. 允许有条件转载,转载请附带底部二维码. 一.什么是Monkey Monkey是Android自身提供的,可以通 ...
- epplus动态合并列数据
private static void SetColMerge(DataTable tb, ExcelWorksheet worksheet, string dtColumnName) { KeyVa ...
- D. Make a Permutation!(思维)
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 2016 ICPC 大连网络赛 部分题解
先讲1007,有m个人,n种石头,将n种石头分给m个人,每两个人之间要么是朋友关系,要么是敌人关系,朋友的话他们必须有一种相同颜色的石头,敌人的话他们必须所有石头的颜色都不相同.另外,一个人可以不拥有 ...
- PHP-配置MySQL
安装mysql 修改PHP配置文件 修改php安装路径下 php.ini extension=php_mysqli.dll 在代码路径下添加php文件,在里面编辑 <?php phpinfo() ...
- linux shell 之流程控制 if if else while
(1)流程控制不可以为空: (2)if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi 条件 ...
- ai笔记
- 安装ubuntu是所需的引导
title Install Ubuntu root (hd0,0) kernel (hd0,0)/vmlinuz boot=casper iso-scan/filename=/ubuntu-16.04 ...