2、SpringBoot接口Http协议开发实战8节课(7-8)

时间:2024-12-24 13:05:56

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:

 package net.xdclass.demo.controller;

 import java.io.File;
import java.io.IOException;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import net.xdclass.demo.domain.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; /**
* 功能描述:文件测试
*
* <p> 创建时间:Apr 22, 2018 11:22:29 PM </p>
*/
@Controller
public class FileController { @RequestMapping(value = "/api/v1/gopage")
public Object index() {
return "index";
} private static final String filePath = "L:/Workspaces/Eclipse_Neon/txkt/SpringBootClass/xdclass_springboot/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.java:

 package net.xdclass.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 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;
} }

上传成功

2、SpringBoot接口Http协议开发实战8节课(7-8)

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搭建一个简单的文件服务器