文件上传前后端代码
后端采用Springboot实现,读者还需根据自己的本地环境进行更改。
前端代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Apache文件解析漏洞靶场</title>
</head>
<body>
<form action="http://localhost:8082/fileload/up" enctype="multipart/form-data" method="POST">
<input name="file" type="file" id="file">
<input type="submit" value="提交">
</form>
</body>
</html>
后端代码
package Orion.AAA.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
/**
* @Author: Orion
* @Date: 2023/04/12/09:51
* 少就是多,慢就是快
*/
@RestController
@RequestMapping("fileload")
public class FileLoadServlet extends HttpServlet {
@PostMapping("/up")
protected String service(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(file != null && request.getContentLength() > 0){
// 获取前端传过来文件的文件名
String fileName = file.getOriginalFilename();
// 指定上传路劲,我是mac,读者可以改为自己的路径
String pathName = "/Applications/phpstudy/WWW/ApacheV/uploadfiles";
String path =pathName+'/';
// 生成上述路径文件
File tempFile = new File(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
// 通过父路径和子文件名创建文件
File file2 = new File(tempFile,fileName);
// 将前端发过来的文件输送给新文件 完成存入
file.transferTo(file2);
return "文件上传成功";
}else{
return "文件上传失败";
}
}
}