关键词:
知识点:SpringBoot2.x文件上传:HTML页面文件上传和后端处理
1、springboot文件上传 MultipartFile file,源自SpringMVC
1)静态页面直接访问:localhost:8080/index.html
注意点:如果想要直接访问html页面,则需要把html放在springboot默认加载的文件夹下面
2)MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效)
访问路径 http://localhost:8080/images/39020dbb-9253-41b9-8ff9-403309ff3f19.jpeg
2、jar包方式运行web项目的文件上传和访问处理,SpingBoot2.x使用 java -jar运行方式的图片上传和访问处理(核心知识)
1)文件大小配置,启动类里面配置
@Bean public MultipartConfigElement multipartConfigElement() MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("10240KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("1024000KB"); return factory.createMultipartConfig();
2)打包成jar包,需要增加maven依赖
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
如果没加相关依赖,执行maven打包,运行后会报错:no main manifest attribute, in XXX.jar
GUI:反编译工具,作用就是用于把class文件转换成java文件
3)文件上传和访问需要指定磁盘路径
application.properties中增加下面配置
a) web.images-path=/Users/aaron/Desktop
b) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:$web.upload-path
4)文件服务器:fastdfs,阿里云oss,nginx搭建一个简单的文件服务器
一、目录结构
二、文件上传需求
1.前端
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>uploading</title> <meta name="keywords" content="keyword1,keyword2,keyword3"></meta> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="/js/test.js" type="text/javascript"></script> </head> <body> <form enctype="multipart/form-data" method="post" action="/fileController/upload"> 文件:<input type="file" name="head_img" /> 姓名:<input type="text" name="name" /> <input type="submit" value="上传" /> </form> </body> </html>
2.文件上传Controller
package net.aaron.demo.controller; import java.io.File; import java.io.IOException; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import net.aaron.demo.domain.JsonData; @Controller @RequestMapping(value = "fileController") @PropertySource(value="classpath:application.properties") public class FileController @Value("$web.filePath") private String filePath; @RequestMapping(value = "upload") @ResponseBody public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) //file.isEmpty(); 判断图片是否为空 //file.getSize(); 图片大小进行判断 System.out.println("配置注入打印,文件路径为:"+filePath); String name = request.getParameter("name"); System.out.println("用户名:"+name); // 获取文件名 String fileName = file.getOriginalFilename(); System.out.println("上传的文件名为:" + fileName); // 获取文件的后缀名,比如图片的jpeg,png // lastIndexOf 是从右向左查某个指定的字符串在字符串中最后一次出现的位置 String suffixName = fileName.substring(fileName.lastIndexOf(".")); System.out.println("上传的后缀名为:" + suffixName); // 文件上传后的路径 fileName = UUID.randomUUID() + suffixName; System.out.println("转换后的名称:"+fileName); File dest = new File(filePath + fileName); try file.transferTo(dest); return new JsonData(0, fileName); catch (IllegalStateException e) // TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) // TODO Auto-generated catch block e.printStackTrace(); return new JsonData(-1, "fail to save ", null);
3.返回JsonData类
package net.aaron.demo.domain; import java.io.Serializable; public class JsonData implements Serializable /** * */ private static final long serialVersionUID = 1L; //状态码,0表示成功,-1表示失败 private int code; //结果 private Object data; //错误描述 private String msg; public JsonData(int code, Object data) super(); this.code = code; this.data = data; public JsonData(int code, String msg,Object data) super(); this.code = code; this.msg = msg; this.data = data;
4.访问:
springboot项目结合fastdfs做文件上传
...为这样做就算fastdfs环境搭建成功了。 不料,通过springboot+fastdfs-client来上传文件却报 查看详情
使用eclipse搭建springboot项目(代码片段)
使用Eclipse搭建SpringBoot项目一、创建项目过程二、加入相关依赖、配置三、常用配置设置四、热部署:即有文件修改保存后,自动重启五、配置Thymeleaf一、创建项目过程01、点击eclipse左上角file—new—other02、选择MavenProject&... 查看详情
eclipse搭建springboot项目单元测试(代码片段)
知识点:Springboot2.0单元测试和自定义异常处理 1、@SpringBootTest单元测试 1)、引入相关依赖<!--springboot程序测试依赖,如果是自动创建项目默认添加--><dependency><groupId>org.springframework.boot</groupId><art... 查看详情
eclipse搭建springboot项目热部署(代码片段)
知识点:SpringBoot2.x使用Dev-tool工具热部署,快速加载启动应用 官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools 核心依赖包:<dependency><groupId>org.spri 查看详情
springboot:浅谈文件上传和访问的坑(multipartfile)(代码片段)
本次的项目环境为SpringBoot2.0.4,JDK8.0.服务器环境为CentOS7.0,Nginx的忘了版本.前言SpringBoot使用MultiPartFile接收来自表单的file文件,然后进行服务器的上传是一个项目最基本的需求,我以前的项目都是基于SpringMVC框架搭建的,所以在使用Spr... 查看详情
eclipse搭建springboot框架(非maven)
无论是eclipse还是idea,创建springBoot项目都很简单,而且网上教程一大把。奈何我我工作的是内网环境,而且没有本地maven仓库,所以只能手工拿取相关jar包,导入到项目中去。下面分享一下过程,虽然没什么技术含量。 ... 查看详情
springboot开发环境搭建
姓名:罗秀群 班级:软件151 第一步:在Eclipse下面配置Maven环境第二步:构建Maven+spring web项目 : 1.打开Eclipse,选择新建MavenProject, 2.然后利用向导分别建立一个webapp项目和quickStart项目 ... 查看详情
react+springboot项目部署到腾讯云
...个人网站,并将项目部署到腾讯云。前端:React,后端:SpringBoot,云服务:centos7建议使用create-react-app创建react项目包,简单快捷。进入项目路径,开始运行直接使用项目自身的build指令进行打包,完成后会在项目根目录多出一个... 查看详情
springboot学习一springboot项目初步搭建
...8+tomcate8.03参考学习地址:http://www.ityouknow.com/spring-boot.htmlspringboot项目可以直接在https://start.spring.io/网上创建项目下载后,通过maven导入项目(项目空白区右键-Import-Import-Maven-ExistingMavenProjects)导入项目包下面有一个 查看详情
springboot项目上传文件报错
SpringBoot项目上传图片报错FileSizeLimitExceededException:Thefielduploadexceedsitsmaximumpermittedsizeof1048576bytes.处理办法:1.application.yml文件添加spring.http.multipart.maxFileSize:3Mbspring.http.multipart.maxRequ 查看详情
在eclipse上搭建springboot
1,具体步骤网上有,需要注意的是,如果是maven项目,需要先下载maven,配置环境变量,再在eclipse-windows--preference--maven,选择usersetting,将usersetting的路径设成你的maven下config的setting.xml,如 D:softwareapache-maven-3.5.4confsettings.xml2,... 查看详情
[读书笔记]springboot项目搭建与配置文件
读书笔记:[JavaEE开发的颠覆者SpringBoot实战]作者:汪云飞从今天开始坚持读书,并记录下此读书笔记。一,初接触Springboot项目Helloworld搭建1.pom.xml配置如下:<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst 查看详情
springboot项目搭建
一、创建maven聚合工程 这个比较简单,就不在此赘述了。 给出结构图: 二、配置pom文件 1.配置setting文件(这个不是本文重点,也就先不多说了) 2.配置pom文件 a.pom文件一般主要需... 查看详情
springboot学习之构建简单项目搭建
...erties文件,构建一个项目还是挺复杂的,在这种情况下,springboot应运而生,他能够快速的构建spring项目,而且让项目正常运行起来的配置文件非常少,甚至只需要几个注解就可以运行整个项目。 总的说来,springboot项目可以... 查看详情
springboot入门(项目搭建及基本配置)(代码片段)
...目1、先创建一个maven项目删除不用的文件2、再创建一个springboot项目二、SpringBoot–配置属性1、SpringBoot的配置SpringBoot使用一个固定文件名做为全局的配置文件,用来修改SpringBoot自动 查看详情
springboot1-项目快速搭建(代码片段)
springboot快速搭建方法1、访问http://start.spring.io 方法2、使用SpringToolSuite,我们这里注重讲解这个方法。第一步:首先eclipse->help->eclipsemarketplace->popular然后按照提示一步一步安装STS插件即可。第二步:restarteclipse后,... 查看详情
快速搭建springboot+mybatis+postgresql开发环境(代码片段)
这里,利用eclipse或者idea提供的springboot项目创建向导,不用去找依赖。 普通的eclipse需要安装spring插件。可以直接使用sts版本。全称是SpringToolsSuite。在eclipse下,新建->SpringStarterProject,跟着向导,选择... 查看详情
springboot入门十九,简单文件上传
项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可。现在来给项目添加一个MyBatis支持,添加方式非常简单,仅需两步即可,具体内容如下:1.pom.xml添加以下配置信息<!--文件上... 查看详情