Commit 61cf40b4 authored by 王业雄's avatar 王业雄

vcc线下还款 文件上传

parent 9b79f4aa
......@@ -282,6 +282,28 @@
<version>4.2.1</version>
<scope>provided</scope>
</dependency>
<!-- fastdfs start -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.25.2-RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- fastdfs end -->
</dependencies>
</project>
......@@ -13,6 +13,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;
@Slf4j
@RestController
......@@ -149,8 +150,11 @@ public class VccRest {
*/
@PostMapping("/offline_repay_file/upload")
public JsonResult fileUpload (MultipartFile file){
log.info("fileUpload | 开始上传打款凭证");
try {
return null;
Map<String, String> map = vccService.fileUpload(file);
log.info("fileUpload | 上传打款凭证成功map={}",map);
return JsonResult.buildSuccessResult("请求成功",map);
}catch (Exception e){
log.error("fileUpload | 文件上传时发生异常",e);
return JsonResult.buildErrorStateResult("文件上传时发生异常");
......
package cn.quantgroup.customer.service;
import java.io.IOException;
import java.io.InputStream;
/**
* fastDFS 接入 http://confluence.quantgroup.cn/x/bMU5AQ
*
* @author jingfeng.guo
* @since 2019-08-24 17:04
*/
public interface IFastDFSService {
/**
* 上传文件
*
* @param fileInput
* @param fileSize
* @param fileExtName 扩展名
* @return
* @throws IOException
*/
String uploadFile(InputStream fileInput, Long fileSize, String fileExtName) throws IOException;
/**
* 上传文件
*
* @param bytes
* @param fileExtName
* @return
* @throws IOException
*/
String uploadFile(byte[] bytes, String fileExtName) throws IOException;
/**
* 文件上传, 下载url 再传
*
* @param url
* @param fileExtName
* @return
* @throws IOException
*/
String uploadFile(String url, String fileExtName) throws IOException;
/**
* 上传文件 base64
*
* @param base64String
* @param fileExtName
* @return
* @throws IOException
*/
String uploadFileOfBase64String(String base64String, String fileExtName) throws IOException;
/**
* 下载文件
*
* @param path
* @return
* @throws IOException
*/
byte[] downloadFile(String path) throws IOException;
/**
* 转成一个临时可用的url
*
* @param path
* @return
* @throws IOException
*/
String toUrl(String path) throws IOException;
}
......@@ -5,6 +5,8 @@ import cn.quantgroup.customer.rest.param.vcc.UserPreRepayInfoQuery;
import cn.quantgroup.customer.rest.vo.JsonResult;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
public interface IVccService {
JsonResult queryPage(UserPreRepayInfoQuery query) throws Exception;
......@@ -18,7 +20,7 @@ public interface IVccService {
void approvalResult( String serialNo, String remark, Integer status ,String token) throws Exception;
String fileUpload(MultipartFile file) throws Exception;
Map<String,String> fileUpload(MultipartFile file) throws Exception;
JsonResult queryApplyRecord(UserPreRepayInfoQuery query) throws Exception;
......
package cn.quantgroup.customer.service.impl;
import cn.quantgroup.customer.service.IFastDFSService;
import cn.quantgroup.customer.util.ProtoCommon;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author xing.yuan
*/
@Service
@Slf4j
public class FastDFSServiceImpl implements IFastDFSService {
@Resource
private FastFileStorageClient storageClient;
@Value("${fdfs.secret_key}")
private String secretKey;
@Value("${fdfs.domain}")
private String fastDfsHttp;
/**
* 文件上传
*
* @param fileInput
* @param fileSize
* @param fileExtName 扩展名
* @return
*/
@Override
public String uploadFile(InputStream fileInput, Long fileSize, String fileExtName) throws IOException {
StorePath storePath = null;
try {
storePath = storageClient.uploadFile(fileInput, fileSize, fileExtName, null);
} finally {
if (fileInput != null) {
try {
fileInput.close();
} catch (IOException e) {
}
}
}
if (storePath != null && StringUtils.isNotBlank(storePath.getFullPath())) {
return storePath.getFullPath();
} else {
throw new IOException("文件上传失败");
}
}
/**
* 文件上传
*
* @param bytes
* @param fileExtName 扩展名
* @return
*/
@Override
public String uploadFile(byte[] bytes, String fileExtName) throws IOException {
return uploadFile(new ByteArrayInputStream(bytes), (long) bytes.length, fileExtName);
}
/**
* 文件上传, 下载url 再传
*
* @param url
* @param fileExtName
* @return
* @throws IOException
*/
@Override
public String uploadFile(String url, String fileExtName) throws IOException {
InputStream in = null;
long size = 0;
HttpURLConnection conn = null;
try {
URL httpUrl = new URL(url);
conn = (HttpURLConnection) httpUrl.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
in = conn.getInputStream();
size = in.available();
} catch (IOException e) {
log.warn("下载文件异常,url={},", url, e);
throw e;
}
log.info("准备上传文件; url={}, ", url);
try {
String path = uploadFile(in, size, fileExtName);
return path;
} finally {
if (conn != null) {
try {
conn.disconnect();
} catch (Exception e) {
}
}
}
}
/**
* 上传文件 base64
*
* @param base64String
* @param fileExtName
* @return
* @throws IOException
*/
@Override
public String uploadFileOfBase64String(String base64String, String fileExtName) throws IOException {
byte[] bytes = Base64.decodeBase64(base64String);
return uploadFile(bytes, fileExtName);
}
/**
* 下载文件
*
* @param path
* @return
*/
@Override
public byte[] downloadFile(String path) throws IOException {
if (StringUtils.isBlank(path)) {
return null;
}
String[] split = StringUtils.split(path, "/", 2);
if (split.length < 2) {
throw new IOException("路径不对");
}
byte[] bytes = storageClient.downloadFile(split[0], split[1], new DownloadByteArray() {
});
return bytes;
}
/**
* 转成一个临时可用的url
* @param path
* @return
* @throws IOException
*/
@Override
public String toUrl(String path) throws IOException {
if (StringUtils.isBlank(path)) {
return null;
}
String fileName = path;
if (fileName.indexOf("/M") > 0) {
fileName = fileName.substring(fileName.indexOf("/M") + 1);
}
int lts = (int) (System.currentTimeMillis() / 1000);
// 初始化secret_key
try {
String token = ProtoCommon.getToken(fileName, lts, secretKey);
return fastDfsHttp + "/" + path + "?token=" + token + "&ts=" + lts;
} catch (Exception e) {
log.warn("生成FastDFS下载链接失败:path:{},", path, e);
throw new IOException("生成下载链接失败");
}
}
}
......@@ -11,6 +11,7 @@ import cn.quantgroup.customer.rest.param.vcc.OfflineRepaySubmitParam;
import cn.quantgroup.customer.rest.param.vcc.UserPreRepayInfoQuery;
import cn.quantgroup.customer.rest.vo.JsonResult;
import cn.quantgroup.customer.rest.vo.vcc.QueryPreOfflineRepayVo;
import cn.quantgroup.customer.service.IFastDFSService;
import cn.quantgroup.customer.service.IOpSystemService;
import cn.quantgroup.customer.service.IVccService;
import cn.quantgroup.customer.service.http.IHttpService;
......@@ -56,6 +57,8 @@ public class VccServiceImpl implements IVccService {
private OfflineRepayOperateRecordRepo offlineRepayOperateRecordRepo;
@Autowired
private IOpSystemService IOpSystemService;
@Autowired
private IFastDFSService fastDfsService;
@Override
public JsonResult queryPage(UserPreRepayInfoQuery query) throws Exception{
......@@ -250,9 +253,16 @@ public class VccServiceImpl implements IVccService {
}
@Override
public String fileUpload(MultipartFile file) throws Exception {
return null;
public Map<String,String> fileUpload(MultipartFile file) throws Exception {
String name = file.getName();
log.info("fileUpload | filename={}",name);
String substring = name.substring(name.lastIndexOf(".") + 1);
String baseUrl = fastDfsService.uploadFile(file.getBytes(), substring);
String viewUrl = fastDfsService.toUrl(baseUrl);
Map<String,String> map = new HashMap<>();
map.put("baseUrl",baseUrl);
map.put("viewUrl",viewUrl);
return map;
}
@Override
......
package cn.quantgroup.customer.util;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
/**
* protocol common functions
*
* @author jie.feng
*/
public class ProtoCommon {
private ProtoCommon() {
}
/**
* md5 function
*
* @param source the input buffer
* @return md5 string
*/
public static String md5(byte[] source) throws NoSuchAlgorithmException {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest();
char str[] = new char[32];
int k = 0;
for (int i = 0; i < 16; i++) {
str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
str[k++] = hexDigits[tmp[i] & 0xf];
}
return new String(str);
}
/**
* get token for file URL
*
* @param remote_filename the filename return by FastDFS server
* @param ts unix timestamp, unit: second
* @param secret_key the secret key
* @return token string
*/
public static String getToken(String remote_filename, int ts, String secret_key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
byte[] bsFilename = remote_filename.getBytes(Charset.defaultCharset());
byte[] bsKey = secret_key.getBytes(Charset.defaultCharset());
byte[] bsTimestamp = (new Integer(ts)).toString().getBytes(Charset.defaultCharset());
byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length];
System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length);
System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length);
System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length);
return md5(buff);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment