|
1 | 1 | package rml.controller; |
2 | 2 |
|
3 | 3 | import java.io.File; |
| 4 | +import java.util.Date; |
4 | 5 |
|
| 6 | +import javax.servlet.ServletContext; |
5 | 7 | import javax.servlet.http.HttpServletRequest; |
6 | 8 |
|
7 | 9 | import org.apache.commons.io.FileUtils; |
8 | 10 | import org.apache.log4j.Logger; |
9 | 11 | import org.springframework.beans.factory.annotation.Autowired; |
10 | 12 | import org.springframework.stereotype.Controller; |
11 | 13 | import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 15 | +import org.springframework.web.bind.annotation.RequestParam; |
12 | 16 | import org.springframework.web.bind.annotation.ResponseBody; |
| 17 | +import org.springframework.web.context.ServletContextAware; |
13 | 18 | import org.springframework.web.context.request.RequestContextHolder; |
14 | 19 | import org.springframework.web.context.request.ServletRequestAttributes; |
| 20 | +import org.springframework.web.multipart.commons.CommonsMultipartFile; |
15 | 21 |
|
16 | 22 | import rml.model.vo.DataGrid; |
17 | 23 | import rml.model.vo.Doc; |
|
21 | 27 |
|
22 | 28 | @Controller |
23 | 29 | @RequestMapping("/docController") |
24 | | -public class DocController extends BaseController{ |
| 30 | +public class DocController extends BaseController implements ServletContextAware{ |
25 | 31 |
|
26 | 32 | private static final Logger logger = Logger.getLogger(DocController.class); |
27 | 33 |
|
28 | 34 | @Autowired |
29 | 35 | private DocServiceI docService; |
30 | 36 |
|
31 | | - private File uploadFile; |
32 | | - private String uploadFileFileName; |
33 | | - private String uploadFileContentType; |
| 37 | + private ServletContext servletContext; |
34 | 38 |
|
35 | | - public File getUploadFile() { |
36 | | - return uploadFile; |
37 | | - } |
38 | | - |
39 | | - public void setUploadFile(File uploadFile) { |
40 | | - this.uploadFile = uploadFile; |
41 | | - } |
42 | | - |
43 | | - public String getUploadFileFileName() { |
44 | | - return uploadFileFileName; |
45 | | - } |
46 | | - |
47 | | - public void setUploadFileFileName(String uploadFileFileName) { |
48 | | - this.uploadFileFileName = uploadFileFileName; |
49 | | - } |
50 | | - |
51 | | - public String getUploadFileContentType() { |
52 | | - return uploadFileContentType; |
53 | | - } |
54 | | - |
55 | | - public void setUploadFileContentType(String uploadFileContentType) { |
56 | | - this.uploadFileContentType = uploadFileContentType; |
| 39 | + @Override |
| 40 | + public void setServletContext(ServletContext context) { |
| 41 | + this.servletContext = context; |
57 | 42 | } |
58 | 43 |
|
59 | 44 | @RequestMapping("/doc") |
@@ -127,32 +112,32 @@ public DataGrid datagrid(Doc doc) { |
127 | 112 | return docService.datagrid(doc); |
128 | 113 | } |
129 | 114 |
|
130 | | - @RequestMapping("/upload") |
| 115 | + @RequestMapping(value="/upload", method = RequestMethod.POST) |
131 | 116 | @ResponseBody |
132 | | - public Json upload(Doc doc) { |
| 117 | + public Json upload(Doc doc, @RequestParam("uploadFile") CommonsMultipartFile file) { |
133 | 118 | Json j = new Json(); |
134 | | - try { |
135 | | - String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ", ""); |
136 | | - //String realpath = ServletActionContext.getServletContext().getRealPath("/upload"); |
137 | | - HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); |
138 | | - String realpath = request.getSession().getServletContext().getRealPath("/upload"); |
139 | | - |
| 119 | + |
| 120 | + try { |
| 121 | + String realpath = this.servletContext.getRealPath("/upload"); |
| 122 | + String uploadFileFileName = file.getOriginalFilename(); |
| 123 | + String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ", ""); |
| 124 | + String fileType = uploadFileFileNameWithoutSpace.substring(uploadFileFileNameWithoutSpace.lastIndexOf(".")); |
140 | 125 |
|
141 | 126 | File targetFile = new File(realpath+File.separator, uploadFileFileNameWithoutSpace); |
142 | 127 | if (targetFile.exists()) { |
143 | 128 | targetFile.delete(); |
144 | 129 | } |
145 | | - FileUtils.copyFile(uploadFile, targetFile); |
146 | | - |
| 130 | + file.getFileItem().write(targetFile); |
147 | 131 | docService.upload(doc,uploadFileFileNameWithoutSpace); |
148 | 132 |
|
149 | 133 | j.setSuccess(true); |
150 | 134 | j.setMsg("Upload manual successfully"); |
| 135 | + |
151 | 136 | }catch (Exception e) { |
152 | 137 | logger.error(ExceptionUtil.getExceptionMessage(e)); |
153 | 138 | j.setMsg("Upload manual unsuccessfully"); |
154 | 139 | } |
| 140 | + |
155 | 141 | return j; |
156 | 142 | } |
157 | | - |
158 | 143 | } |
0 commit comments