?文件上传
upload.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
文件上传<input type="file" name="file" /></br>
<input type="submit" value="提交">
</form>
</body>
</html>
?contorller控制层
@RestController
public class UploadContorller {
@PostMapping("/upload")
public Map multipart(MultipartFile file, HttpServletRequest request){//MultipartFile 表示你当前上传文件的对象
Map map = new HashMap();
// 拿到文件名
String name = file.getOriginalFilename();
// 换名字
String newNamae = new Date().getTime() + new Random().nextInt(999999) + name;
// 得到上传路径 真实的开发过程中,实际上是你的服务器路径 192.168.208.128/xxx/xxx
String path = request.getServletContext().getRealPath("/upload/");
//
File f = new File(path);
// 当前上传的这个服务器地址如果没有这个目录
if (!f.exists()) {
f.mkdir();//那我就创建这个目录
}
File f1 = new File(path+newNamae);
//显示路径地址
System.out.println(path+newNamae);
try {
file.transferTo(f1);
map.put("msg","成功");
}catch (IllegalStateException e){
e.printStackTrace();
map.put("msg","失败");
}catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
boot启动类
package com.zhf.demoboot3_30;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.util.unit.DataSize;
import javax.servlet.MultipartConfigElement;
import javax.servlet.annotation.MultipartConfig;
import javax.sql.DataSource;
@SpringBootApplication
public class Demoboot330Application {
public static void main(String[] args) {
SpringApplication.run(Demoboot330Application.class, args);
}
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//设置上传的文件大小
factory.setMaxFileSize(DataSize.parse("1024kb"));
//设置上传的总文件大小
factory.setMaxRequestSize(DataSize.parse("102400kb"));
return factory.createMultipartConfig();//创建上传的配置
}
}
文件上传路径
浏览器访问upload.html? ? ? ? ? ? ? ? 并选择文件上传
成功
?application.properties配置文件的值获取
?ServletUtil
package com.zhf.demoboot3_30.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
//1、给属性增加注解读取配置文件数据
//2、编写实体类通过注解读取配置文件数据
@Component
@PropertySource("classpath:application.properties")//去读资源文件
// 通过注解读取配置application.perperties文件中的key-value(键值对)并将key-value中的value映射到属性或实体类中
public class ServerUtil {
@Value("${server.name}")
private String name;
@Value("${server.ip}")
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
contorller层
package com.zhf.demoboot3_30.contorller;
import com.zhf.demoboot3_30.entity.ServerUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UploadContorller {
@Resource
private ServerUtil serverUtil;
public ServerUtil getServerUtil() {
return serverUtil;
}
public void setServerUtil(ServerUtil serverUtil) {
this.serverUtil = serverUtil;
}
@GetMapping("/show")
public ServerUtil getData(){
return serverUtil;
}
}
?application.properties配置
server.name=centeros7
server.ip=192.168.208.128
浏览器访问show请求? 返回配置文件的值
?启动样式banner? ? 在resources下file创建banner.tex文件更改启动样式
?
|