InformController.java
6.42 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.project.demo.web;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.demo.domain.AjaxJson;
import com.project.demo.domain.DataTablePageUtil;
import com.project.demo.domain.Inform;
import com.project.demo.domain.Users;
import com.project.demo.service.InformService;
import com.project.demo.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* Created by xuwenhao on 2018/7/8.
*/
@Controller
@RequestMapping("informs")
public class InformController {
@Autowired
private UsersService usersService;
@Autowired
private InformService informService;
@RequestMapping(method = RequestMethod.GET)
public String findAll() {
return "/inform/index";
}
@RequestMapping(value = "/informList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String investigateList(final HttpSession session, final HttpServletRequest request){
Users currentUser = (Users) session.getAttribute("users");
DataTablePageUtil<Inform> dataTable = new DataTablePageUtil<Inform>(request);
PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());//分页
List<Inform> informs = informService.search();
informs.stream().forEach(d ->{
d.setUserName(usersService.findById(d.getUserId()).getName());
});
PageInfo<Inform> pageInfo = new PageInfo<>(informs);
//封装数据给DataTables
dataTable.setDraw(dataTable.getDraw());
dataTable.setData(pageInfo.getList());
dataTable.setRecordsTotal((int)pageInfo.getTotal());
dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
//返回数据到页面
String jsonObject = JSONObject.toJSONString(dataTable);
return jsonObject;
}
@RequestMapping(value = "/create")
public String create() {
return "/welcome/new";
}
@RequestMapping(value = "/new", method = RequestMethod.POST)
public String save(final HttpSession session, @RequestParam String information, MultipartFile file,HttpServletRequest request )throws IOException {
System.out.println(file.getName()+"----file.getName()");
Inform inform = new Inform();
// =file.getOriginalFilename();
if (file.getSize() != 0) {
String filename = new String(file.getOriginalFilename().getBytes("ISO-8859-1"), "UTF-8");
String fileName = filename.substring(0, filename.lastIndexOf(".")).toUpperCase();
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
String path = "/opt/projectManagement/file";
String fileId = UUID.randomUUID().toString();
String finalName = fileId + "." + ext;
SaveFileFromInputStream(file.getInputStream(), path, finalName);//保存到服务器的路径
inform.setFileName(fileName);
inform.setFileId(finalName);
}
Users users = (Users)session.getAttribute("users");
inform.setState(true);
inform.setInformation(information);
inform.setCreateTime(new Date());
inform.setUserId(users.getId());
informService.saveInform(inform);
return "redirect:/welcome";
}
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException {
System.out.println("进入SaveFileFromInputStream");
FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);
System.out.println("进入SaveFileFromInputStream1");
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
@RequestMapping(value = "/deleteInform", method = RequestMethod.POST)
public AjaxJson deleteInform(final HttpSession session, @RequestParam Integer id){
AjaxJson ajaxJson = new AjaxJson();
Users users = (Users)session.getAttribute("users");
informService.deleteInform(id);
ajaxJson.setCode(1);
ajaxJson.setMsg("删除成功");
return ajaxJson;
}
@RequestMapping(value = "/download/{id}",method = RequestMethod.GET)
public HttpServletResponse download(HttpServletResponse response, @PathVariable Integer id) {
Inform inform = informService.findById(id);
String fileId = inform.getFileId();
String fileName = inform.getFileName();
try {
// path是指欲下载的文件的路径。
String path = "/opt/projectManagement/file/"+fileId;
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
String finalName = fileName+"."+ext;
System.out.println(finalName+"-----=====finalName");
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(finalName.getBytes("UTF-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
}