-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUpload.java
More file actions
61 lines (52 loc) · 1.89 KB
/
Copy pathFileUpload.java
File metadata and controls
61 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package boot.spring.controller;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUpload {
@RequestMapping(value="/uploadfile",method = RequestMethod.POST)
@ResponseBody
public String fileupload(@RequestParam MultipartFile uploadfile,HttpServletRequest request){
try{
String filename=uploadfile.getOriginalFilename();
// String targetDir=request.getSession().getServletContext().getRealPath("uploadfiles");
File targetfile=new File("D:\\",filename);
uploadfile.transferTo(targetfile);
}catch(Exception e){
e.printStackTrace();
}
return "success";
}
@RequestMapping(value="/uploadfile2",method = RequestMethod.POST)
@ResponseBody
public String fileuploads(@RequestParam MultipartFile[] uploadfile,HttpServletRequest request){
try{
if(uploadfile!=null&&uploadfile.length>0){
//循环获取file数组中得文件
for(int i = 0;i<uploadfile.length;i++){
MultipartFile file = uploadfile[i];
if(file.getSize()==0){
continue;
}
//保存文件
String filename=file.getOriginalFilename();
// String targetDir=request.getSession().getServletContext().getRealPath("uploadfiles");
File targetfile=new File("D:\\",filename);
file.transferTo(targetfile);
}
}
}catch(Exception e){
e.printStackTrace();
}
return "success";
}
@RequestMapping(value="/fileupload",method = RequestMethod.GET)
String fileupload(){
return "fileupload";
}
}