Skip to content

Commit 9341d8f

Browse files
author
Jachin
committed
2019/06/14
1 parent 2b1c25c commit 9341d8f

21 files changed

Lines changed: 381 additions & 109 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.
6 KB
Binary file not shown.

springmvc/day02_fileupload/.idea/compiler.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springmvc/day02_fileupload/.idea/encodings.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springmvc/day02_fileupload/.idea/misc.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springmvc/day02_fileupload/.idea/workspace.xml

Lines changed: 229 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springmvc/day02_fileupload/src/main/java/controller/UserController.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package controller;
22

3+
import com.sun.jersey.api.client.Client;
4+
import com.sun.jersey.api.client.WebResource;
35
import org.apache.commons.fileupload.FileItem;
46
import org.apache.commons.fileupload.FileUploadException;
57
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
@@ -69,6 +71,7 @@ public String fileupload2(HttpServletRequest request, MultipartFile upload) thro
6971
//使用fileupload组件完成文件上传
7072
//上传的位置
7173
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
74+
System.out.println(realPath);
7275
//判断该路径是否存在
7376
File file = new File(realPath);
7477
if (!file.exists()) {
@@ -87,4 +90,31 @@ public String fileupload2(HttpServletRequest request, MultipartFile upload) thro
8790

8891
return "success";
8992
}
93+
94+
/*
95+
* springmvc跨服务器文件上传
96+
* MultipartFile类型参数名称必须和表单中上传文件框中的name属性的名称一样
97+
* */
98+
@RequestMapping("/fileupload3")
99+
public String fileupload3(MultipartFile upload) throws Exception {
100+
System.out.println("springmvc跨服务器文件上传");
101+
//定义上传服务器路径
102+
String path = "http://localhost:9090/uploads/";
103+
104+
//说明是上传文件项
105+
//获取上传文件名称
106+
String filename = upload.getOriginalFilename();
107+
//把文件名设置成唯一值,以便上传相同文件名称不覆盖
108+
String uuid = UUID.randomUUID().toString().replace("-", "");
109+
filename = uuid + "_" + filename;
110+
111+
//创建客户端的对象
112+
Client client = Client.create();
113+
//和图片服务器连接
114+
WebResource webResource = client.resource(path + filename); //上面的服务器路径如果最后没有/,则这里要拼进去/
115+
//跨服务器上传
116+
webResource.put(upload.getBytes());
117+
118+
return "success";
119+
}
90120
}

springmvc/day02_fileupload/src/main/webapp/index.jsp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,12 @@
2525

2626
</form>
2727

28+
<h3>springmvc跨服务器文件上传</h3>
29+
<form action="/user/fileupload3" method="post" enctype="multipart/form-data">
30+
选择文件:<input type="file" name="upload"/><br/>
31+
<input type="submit" value="上传"/>
32+
33+
</form>
34+
2835
</body>
2936
</html>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)