| | |
| | | package com.ruoyi.file.controller; |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.utils.file.FileUtils; |
| | | import com.ruoyi.file.service.ISysFileService; |
| | | import com.ruoyi.file.utils.OBSUploadUtils; |
| | | import com.ruoyi.system.api.domain.poji.config.FileRecord; |
| | | import com.ruoyi.system.api.domain.poji.sys.SysFile; |
| | | import com.ruoyi.system.api.service.RemoteConfigService; |
| | | import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; |
| | | import com.wechat.pay.contrib.apache.httpclient.WechatPayUploadHttpPost; |
| | | import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier; |
| | | import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner; |
| | | import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials; |
| | | import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator; |
| | | import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestPart; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | import java.net.URI; |
| | | import java.security.PrivateKey; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 文件请求处理 |
| | |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(SysFileController.class); |
| | | |
| | | @Autowired |
| | | private final static String PRIVATE_KEY = ""; |
| | | |
| | | private final static String MCH_ID = "1631381314"; |
| | | |
| | | private final static String MCH_SERIAL_NO = ""; |
| | | |
| | | private final static String API_V3_KEY = "J8v9JDHKJYDHW9bdk30o2bdjy6z9mn1A"; |
| | | |
| | | |
| | | @Resource |
| | | private ISysFileService sysFileService; |
| | | |
| | | @Resource |
| | | private RemoteConfigService remoteConfigService; |
| | | |
| | | /** |
| | | * 文件上传请求 |
| | |
| | | try |
| | | { |
| | | // 上传并返回访问地址 |
| | | String fileName = file.getOriginalFilename(); |
| | | String prefix = fileName.substring(fileName.lastIndexOf(".")); |
| | | long fileSize = file.getSize(); |
| | | String url = sysFileService.uploadFile(file); |
| | | SysFile sysFile = new SysFile(); |
| | | sysFile.setName(FileUtils.getName(url)); |
| | | sysFile.setUrl(url); |
| | | //创建上传记录 |
| | | FileRecord fileRecord = new FileRecord(); |
| | | fileRecord.setFileName(fileName); |
| | | fileRecord.setFilePath(url); |
| | | fileRecord.setUploadType(2); |
| | | fileRecord.setUploadTime(new Date()); |
| | | fileRecord.setFileSize(fileSize); |
| | | fileRecord.setFileType(prefix); |
| | | remoteConfigService.createFileRecord(fileRecord); |
| | | return R.ok(sysFile); |
| | | } |
| | | catch (Exception e) |
| | |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 文件上传请求 |
| | | */ |
| | | @PostMapping("uploadOSS") |
| | | public R<String> uploadOSS(@RequestPart("file")MultipartFile file) |
| | | { |
| | | try |
| | | { |
| | | // 上传并返回访问地址 |
| | | String fileName = file.getOriginalFilename(); |
| | | String prefix = fileName.substring(fileName.lastIndexOf(".")); |
| | | long fileSize = file.getSize(); |
| | | String url = OBSUploadUtils.uploadFile(file); |
| | | //创建上传记录 |
| | | FileRecord fileRecord = new FileRecord(); |
| | | fileRecord.setFileName(fileName); |
| | | fileRecord.setFilePath(url); |
| | | fileRecord.setUploadType(2); |
| | | fileRecord.setUploadTime(new Date()); |
| | | fileRecord.setFileSize(fileSize); |
| | | fileRecord.setFileType(prefix); |
| | | remoteConfigService.createFileRecord(fileRecord); |
| | | return R.ok(url); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("上传文件失败", e); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @PostMapping("batchUploadOSS") |
| | | public R<List<String>> batchUploadOSS(@RequestPart("file") MultipartFile[] files) { |
| | | List<String> urls = new ArrayList<>(); |
| | | String fileName; |
| | | String prefix; |
| | | long fileSize; |
| | | FileRecord fileRecord; |
| | | for (MultipartFile file : files) { |
| | | try { |
| | | fileName = file.getOriginalFilename(); |
| | | prefix = fileName.substring(fileName.lastIndexOf(".")); |
| | | fileSize = file.getSize(); |
| | | // 上传并返回访问地址 |
| | | String url = OBSUploadUtils.uploadFile(file); |
| | | urls.add(url); |
| | | //创建上传记录 |
| | | fileRecord = new FileRecord(); |
| | | fileRecord.setFileName(fileName); |
| | | fileRecord.setFilePath(url); |
| | | fileRecord.setUploadType(2); |
| | | fileRecord.setUploadTime(new Date()); |
| | | fileRecord.setFileSize(fileSize); |
| | | fileRecord.setFileType(prefix); |
| | | remoteConfigService.createFileRecord(fileRecord); |
| | | } catch (Exception e) { |
| | | log.error("上传文件失败", e); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | } |
| | | return R.ok(urls); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * @description 创建httpClient |
| | | * @author jqs |
| | | * @date 2023/6/19 12:50 |
| | | * @param |
| | | * @return CloseableHttpClient |
| | | */ |
| | | private CloseableHttpClient createHttpClient() throws Exception { |
| | | |
| | | String privateKey = PRIVATE_KEY; |
| | | String mchId = MCH_ID; |
| | | String mchSerialNo = MCH_SERIAL_NO; |
| | | String apiV3Key = API_V3_KEY; |
| | | // 加载商户私钥(privateKey:私钥字符串) |
| | | PrivateKey merchantPrivateKey = PemUtil |
| | | .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8"))); |
| | | |
| | | // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥) |
| | | AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( |
| | | new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),apiV3Key.getBytes("utf-8")); |
| | | |
| | | // 初始化httpClient |
| | | CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create() |
| | | .withMerchant(mchId, mchSerialNo, merchantPrivateKey) |
| | | .withValidator(new WechatPay2Validator(verifier)).build(); |
| | | return httpClient; |
| | | } |
| | | |
| | | @PostMapping("uploadWXFile") |
| | | public R<String> uploadWXFile(@RequestPart("file")MultipartFile file) throws Exception { |
| | | URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload"); |
| | | CloseableHttpClient httpClient = createHttpClient(); |
| | | InputStream inputStream = file.getInputStream(); |
| | | String sha256 = DigestUtils.sha256Hex(inputStream); |
| | | HttpPost request = new WechatPayUploadHttpPost.Builder(uri) |
| | | .withImage(file.getName(), sha256, inputStream) |
| | | .build(); |
| | | CloseableHttpResponse response = httpClient.execute(request); |
| | | HttpEntity entity = response.getEntity(); |
| | | String s = EntityUtils.toString(entity); |
| | | System.out.println(s); |
| | | return R.ok(s); |
| | | } |
| | | |
| | | } |