1,简介:
很常用的一个功能,也很简单
2,上代码:
/**
* 导出未通过主体-优化
* @param param
* @return
* @throws IOException
* @throws ApiException
*/
@Override
public ResponseBean exportNoPassOrderListV2(NoPassOrderVo param) throws IOException, ApiException {
List list = this.commonExportMapper.queryNoPassOrderExportList2(param);
String filePath = downLoadOrderInfo(list, ExportNoPassOrder.class, excelExportPath, “未通过主体”);
return uploadExcelToFtpForDownloadV2(filePath);
}
@Data
public class ExportNoPassOrder extends BaseExportOrderBean {
/**
* 姓名
*/
@ExcelProperty("姓名")
private String custName;
/**
* 身份证号
*/
@ExcelProperty("身份证号")
private String custIdentifyCard;
@Component
@Slf4j
public abstract class AbstractCommonExportService {
@Value("${outSideService.uploadFileServiceUrl}")
String uploadFileServiceUrl;
@Value("${outSideService.uploadFileProd}")
String uploadFileProd;
private static final String FILE_POST_FIX = ".xlsx";
private static final String DEFAULT_PORT = "8000";
protected String downLoadOrderInfo(List<? extends BaseExportOrderBean> exportOrderBeanList, Class<?> className, String excelExportPath, String fileName) throws FileNotFoundException {
String exportFilePath = new StringBuilder(excelExportPath).append(fileName).append(System.currentTimeMillis()).append(FILE_POST_FIX).toString();
OutputStream outputStream = new FileOutputStream(exportFilePath);
EasyExcel.write(outputStream, className).sheet(fileName).registerWriteHandler(new FixColumnWidthStyleStrategy()).doWrite(exportOrderBeanList);
return exportFilePath;
}
protected HkmResponse uploadExcelToFtpForDownload(String filePath) throws IOException, ApiException {
log.info("本地文件路径是{}",filePath);
Map map = uploadFile(filePath);
Integer code = (Integer) map.get("code");
log.info("调用文件上传服务的code是的{},路径是{}",code,map.get("fileUrl"));
if (code == 1){
return HkmResponse.builder().data(map.get("fileUrl")).code(CheckResult.SUCCESS).msg("下载成功").build();
} else {
return HkmResponse.builder().data(map.get("fileUrl")).code(CheckResult.FAIL).msg("下载失败").build();
}
}
/**
* 上传到ftp优化
*/
// protected ResponseBean uploadExcelToFtpForDownloadV2(String filePath) throws IOException, ApiException {
//
// log.info(“本地文件路径是{}”,filePath);
// Map map = uploadFile(filePath);
// Integer code = (Integer) map.get(“code”);
// log.info(“调用文件上传服务的code是的{},路径是{}”,code,map.get(“fileUrl”));
// if (code == 1){
// return ResponseBean.success(map.get(“fileUrl”),“下载成功”);
// } else {
// return ResponseBean.fail(map.get(“fileUrl”),CheckResult.FAIL,“下载成功”);
// }
//
// }
/**
* 优化后的下载方法
* @param filePath
* @return
* @throws IOException
* @throws ApiException
*/
protected ResponseBean uploadExcelToFtpForDownloadV2(String filePath) throws IOException, ApiException {
log.info("本地文件路径是{}",filePath);
Map<String, Object> map = uploadFile(filePath);
Integer code = (Integer) map.get("code");
log.info("调用文件上传服务的code是的{},路径是{}",code,map.get("fileUrl"));
if (code == 1){
return ResponseBean.success(map.get("fileUrl"),"下载成功");
} else {
return ResponseBean.fail(map.get("fileUrl"),CheckResult.FAIL,"下载成功");
}
}
public Map<String,Object> uploadFile(String filePath) throws IOException, ApiException {
String successMsg = "上传成功";
String errorMsg = "上传失败";
Map<String,Object> resultMap = new HashMap(Constant.MAP_DEFAULT_SIZE);
try {
String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);
FSUploadFileParam fsUploadFileParam = new FSUploadFileParam();
MultipartFile testFile = new MockMultipartFile(fileName,fileName,"",new FileInputStream(new File(filePath)));
fsUploadFileParam.setFile(testFile);
fsUploadFileParam.setProductCode("SJZN.AHND");
MlyunFSUploadRequest mlyunFSUploadRequest = new MlyunFSUploadRequest(fsUploadFileParam);
DefaultMlyunClient defaultMlyunClient = new DefaultMlyunClient(uploadFileServiceUrl);
MlyunResponse response = defaultMlyunClient.execute(mlyunFSUploadRequest);
log.info("上传文件到***返回结果:{}",response.toString());
JSONObject jsonObject = JSONObject.parseObject(response.getBody());
String fileUrl = jsonObject.getString("data");
if(fileUrl.contains(DEFAULT_PORT)){
fileUrl = fileUrl.replace(uploadFileServiceUrl, uploadFileProd);
}
resultMap.put("code", 1);
resultMap.put("msg", successMsg);
resultMap.put("fileUrl", fileUrl + "?attName="+fileName);
return resultMap;
} catch (ApiException e) {
resultMap.put("code", 0);
resultMap.put("msg", errorMsg);
log.error(e.getMessage());
return resultMap;
}
}
}