springmvc 中multipartFile ⽂件上传
⾸先准备⼯作:
我⽤的是Spring 3.02 +Gson1.71+JQuery 1.52+JQuery Uploader Plugin
服务端:Controller 、Service 都是从⽹上Copy 来的,加以修改。
Controller :
为JQuery uploader 删除增加了⼀个handleDelete ⽅法。
Java 代码
1.
<!-- 激活spring 的注解. --> 2.
<context:annotation-config /> 3.
4.
<!-- 扫描注解组件并且⾃动的注⼊spring beans 中. 5.
例如,他会扫描@Controller 和@Service 下的⽂件.所以确保此base-package 设置正确. --> 6.
<context:component-scan base-package ="com.swind.web" /> 7.
8.
<!-- 配置注解驱动的Spring MVC Controller 的编程模型.注:次标签只在 Servlet MVC ⼯作! --> 9. <mvc:annotation-driven />
Java 代码
1.
<bean id="multipartResolver" class ="org.springframework.web.multipartmons.CommonsMultipartResolver"> 2.
<property name="maxUploadSize"> 3.
<value>104857600</value> 4.
</property> 5.
<property name="maxInMemorySize"> 6.
<value>4096</value> 7.
</property> 8.
<property name="defaultEncoding"> 9.
<value>UTF-8</value> 10.
</property> 11. </bean>
Java 代码
1.
<servlet-mapping> 2.
<servlet-name>Dispatcher</servlet-name> 3.
<url-pattern>/</url-pattern> 4. </servlet-mapping>
Java 代码
1.
package com.ller; 2.
3.
import le.gson.Gson; 4.
import com.swindmon.GlobalVariable; 5.
import com.del.FileModel; 6.
import com.swind.web.service.UploadService; 7.
import java.io.IOException; 8.
import java.io.PrintWriter; 9.
import java.util.ArrayList; 10.
import java.util.Iterator; 11.
import java.util.List; 12.
import javax.annotation.Resource; 13.
import javax.servlet.http.HttpServletResponse; 14.
15.
import org.springframework.stereotype.Controller; 16.
import org.springframework.ui.Model; 17.
import org.springframework.util.StringUtils; 18.
import org.springframework.web.bind.annotation.RequestMapping; 19.
import org.springframework.web.bind.annotation.RequestMethod; 20.
import org.springframework.web.bind.annotation.RequestParam; 21.
import org.springframework.web.bind.annotation.ResponseBody; 22.
import org.springframework.web.multipart.MultipartFile; 23. import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
23. import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
24.
25. @Controller
26. public class UploadController {
27.
28. @Resource(name = "uploadService")
29. private UploadService uploadService;
30.
31. /**
32. * 描述 : <;事先就知道确切的上传⽂件数⽬>. <br>
33. *<p>
34.
35. * @param file1
36. * @param file2
37. * @param model
38. * @return
39. * @throws IOException
40. */
41. @RequestMapping(value = "/upload.single", method = RequestMethod.POST)
42. public
43. @ResponseBody
44. String handleImport(
45. @RequestParam(value = "file", required = false) MultipartFile file,
46. HttpServletResponse response) throws IOException {
47.
48. String uploadHome = UploadHome();
49. FileModel fileModel = new FileModel();
50.
51. if (file != null && StringUtils.OriginalFilename())) {
52. // System.out.OriginalFilename());
53.
54.
55. fileModel.OriginalFilename());
56. fileModel.Size());
57. fileModel.ContentType());
58. String path = uploadService.saveFileToServer(file, uploadHome);
59. fileModel.setPath(path);
60. }
61.
62. uploadService.json_encode(response, fileModel);
63. return null;
64.
65. }
66.
67. /**
68. * 描述 : <;事先就并不知道确切的上传⽂件数⽬,⽐如FancyUpload这样的多附件上传组件>. <br>
69. *<p>
70.
71. * @param model
72. * @param multipartRequest
73. * @return
74. * @throws IOException
75. */
76. @SuppressWarnings("unchecked")
77. @RequestMapping(value = "/upload.multi", method = RequestMethod.POST)
78. public
79. @ResponseBody
80. String handleImport(
81. DefaultMultipartHttpServletRequest multipartRequest,
82. HttpServletResponse response) throws IOException {
83.
84. String uploadHome = UploadHome();
85. List<FileModel> list = new ArrayList<FileModel>();
86. if (multipartRequest != null) {
87. Iterator iterator = FileNames();
88.
89. while (iterator.hasNext()) {
90. MultipartFile multifile =
91. File((String) ());
92.
93. if (StringUtils.OriginalFilename())) {
94. // System.out.OriginalFilename());
95. FileModel fileModel = new FileModel();
96. fileModel.OriginalFilename());
97. fileModel.Size());
98. String path = uploadService.saveFileToServer(multifile, uploadHome);
99. fileModel.setPath(path);
100. list.add(fileModel);
101. }
102.
102.
103. }
104. }
105. uploadService.json_encode(response, list);
106. return null;
107.
108. }
109.
110. @RequestMapping(value = "/upload.*", method = RequestMethod.DELETE)
111. public@ResponseBody String handleDelete(@RequestParam(value = "file", required = false) String file, 112. HttpServletResponse response) throws IOException {
113. String uploadHome = UploadHome();
114. boolean success = uploadService.deleteFiletoServer(file, uploadHome);
115. uploadService.json_encode(response, success);
116. return null;
117. }
118. }
Service:
1. package com.swind.web.service;
2.
3. le.gson.Gson;
4. import java.io.File;
5. import java.io.FileOutputStream;
6. import java.io.IOException;
7. import java.io.InputStream;
8. import java.io.OutputStream;
9. import java.io.PrintWriter;
10. import javax.servlet.http.HttpServletResponse;
11.
mvc的controller12. import org.springframework.stereotype.Service;
13. import org.springframework.web.multipart.MultipartFile;
14.
15. @Service("uploadService")
16. public class UploadService {
17.
18. /**
19. * 描述 : <;将⽂件保存到指定路径>. <br>
20. *<p>
21. *
22. * @param multifile
23. * @param path
24. * @return
25. * @throws IOException
26. */
27. public String saveFileToServer(MultipartFile multifile, String path)
28. throws IOException {
29. // 创建⽬录
30. File dir = new File(path);
31. if (!ists()) {
32. dir.mkdir();
33. }
34. // 读取⽂件流并保持在指定路径
35. InputStream inputStream = InputStream();
36. OutputStream outputStream = new FileOutputStream(path
37. + OriginalFilename());
38. byte[] buffer = Bytes();
39. int bytesum = 0;
40. int byteread = 0;
41. while ((byteread = ad(buffer)) != -1) {
42. bytesum += byteread;
43. outputStream.write(buffer, 0, byteread);
44. outputStream.flush();
45. }
46. outputStream.close();
47. inputStream.close();
48.
49. return path + OriginalFilename();
50. }
51. public boolean deleteFiletoServer(String file, String path)
52. throws IOException {
53. boolean success = Boolean.FALSE;
54. File f = new File(path+file);
55. if (f.exists()) {
56. f.delete();
57. success = Boolean.TRUE;
58. }
59.
60.
61. return success;
62. }
63. public void json_encode(final HttpServletResponse response, Object o) throws IOException{
64. response.setHeader("Cache-Control", "no-store");
65. response.setHeader("Pragma", "no-cache");
66. response.setDateHeader("Expires", 0);
67. response.setContentType("text/html");
68. PrintWriter out = Writer();
69. Gson gs = new Gson();
70. out.Json(o));
71. }
72. }
注;MultipartFile⽂件上传,若⼀个⽂件同时上传⾄多个⽬录,那么只有第⼀次可以被成功写⼊,因为Mul
tipartFile是把⽂件写到内存,读取完之后⾃动删除的,所以第⼆次在读的时候,⽂件已经不存在了,所以上传到第N个⽬录应该和上传到⼀个⽬录区分开来,即第⼀次直接从内存读取,第⼆次读取第⼀次存取的⽂件
Java代码
1. package com.del;
2.
3. import java.io.Serializable;
4.
5. public class FileModel implements Serializable {
6.
7. /**
8. *
9. */
10. private static final long serialVersionUID = 7964950152782381235L;
11. private String name;
12. private long size;
13. private String path;
14. private String type;
15.
16. /**
17. * @return the path
18. */
19. public String getPath() {
20. return path;
21. }
22.
23. /**
24. * @param path the path to set
25. */
26. public void setPath(String path) {
27. this.path = path;
28. }
29.
30. /**
31. * @return the name
32. */
33. public String getName() {
34. return name;
35. }
36.
37. /**
38. * @param name the name to set
39. */
40. public void setName(String name) {
41. this.name = name;
42. }
43.
44. /**
45. * @return the size
46. */
47. public long getSize() {
48. return size;
49. }
50.
51. /**
52. * @param size the size to set
53. */
54. public void setSize(long size) {
55. this.size = size;
56. }
57.
58. public String getType() {
59. return type;
60. }
61.
62. public void setType(String type) {
63. pe = type;
64. }
65.
66.
67. }
HTML UPload Page:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论