- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileUploadController.java
More file actions
Latest commit
84 lines (75 loc) · 4.15 KB
/
Copy pathFileUploadController.java
File metadata and controls
84 lines (75 loc) · 4.15 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
packagecom.controller;
importcom.sun.jersey.api.client.Client;
importcom.sun.jersey.api.client.WebResource;
importorg.apache.commons.fileupload.FileItem;
importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
importorg.apache.commons.fileupload.servlet.ServletFileUpload;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.multipart.MultipartFile;
importjavax.servlet.http.HttpServletRequest;
importjava.io.File;
importjava.util.List;
importjava.util.UUID;
@Controller
@RequestMapping("/fileUpload")
publicclassFileUploadController {
//传统文件上传(使用fileupload组件完成文件上传,common-fileupload.jar和common-io.jar)
@RequestMapping("/fileupload1")
publicStringfileuoload1(HttpServletRequestrequest) throwsException {
System.out.println("文件上传...");
Stringpath = request.getSession().getServletContext().getRealPath("/uploads/");//上传的位置
Filefile = newFile(path);//判断,该路径是否存在
if(!file.exists()){
file.mkdirs();//如果不存在,则创建该文件夹
}
//解析request对象,获取上传文件项
DiskFileItemFactoryfactory = newDiskFileItemFactory();
ServletFileUploadupload = newServletFileUpload(factory);
//解析request
List<FileItem> items = upload.parseRequest(request);
for(FileItemitem:items){//遍历
if(item.isFormField()){// 进行判断,当前item对象是否是上传文件项
//说明普通表单向
}else{
//说明上传文件项
Stringfilename = item.getName().substring(item.getName().lastIndexOf("\\") + 1);//获取上传文件的名称
Stringuuid = UUID.randomUUID().toString().replace("-", "");//把文件的名称设置唯一值,uuid
filename = uuid+"_"+filename;
item.write(newFile(path,filename));//完成文件上传
item.delete();//删除临时文件
}
}
return"success";
}
//SpringMVC文件上传
@RequestMapping("/fileupload2")
publicStringfileuoload2(HttpServletRequestrequest, MultipartFileupload) throwsException {//MultipartFile upload的upload必须与表单file类型的input的name一致
System.out.println("SpringMVC文件上传...");
//使用fileupload组件完成文件上传
Stringpath = request.getSession().getServletContext().getRealPath("/uploads/");//上传的位置
Filefile = newFile(path);
if(!file.exists()){//判断,该路径是否存在
file.mkdirs();//若不存在,则创建该文件夹
}
Stringfilename = upload.getOriginalFilename();//获取上传文件的名称
Stringuuid = UUID.randomUUID().toString().replace("-", "");//把文件的名称设置唯一值,uuid
filename = uuid+"_"+filename;
upload.transferTo(newFile(path,filename));//完成文件上传
return"success";
}
//SpringMVC跨服务器文件上传
@RequestMapping("/fileupload3")
publicStringfileuoload3(MultipartFileupload) throwsException {
System.out.println("跨服务器文件上传...");
//定义上传文件服务器路径(这是新开启的本地服务器,模拟跨服务器的模式)
Stringpath = "http://localhost:9090/uploads/";//新tomcat服务器参数:HTTP prot:9090, JMX port:1090
Stringfilename = upload.getOriginalFilename();//获取上传文件的名称
Stringuuid = UUID.randomUUID().toString().replace("-", "");//把文件的名称设置唯一值,uuid
filename = uuid+"_"+filename;
Clientclient = Client.create();//创建客户端的对象
WebResourcewebResource = client.resource(path + filename);//和图片服务器进行连接
webResource.put(upload.getBytes());//上传文件;目前没有uploads文件,代码里也没自动创建,因此需手动创建,否则409错误
return"success";
}
}