CommonController.java
6.17 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.lhcredit.framework.web.controller;
import cn.hutool.core.io.resource.FileObjectResource;
import cn.hutool.core.io.resource.FileResource;
import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.core.io.resource.MultiFileResource;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.common.utils.OssUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.file.FileUtils;
import com.lhcredit.common.utils.oss.OssService;
import com.lhcredit.common.utils.oss.OssUploadController;
import com.lhcredit.framework.config.CommonConfig;
import com.lhcredit.framework.config.ServerConfig;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.framework.web.service.FastDFSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 通用请求处理
*
* @author ruoyi
*/
@Controller
@RequestMapping(value = {"web/common","/"})
public class CommonController
{
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@Autowired
private ServerConfig serverConfig;
@Autowired
private FastDFSClient fastDFSClient;
private static final String FILE_DELIMETER = ",";
public static List<String> media=Arrays.asList("jpg","png","pdf","doc","txt","docx","xlsx","xls","ppt");
/**
* 通用下载请求
*
* @param fileName 文件名称
* @param delete 是否删除
*/
@GetMapping("common/download")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
try {
if (!FileUtils.isValidFilename(fileName)) {
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
}
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
String filePath = CommonConfig.getDownloadPath() + fileName;
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
if (delete) {
FileUtils.deleteFile(filePath);
}
} catch (Exception e) {
log.error("下载文件失败", e);
}
}
/**
* 通用上传请求(单个)
*/
@PostMapping("/upload")
@ResponseBody
public Object uploadFile(MultipartFile file) throws Exception
{
// Object object = OssUtils.standardAnnax(file);
String filename = file.getOriginalFilename();
String ex = OssService.getExtensionName(filename);
String s = fastDFSClient.uploadFile(file);
Map map=new HashMap<>();
map.put("url",s);
map.put("fileName",filename);
map.put("originalFilename",file.getOriginalFilename());
map.put("ex",ex);
return AjaxResult.success(map);
}
/**
* 通用上传请求(单个)
*/
@PostMapping("/uploadForBReport")
@ResponseBody
public Object uploadForBReport(@RequestParam("file_data") MultipartFile file) throws Exception
{
// Object object = OssUtils.standardAnnax(file);
String filename = file.getOriginalFilename();
String fileType = OssService.getExtensionName(filename);
String url = fastDFSClient.uploadFile(file);
String fileName = filename.substring(0, filename.lastIndexOf("."));
//设置 对应的文件信息
JSONArray fileArr = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("fileName",fileName);
obj.put("fileExtension",fileType);
obj.put("originalFilename",file.getOriginalFilename());
//此处 value 填写上面文件上传 返回的url
obj.put("fileAttachments",url);
fileArr.add(obj);
return AjaxResult.success(fileArr);
}
/**
* 通用上传请求(单个)
*/
@PostMapping("/upload2")
@ResponseBody
public Object uploadFile2(MultipartFile file) throws Exception
{
Object object = OssUtils.standardAnnax2(file);
return object;
}
/**
* 通用上传请求(单个)
*/
@PostMapping("/upload3")
@ResponseBody
public Object uploadFile3(MultipartFile file) throws Exception
{
Object object = new OssUploadController().standardAnnax(file, false, false);
return object;
}
/**
* 通用上传请求(单个)
*/
@PostMapping("/test")
@ResponseBody
public Object test(String path) throws Exception
{
Object object = OssUtils.standardAnnax(new File(path));
return object;
}
//获取文件拓展名
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}else{
return null;
}
}
return filename;
}
//获取去除拓展名的文件名
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
}