2、SpringBoot接口Http协议开发实战8节课(7-8)
7、SpringBoot2.x文件上传实战
简介:讲解HTML页面文件上传和后端处理实战
1、讲解springboot文件上传 MultipartFile file,源自SpringMVC
1)静态页面直接访问:localhost:8080/index.html
注意点:
如果想要直接访问html页面,则需要把html放在springboot默认加载的文件夹下面
2)MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效)
访问路径 http://localhost:8080/images/cdd6ab24-acfd-48b0-a043-6e22812c7afd.jpg
代码示例:
FileController.java:
1 package net.xdclass.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.UUID; 6 7 import javax.servlet.http.HttpServletRequest; 8 9 import net.xdclass.demo.domain.JsonData; 10 11 import org.springframework.stereotype.Controller; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.bind.annotation.ResponseBody; 15 import org.springframework.web.multipart.MultipartFile; 16 17 /** 18 * 功能描述:文件测试 19 * 20 * <p> 创建时间:Apr 22, 2018 11:22:29 PM </p> 21 */ 22 @Controller 23 public class FileController { 24 25 @RequestMapping(value = "/api/v1/gopage") 26 public Object index() { 27 return "index"; 28 } 29 30 private static final String filePath = "L:/Workspaces/Eclipse_Neon/txkt/SpringBootClass/xdclass_springboot/src/main/resources/static/images/";//末尾需要加/,这样才能写进来 31 32 @RequestMapping(value = "upload") 33 @ResponseBody 34 public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) { 35 36 // file.isEmpty(); 判断图片是否为空 37 // file.getSize(); 图片大小进行判断 38 39 String name = request.getParameter("name"); 40 System.out.println("用户名:" + name); 41 42 // 获取文件名 43 String fileName = file.getOriginalFilename(); 44 System.out.println("上传的文件名为:" + fileName); 45 46 // 获取文件的后缀名,比如图片的jpeg,png 47 String suffixName = fileName.substring(fileName.lastIndexOf(".")); 48 System.out.println("上传的后缀名为:" + suffixName); 49 50 // 文件上传后的路径 51 fileName = UUID.randomUUID() + suffixName; 52 System.out.println("转换后的名称:" + fileName); 53 54 File dest = new File(filePath + fileName); 55 56 try { 57 file.transferTo(dest); 58 59 return new JsonData(0, fileName); 60 } catch (IllegalStateException e) { 61 e.printStackTrace(); 62 } catch (IOException e) { 63 e.printStackTrace(); 64 } 65 return new JsonData(-1, "fail to save ", null); 66 } 67 68 }
JsonData.java:
1 package net.xdclass.demo.domain; 2 3 import java.io.Serializable; 4 5 public class JsonData implements Serializable { 6 private static final long serialVersionUID = 1L; 7 8 //状态码,0表示成功,-1表示失败 9 private int code; 10 11 //结果 12 private Object data; 13 14 //错误描述 15 private String msg; 16 17 public int getCode() { 18 return code; 19 } 20 21 public String getMsg() { 22 return msg; 23 } 24 25 public void setMsg(String msg) { 26 this.msg = msg; 27 } 28 29 public void setCode(int code) { 30 this.code = code; 31 } 32 33 public Object getData() { 34 return data; 35 } 36 37 public void setData(Object data) { 38 this.data = data; 39 } 40 41 public JsonData(int code, Object data) { 42 super(); 43 this.code = code; 44 this.data = data; 45 } 46 47 public JsonData(int code, String msg,Object data) { 48 super(); 49 this.code = code; 50 this.msg = msg; 51 this.data = data; 52 } 53 54 55 56 }
上传成功
8、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中增加下面配置
1) web.images-path=/Users/jack/Desktop(存储路径)
2) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
4、文件服务器:fastdfs,阿里云oss,nginx搭建一个简单的文件服务器