spring MVC 文件上传
前端代码
form表单方式
<html>
<head>
<title>非Ajax上传</title>
</head>
<body>
<h1> 采用非Ajax上传,通过表单来提交</h1>
<form method="post" action="uploadImg1/" enctype="multipart/form-data">
请选择: <input type="file" name="myfile" multiple accept="image/jpeg">
<input type="submit" value="上传">
</form>
</body>
</html>
ajax方式
<html>
<head>
<title>Ajax上传</title>
<%-- 导入在线的jquery库文件--%>
<script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<h1> Ajax上传,通过构造FormData对象,使用Ajax提交.</h1>
请选择: <input type="file" id="myfile" name="myfile" multiple accept="image/jpeg">
<%-- 按钮注释掉,备用--%>
<%-- <input type="button" id="upload" value="上传">--%>
<script>
$(function(){
$("#myfile").change(function (e){
let formData =new FormData();
let files = e.target.files;
console.log(files);
if(files.length == 0){
formData.append("myfile",files[0]);
}
else if(files.length >0)
{
for (let i = 0; i < files.length; i++) {
formData.append("myfile",files[i]);
}
}
console.log("formdata: "+formData);
$.ajax({
type: "post",
url: "uploadImg2/",
data: formData,
cache: false,
contentType: false,
processData: false,
}).success(function (data){
alert(data);
}).error(function (){
alert("上传失败");
});
});
});
</script>
</body>
</html>
后端代码
@Controller
public class UploadCarId {
@PostMapping(value = "images1/")
public String Upload1(MultipartFile myfile[], HttpServletRequest request){
System.out.println("uploadImage myfile: "+myfile);
for (MultipartFile multipartFile : myfile) {
System.out.println("multipartFile.getOriginalFilename(): "+multipartFile.getOriginalFilename());
File file = new File(request.getServletContext().getRealPath("/images/")+multipartFile.getOriginalFilename());
try {
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
@PostMapping(value = "images2/")
@ResponseBody
public String Upload2(MultipartFile myfile[], HttpServletRequest request){
System.out.println("uploadImage myfile: "+myfile);
for (MultipartFile multipartFile : myfile) {
System.out.println("multipartFile.getOriginalFilename(): "+multipartFile.getOriginalFilename());
File file = new File(request.getServletContext().getRealPath("/images/")+multipartFile.getOriginalFilename());
try {
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
}
文件下载
前端代码(表单方式)
<html>
<body>
<h2>Hello World!</h2>
<form method="post" action="download/" enctype="multipart/form-data">
<label>pptx文件</label>
<input name="filename" value="文件名.pptx" hidden="hidden">
<input type="submit" value="下载">
</form>
</body>
</html>
后端代码
@Controller
public class Controller1 {
@PostMapping(value="download/")
public String downloads(HttpServletResponse response , HttpServletRequest request , String filename) throws Exception{
String path = request.getServletContext().getRealPath("/file");
String fileName = filename;
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
File file = new File(path,fileName);
InputStream input=new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buff =new byte[1024];
int index=0;
while((index= input.read(buff))!= -1){
out.write(buff, 0, index);
out.flush();
}
out.close();
input.close();
return null;
}
}