记录文件上传的一些过程,慢慢记录和补充
先创建一个简单的springboot项目,勾选上web模块和lombok,创建 controller,entity,config,util四个包文件,项目结构创建完毕后如下图
配置文件
spring:
servlet:
multipart:
#单个文件的最大值
max-file-size: 5MB
#上传文件的最大值
max-request-size: 5MB
resources:
#增加外部静态资源目录映射
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:D:\File\
webvalue:
resourcePath: D:\File\
entity中新建类读取yml中的自定义配置
package cn.ordinary.upload.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author ordinary
* @date 2019/12/24
*/
@Data
@Component
@ConfigurationProperties(prefix = "webvalue")
public class WebValue {
private String resourcePath;
}
文件夹工具类
package cn.ordinary.upload.util;
import java.io.File;
import java.util.Calendar;
/**
* @author ordinary
* @date 2019/12/24
*/
public class FolderUtil {
/**
* 按照年月日的方式生成存储文件夹
*
* @param resourcePath 根目录
* @return 文件夹绝对路径
*/
public static String getSaveFolder(String resourcePath) {
Calendar calendar = Calendar.getInstance();
//获取月份加1 月份从0开始的
String folder = String.format("%s\\%d\\%d\\%d\\", resourcePath, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
File file = new File(folder);
//如果文件夹不存在,先生成
if (!file.exists()) {
file.mkdirs();
}
return folder;
}
}
文件工具类
package cn.ordinary.upload.util;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author ordinary
* @date 2019/12/24
*/
public class FileUtil {
/**
* 字节流写入文件
*
* @param filePath 文件路径
* @param data 数据
* @throws IOException io异常
*/
public static void writeFile(String filePath, byte[] data) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(data);
fileOutputStream.flush();
fileOutputStream.close();
}
}
控制器
package cn.ordinary.upload.controller;
import cn.ordinary.upload.entity.WebValue;
import cn.ordinary.upload.util.FileUtil;
import cn.ordinary.upload.util.FolderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
/**
* @author ordinary
* @date 2019/12/24
*/
@Controller
@RequestMapping("/upload")
public class UploadController {
@Autowired
private WebValue webValue;
@ResponseBody
@PostMapping("/file")
public String uploadFile(HttpServletRequest servletRequest, MultipartFile file) {
try {
String folder = FolderUtil.getSaveFolder(webValue.getResourcePath());
String fileName = file.getOriginalFilename();
System.out.println(fileName);
FileUtil.writeFile(folder + fileName, file.getBytes());
return "success";
} catch (Exception ex) {
System.out.println(ex.getMessage());
return "failed";
}
}
}
上传页面
在static文件夹下新建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/upload/file" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传文件">
</form>
</body>
</html>
大功告成,慢慢优化。。。。。。