FastDFSClient.java
8.94 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.lhcredit.framework.web.service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.service.TrackerClient;
import com.lhcredit.common.utils.FileSysUtils;
import com.lhcredit.project.business.bCreditReport.domain.BCreditReport;
import com.lhcredit.project.business.bCreditReport.service.IBCreditReportService;
import com.lhcredit.project.business.bReportTable.service.IBReportTableService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
@RestController
@Component
@RequestMapping("web/test/")
public class FastDFSClient {
private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private FdfsWebServer fdfsWebServer;
@Autowired
private IBCreditReportService bCreditReportService;
@Autowired
private IBReportTableService tableService;
// @RequestMapping("aaaa")
public void test() throws IOException {
List<BCreditReport> bCreditReports = bCreditReportService.selectBCreditReportList(null);
bCreditReports=bCreditReports.stream().sorted(Comparator.comparing(BCreditReport::getCreateTime)).collect(Collectors.toList());
List<String> list=new ArrayList<>();
for (BCreditReport bCreditReport : bCreditReports) {
System.out.println(bCreditReport.getId());
try{
//跳出生成doc 为null 的
if (StringUtils.isEmpty(bCreditReport.getFirstUrl())) continue;
String doc=null;
if (StringUtils.isNotEmpty(bCreditReport.getFirstUrl())){
JSONArray arr = JSONArray.parseArray(bCreditReport.getFirstUrl());
JSONObject jsonObject = arr.getJSONObject(0);
String fileAttachments=jsonObject.getString("fileAttachments");
String s = uploadFile(fileAttachments);
jsonObject.put("fileAttachments",s);
doc=s;
arr.set(0,jsonObject);
bCreditReport.setFirstUrl(arr.toJSONString());
}
if (StringUtils.isNotEmpty(bCreditReport.getFinalUrl())){
JSONArray arr = JSONArray.parseArray(bCreditReport.getFinalUrl());
JSONObject jsonObject = arr.getJSONObject(0);
String fileAttachments=jsonObject.getString("fileAttachments");
String s = uploadFile(fileAttachments);
jsonObject.put("fileAttachments",s);
arr.set(0,jsonObject);
bCreditReport.setFinalUrl(arr.toJSONString());
}
if (StringUtils.isNotEmpty(bCreditReport.getPdfUrl())){
JSONArray arr = JSONArray.parseArray(bCreditReport.getPdfUrl());
JSONObject jsonObject = arr.getJSONObject(0);
String fileAttachments=jsonObject.getString("fileAttachments");
String s = uploadFile(fileAttachments);
jsonObject.put("fileAttachments",s);
arr.set(0,jsonObject);
bCreditReport.setPdfUrl(arr.toJSONString());
bCreditReport.setUrl(arr.toJSONString());
}
if (StringUtils.isNotEmpty(bCreditReport.getAnnex())&&bCreditReport.getAnnex().contains("{\"doc\"")){
JSONObject jsonObject = JSONObject.parseObject(bCreditReport.getAnnex());
JSONObject docF = jsonObject.getJSONObject("doc");
JSONObject sfExcel = jsonObject.getJSONObject("sfExcel");
JSONObject scoreCard = jsonObject.getJSONObject("scoreCard");
docF.put("fileAttachments",doc);jsonObject.put("doc",docF);
if (null!=sfExcel){
String s = uploadFile(sfExcel.getString("fileAttachments"));
sfExcel.put("fileAttachments",s);jsonObject.put("sfExcel",sfExcel);
}
if (null!=scoreCard){
String s = uploadFile(scoreCard.getString("fileAttachments"));
scoreCard.put("fileAttachments",s);jsonObject.put("scoreCard",scoreCard);
}
bCreditReport.setAnnex(jsonObject.toJSONString());
}
bCreditReportService.updateBCreditReport(bCreditReport);
}catch (Exception e){
e.printStackTrace();
list.add(bCreditReport.getId());
}
}
}
public String uploadFile(String fileUrl) throws IOException {
if (fileUrl.contains("fileStorage")){
throw new RuntimeException("文件已上传fileStorage");
}
if (fileUrl.contains("img30.lhdna.com")){
fileUrl=fileUrl.replace("img30.lhdna.com","file2.lhdna.com");
}
InputStream inputStream = HttpRequest.get(fileUrl).execute().bodyStream();
String s = FileSysUtils.upLoadFile(inputStream, fileUrl.substring(fileUrl.lastIndexOf("/")+1, fileUrl.length()), FileSysUtils.DirectoryType.creditManager.directory);
return FileSysUtils.getFullPath(s);
}
/**
* 上传文件
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public synchronized String uploadFile(MultipartFile file) throws IOException {
String path = FileSysUtils.getFullPath(FileSysUtils.upLoad(file));
return path;
// StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
// return getResAccessUrl(storePath);
}
/**
* 上传文件
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadFileORI(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
return storePath.getFullPath();
}
/**
* 上传头像
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadAvatar(MultipartFile file) throws IOException {
// StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), "png",null);
// return storePath.getFullPath();
String path = FileSysUtils.getFullPath(FileSysUtils.upLoad(file));
return path;
}
/**
* 上传微信头像
* @param urlStr 文件路径
* @return 文件访问地址
* @throws IOException
*/
public String uploadWx(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
StorePath storePath = storageClient.uploadFile(conn.getInputStream(),conn.getContentLength(), "jpeg",null);
return storePath.getFullPath();
}
/**
* 上传文件
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public synchronized String uploadFile(File file) {
String path = FileSysUtils.getFullPath(FileSysUtils.upLoadFile(file));
return path;
}
// 封装图片完整URL地址
private String getResAccessUrl(StorePath storePath) {
String fullpath=storePath.getFullPath();
String suffix = fullpath.substring(fullpath.indexOf("/"));
String fileUrl = fdfsWebServer.getWebServerUrl() + suffix;
return fileUrl;
}
/**
* 下载文件
* @param fileUrl 文件url
* @return
*/
public byte[] download(String fileUrl) {
return HttpRequest.post(fileUrl).execute().bodyBytes();
}
//全路径下载
public byte[] download2(String fileUrl) {
return HttpRequest.post(fileUrl).execute().bodyBytes();
}
}