关键词:
目录结构,文件上传
一、目录结构
1、目录讲解
src/main/java:存放代码
src/main/resources
static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
templates:存放静态页面jsp,html,tpl
config:存放配置文件,application.properties
resources:
2、同个文件的加载顺序,静态资源文件
Spring Boot 默认会挨个从
META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回。
什么意思呢,就是比如你有个index.html文件,springboot默认放在以上文件夹是可以访问到的,而且是按照这个顺序访问。
案例:我在,resources,static ,public ,templates都放一个index.html文件,然后输入:localhost:8080,看访问的是哪个index.html
可以看出:首先访问就是resources里面的index.html
text文件默认是访问不了的,就算你的露筋是localhost:8080/text/index.html也是访问不了的。
不过,你在application.properties配置如下,就可以访问了
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/
二、文件上传
一、war包方式进行上传
springboot文件上传的对象是MultipartFile,它是file的子类,源自
1)静态页面直接访问:localhost:8080/index.html
注意点:
如果想要直接访问html页面,则需要把html放在springboot默认加载的文件夹下面
2)MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效)
案例:在static文件目录下,
upload.html
<!DOCTYPE html> <html> <head> <title>uploadimg.html</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <!--涉及文件上传,这里就需要multipart/form-data--> <form enctype="multipart/form-data" method="post" action="/upload"> 文件:<input type="file" name="head_img"/> 姓名:<input type="text" name="name"/> <input type="submit" value="上传"/> </form> </body> </html>
FileController类
package com.jincou.ocontroller; import java.io.File; import java.io.IOException; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import com.jincou.model.JsonData; 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; @Controller public class FileController { //文件放在项目的images下 private static final String filePath = "C:\\Users\\chenww\\Desktop\\springbootstudy\\springbootstudy\\src\\main\\resources\\static\\images\\"; @RequestMapping(value = "upload") @ResponseBody public JsonData upload(@RequestParam("head_img") MultipartFile file,HttpServletRequest request) { //file.isEmpty(); 判断图片是否为空 //file.getSize(); 图片大小进行判断 String name = request.getParameter("name"); System.out.println("用户名:"+name); // 获取文件名 String fileName = file.getOriginalFilename(); System.out.println("上传的文件名为:" + fileName); // 获取文件的后缀名,比如图片的jpeg,png 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) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //上传失败 return new JsonData(-1, "fail to save ", null); } }
JsonData对象,用来返回状态
package com.jincou.model; 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 int getCode() { return code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public void setCode(int code) { this.code = code; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } 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; } }
效果:
这里面如果你想限制上传问价大小,可以在添加如下:
//文件大小配置,启动类里面配置 @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("10240KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("1024000KB"); return factory.createMultipartConfig(); }
总结:
1.其实在正式项目里,这里的文件上传地址不会是在本项目里,而是会指定一个文件服务器:
常用文件服务器:fastdfs,阿里云oss,nginx搭建一个简单的文件服务器
2.有关单个文件最大值,路径最好是配置在配置文件里,这样后期好维护。
想太多,做太少,中间的落差就是烦恼。想没有烦恼,要么别想,要么多做。上尉【5】
springboot测试简单上传文件
springboot测试简单上传文件先看看目录结构一、引入相应的pom文件<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><d 查看详情
springboot项目上传文件出现临时文件目录为空
最近写文件上传到服务器读取的代码,前端使用FormData上传,服务端用MultipartFile接收,自己测试了下MultipartFile对象有什么东西,结果一般属性都能出来,测试getInputStrea()方法的时候出现了以下错误,简单一看这是什么目录,从... 查看详情
springboot_上传文件至targetstatic目录下并显示到页面
其实springboot中上传文件和在springmvc中上传的方式基本一致,没多大区别,当然在springboot没多少配置,更加简单一点。一、在application.properties中我们只需写上如下两行配置。(其实不写这个也是可以的,只要你的单个文件小于1M)#... 查看详情
springboot整合minio实现文件上传
参考技术A文章目录:MinIO是一个用Golang开发的基于ApacheLicensev2.0源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,适合存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,单个文件... 查看详情
springboot目录文件结构总结(代码片段)
1、目录 src/main/java:存放java代码 src/main/resources static:存放静态文件,比如css、js、image(访问方式http://localhost:8080/js/main.js) templates:存放静态页面jsp,html,tpl config:存放配置文件application.prope... 查看详情
springboot文件上传示例(ajax和rest)
本文介绍如何使用Ajax请求在SpringBootWeb应用程序(REST结构)中上传文件。本文中使用的工具:SpringBoot1.4.3.RELEASESpring4.3.5.RELEASEThymeleafjQuery(webjars)MavenEmbeddedTomcat8.5.6GoogleChrome浏览器1.项目结构一个标准的Maven项目结构。如下图所示-2.项... 查看详情
springboot自带容器的上传文件怎么访问
参考技术ASpringBoot将在类路径中或从ServletContext的根目录中提供名为/static(或/public或/resources或/META-INF/resources)的目录中的静态内容。也就是说默认情况下,可以将静态文件放到static,public,resources,/META-INF/resources四个目录下。... 查看详情
springboot——文件上传
目录单文件上传表单上传ajax上传多文件上传表单上传ajax上传文件上传配置方法一:在配置文件中添加配置方法二:在启动类中添加Bean配置踩坑经验单文件上传表单上传1.前端表单<formaction="/upload"method="post"enctype... 查看详情
springboot文件上传到本地电脑,项目目录,保存到数据库(代码片段)
文件上传三大要素1.表单提交方法用post2.有一个可以选择文件的文本框3.表单属性enctype="multipart/form-data"必须要有1.上传页面<%@pagecontentType="text/html;charset=UTF-8"language="java"%><html 查看详情
关于springboot上传文件报错:thetemporaryuploadlocation***isnotvalid
在运行springboot时,长时间运行后报错thetemporaryuplaodlocation***isnotvalid 查过资料后发现是centos对‘/temp’下文件自动清理的原因。 在springboot项目启动后系统会在‘/temp’目录下创建几个目录用于上传文件。因此清理过‘... 查看详情
springboot上传文件到本服务器目录与jar包同级
前言看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了。当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源。但是当你使用SpringBoot的jar包发布项目的时... 查看详情
springboot目录结构
参考技术A以Maven工程形式新建一个springboot项目。目录结构如下:重点介绍resources目录:在resources文件夹或与其并列的文件夹下建立public文件夹,在public文件夹下的html文件可以通过浏览器中输入文件+后缀名的方式直接访问的. &... 查看详情
springboot如何上传图片文件
...tps://newryan.lanzous.com/ic2vw3a下载后解压,比如解压到E:projectspringboot目录下步骤2:uploadPage.jsp在jsp目录下新建uploadPage.jsp,需要几点:method="post"是必须的enctype="multipart/f 查看详情
springboot关于上传文件例子的剖析
目录SpringBoot上传文件功能实现增加ControllerFileUploadController增加ServiceStorageService增加一个Thymeleaf页面修改一些简单的配置application.properties修改SpringBootApplication类官网没有说明其他的Service类的定义至此,运行项目。可以再上传与下... 查看详情
补习系列(11)-springboot文件上传原理
目录一、文件上传原理二、springboot文件机制临时文件定制配置三、示例代码A.单文件上传B.多文件上传C.文件上传异常D.Bean配置四、文件下载小结一、文件上传原理一个文件上传的过程如下图所示:浏览器发起HTTPPOST请求,指定请... 查看详情
springboot嵌入式tomcat文件上传url映射虚拟路径(代码片段)
...web目录下去就好了,浏览器可以很方便的进行访问。2、SpringBoot默认使用嵌入式Tomcat,将来打包成可执行Jar文件进行部署,显然打成jar包后,总不可能再将上传的文件放在resources目录下去了。3、SpringBoot于是提供了url地址匹配本... 查看详情
springboot整合vue实现上传下载文件(代码片段)
springboot整合vue实现上传下载文件文章目录springboot整合vue实现上传下载文件1上传下载文件api文件2.上传大文件配置3.vue前端主要部分环境springboot1.5.x完整代码下载:springboot整合vue实现上传下1上传下载文件api文件设置上传路径&... 查看详情
springboot文件上传
参考技术A基于SpringBoot的文件上传上传方式:1.直接上传到应用服务器2.上传到OSS(内容存储服务器,如:阿里云,七牛云)3.前端将图片转成Base64编码上传(小容量图片)在Google里面打开http://localhost:8080/upload_status能上传说明已... 查看详情