目录结构,文件上传

一、目录结构

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(3)---目录结构,文件上传的更多相关文章

  1. springboot实现简单的文件上传

    承接上一篇,这里记录一下简单的springboot文件上传的方式 首先,springboot简单文件上传不需要添加额外的jar包和配置 这里贴一下后端controller层的实现代码 补一份前台的HT ...

  2. springboot(九)文件上传

    在企业级项目开发过程中,上传文件是最常用到的功能.SpringBoot集成了SpringMVC,当然上传文件的方式跟SpringMVC没有什么出入.下面我们来创建一个SpringBoot项目完成单个. ...

  3. Springboot 一行代码实现文件上传 20个平台!少写代码到极致

    大家好,我是小富~ 技术交流,公众号:程序员小富 又是做好人好事的一天,有个小可爱私下问我有没有好用的springboot文件上传工具,这不巧了嘛,正好我私藏了一个好东西,顺便给小伙伴们也分享一下,d ...

  4. SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】

    Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...

  5. SpringBoot后台如何实现文件上传下载

    1.单文件上传: @RequestMapping(value = "/upload") @ResponseBody public String upload(@RequestPar ...

  6. springboot整合OSS实现文件上传

    OSS 阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量.安全.低成本.高可靠的云存储服务.OSS可用于图片.音视频.日志等海量文件的存储.各种终端 ...

  7. SpringBoot整合SpringMVC完成文件上传

    1.编写Controller /** * SPringBoot文件上传 */ //@Controller @RestController //表示该类下的方法的返回值会自动做json格式的转换 pub ...

  8. springboot+thymeleaf 实现图片文件上传及回显

    1. 创建一个springboot工程, 在此就不多说了(目录结构). 2. 写一个HTML页面 <!DOCTYPE html> <html lang="en" ...

  9. git push 不想把本地某个目录下文件上传的办法

  10. SpringBoot 文件上传临时文件路径问题

    年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...

随机推荐

  1. Ubuntu学习之路

    一. Ubuntu简介 Ubuntu(乌班图)是一个基于Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“ubuntu”一词,意思是“人性”.“我的存在是因为大 ...

  2. "tsc.exe"已退出,代码1

    公司开发新项目要用到ABP,于是到处在网上找些资料学习,在官网下好了模板(http://aspnetboilerplate.com/Templates),拿下来后用vs(博主用的是vs2013)编译后 ...

  3. 关于numpy.maximum函数的测试

    atr.py import numpy as np a = np.arange(9)print("a:",a)print(a[0:3])print(a[3:6])print(a[6 ...

  4. 微信小程序开发之搞懂flex布局5——cross axis

    Cross Axis——交叉轴,与Main Axis(主轴)垂直交叉. main axis is row or row-reverse the cross axis runs down the col ...

  5. java反射获取Object的属性和值

    在看反射顺便做个笔记,目前知道的反射的Object都是要有对象的也就是实体Bean. import java.lang.reflect.Field; import java.util.ArrayLis ...

  6. 关于浏览器cookie的小知识

    浏览器对于总的cookie数量是没有限制的,但是对于每个域名的cookie数量是有限制的. 一,不同的浏览器,对于一个域名的cookie数量限制上限是不同的: 1,IE6以下版本,最多20个.IE7以 ...

  7. SSM+MyBatis框架详解

  8. String StringBuilder StringBuffer区别

    String StringBuilder StringBuffer String类是final类,不可以被继承,且它的成员方法也是final方法,当一个字符串对象进行操作操作时,任何的改变不会影响到这 ...

  9. Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  10. roadhog如何支持除development和production外的其他环境变量配置

    roadhog的build和start脚本分别对应了env/development和production,但实践中存在第三种开发环境(可能是预发或集成测试),配置和前两种也都不一样,但现在似乎没办法支 ...