tlias管理系统--员工管理--文件上传-本地上传功能实现
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.controller;
|
||||||
|
|
||||||
|
import com.inmind.pojo.Result;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
public class UploadController {
|
||||||
|
|
||||||
|
//本文件上传
|
||||||
|
@PostMapping("/upload")
|
||||||
|
public Result upload(String username, Integer age, MultipartFile image) throws IOException {
|
||||||
|
log.info("文件上传:{},{},{}",username,age,image);
|
||||||
|
//获取源文件的文件名
|
||||||
|
String originalFilename = image.getOriginalFilename();//中国梦.txt .jpg .mp4 .avi
|
||||||
|
//动态文件后缀获取和拼接
|
||||||
|
int index = originalFilename.lastIndexOf(".");
|
||||||
|
String extName = originalFilename.substring(index);
|
||||||
|
|
||||||
|
String newFile = UUID.randomUUID().toString()+extName;
|
||||||
|
|
||||||
|
//将上传的文件数据保存下来!!
|
||||||
|
String filePath = "D:\\upload_images\\"+newFile;
|
||||||
|
log.info("保存的文件名:{}",filePath);
|
||||||
|
image.transferTo(new File(filePath));
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,3 +20,9 @@ spring.datasource.password=1234
|
|||||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
#mybatis 驼峰自动映射
|
#mybatis 驼峰自动映射
|
||||||
mybatis.configuration.map-underscore-to-camel-case=true
|
mybatis.configuration.map-underscore-to-camel-case=true
|
||||||
|
|
||||||
|
#文件上传配置
|
||||||
|
#单个文件上传的大小限制
|
||||||
|
spring.servlet.multipart.max-file-size=10MB
|
||||||
|
#一次请求文件上传的大小限制
|
||||||
|
spring.servlet.multipart.max-request-size=100MB
|
||||||
17
tlias-web-management/src/main/resources/static/upload.html
Normal file
17
tlias-web-management/src/main/resources/static/upload.html
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>上传文件</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<form action="/upload" method="post" enctype="multipart/form-data">
|
||||||
|
姓名: <input type="text" name="username"><br>
|
||||||
|
年龄: <input type="text" name="age"><br>
|
||||||
|
头像: <input type="file" name="image"><br>
|
||||||
|
<input type="submit" value="提交">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -3,6 +3,8 @@ package com.inmind;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class TliasWebManagementApplicationTests {
|
class TliasWebManagementApplicationTests {
|
||||||
|
|
||||||
@@ -10,4 +12,11 @@ class TliasWebManagementApplicationTests {
|
|||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUUID() {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
System.out.println(UUID.randomUUID().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user