|
1
2
|
package com.huaheng.pc.common;
|
|
3
|
import com.google.zxing.WriterException;
|
|
4
|
import com.huaheng.common.config.Global;
|
|
5
|
import com.huaheng.common.config.ServerConfig;
|
|
6
|
import com.huaheng.common.utils.DateUtils;
|
|
7
|
import com.huaheng.common.utils.QRCodeGenerator;
|
|
8
|
import com.huaheng.common.utils.file.FileUploadUtils;
|
|
9
|
import com.huaheng.common.utils.file.FileUtils;
|
|
10
|
import com.huaheng.framework.config.HuaHengConfig;
|
|
11
|
import com.huaheng.framework.web.domain.AjaxResult;
|
|
12
13
|
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
|
|
14
15
16
17
|
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
|
|
18
|
import org.springframework.stereotype.Controller;
|
|
19
20
|
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
|
|
21
|
|
|
22
23
24
25
26
27
28
29
30
|
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
|
|
31
32
|
/**
* 通用请求处理
|
|
33
|
*
|
|
34
35
36
|
* @author huaheng
*/
@Controller
|
|
37
|
public class CommonController {
|
|
38
39
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
40
41
42
|
@Resource
private ServerConfig serverConfig;
|
|
43
|
@RequestMapping("common/download")
|
|
44
45
|
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
try {
|
|
46
47
|
String dateTime = DateUtils.dateTimeNow();
String realFileName = dateTime + fileName.substring(fileName.indexOf("_") + 1);
|
|
48
|
String filePath = Global.getDownloadPath() + fileName;
|
|
49
50
51
52
53
|
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
|
|
54
|
if (Boolean.TRUE.equals(delete)) {
|
|
55
56
|
FileUtils.deleteFile(filePath);
}
|
|
57
|
} catch (Exception e) {
|
|
58
59
60
61
|
log.error("下载文件失败", e);
}
}
|
|
62
|
public String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
|
|
63
64
|
final String agent = request.getHeader("USER-AGENT");
String filename = fileName;
|
|
65
|
if (agent.contains("MSIE")) {
|
|
66
67
68
|
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
|
|
69
|
} else if (agent.contains("Firefox")) {
|
|
70
71
|
// 火狐浏览器
filename = new String(fileName.getBytes(), "ISO8859-1");
|
|
72
|
} else if (agent.contains("Chrome")) {
|
|
73
74
|
// google浏览器
filename = URLEncoder.encode(filename, "utf-8");
|
|
75
|
} else {
|
|
76
77
78
79
80
81
|
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
|
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
@GetMapping(value = "/image/{text}")
public ResponseEntity<byte[]> getImage(@PathVariable("text") String text) {
byte[] qrcode = null;
try {
qrcode = QRCodeGenerator.getQRCodeImage(text, 100, 100);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Set headers
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED);
}
|
|
98
99
100
101
102
103
|
/**
* 通用上传请求
*/
@PostMapping("/common/upload")
@ResponseBody
|
|
104
105
|
public AjaxResult uploadFile(MultipartFile file) throws Exception {
try {
|
|
106
|
// 上传文件路径
|
|
107
|
String filePath = HuaHengConfig.getProfile();
|
|
108
109
110
111
112
113
114
115
116
117
|
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + "/" + fileName;
AjaxResult ajax = AjaxResult.success();
Map<String, Object> map = new HashMap<>();
map.put("fileName", fileName);
map.put("url", url);
ajax.setData(map);
return ajax;
|
|
118
|
} catch (Exception e) {
|
|
119
120
121
|
return AjaxResult.error(e.getMessage());
}
}
|
|
122
|
}
|