1. 简单文件上传实现
1. 部分项目目录结构
1. 配置文件上传路径
-
application.properties 文件中添加如下配置
web.upload-file-path = D:/Workspaces/SpringBootWorkspaces/upload-file-test/ spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-file-path}
2. 添加文件上传配置(此处为SdSpringBootApplication)
// 需要将其配置到有@Configuration注解的类中
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个文件最大
factory.setMaxFileSize("201400KB"); // KB, MB
// 设置上传数据总大小
factory.setMaxRequestSize("2014000KB");
return factory.createMultipartConfig();
}
3. 添加封装Json数据的实体类(此处为JsonData.java)
package com.test.spring_boot_demo.domain;
public class JsonData {
private int code;
private String msg;
private String data;
public JsonData(int code, String data) {
this.code = code;
this.data = data;
}
public JsonData(int code, String msg, String data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
4. 添加文件上传接口(此处为FileController中)
@RestController
public class FileController {
private static final String filePath = "D:/Workspaces/SpringBootWorkspaces/upload-file-test/";
@PostMapping("/file/upload")
public Object fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
// 其他参数
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);
// 文件上传后的路径
String newFileName = UUID.randomUUID() + suffixName;
System.out.println("转换后的文件名:" + newFileName);
File dest = new File(filePath + newFileName);
try {
file.transferTo(dest);
return new JsonData(1, newFileName);
} catch (IOException e) {
e.printStackTrace();
}
return new JsonData(0, "fail to save", null);
}
}
5. 添加静态页面以及css文件
-
file_upload_test.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Title</title> <meta name="Keywords" content=""> <meta name="sdescription" content=""> <link rel="stylesheet" type="text/css" href="/css/main.css"> <style type="text/css"> * {margin: 0; padding: 0;} a {text-decoration: none; color: #111;} li {list-style: none;} body {background: #FFF; font-size: 14px; font-family: "微软雅黑"; color: #666;} </style> </head> <body> <form class="file-form" enctype="multipart/form-data" method="post" action="/file/upload"> <input type="file" name="file" /> <br /> <input type="text" name="name" /> <br /> <button type="submit">提交</button> </form> </body> </html>
-
main.css
.file-form {width: 400px; border: 1px solid #550044; text-align: center; margin: 100px auto; padding: 50px 30px;} input {margin: 10px; width: 200px; height: 30px;} button {background: #2894FF; color: #111; width: 204px; height: 30px; border: 2px solid #2894FF;}
- 此处html、css文件都放在static目录下,已经被加载。
- 此处css文件仅仅是为了验证可被html引用,不做专门的效果。
2. jar包方式运行项目
1. 添加Maven依赖(pom.xml)
<!--
打包成jar包,需要添加该maven依赖
如果没有添加相关依赖,执行maven打包,运行后会报错:no main manifest attribute, in XXX.jar
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 打包jar包
- Eclipse:右键点击pom.xml,选择 run as -> maven install
- IDEA:选择 View -> Tool Windows -> Maven Projects -> 项目 -> Lifecycle -> install,然后点击Maven Projects窗体上方的运行按钮
3. 运行项目jar包(命令行命令)
> java -jar xxx.jar
4. 测试
-
访问localhost:8080/html/file_upload_test.html
-
添加文件上传,假如上传成功,会返回新的文件名字
-
使用 localhost:8080/新文件名 访问图片