在此,特别感谢开源网站Spiderflow 开源爬虫软件
Gitee:spider-flow
虽然看不懂源码,但是根据个人需要
原来的开源Oss插件中
为了不在爬虫设置中写死上传后的文件名
便做了部分二次开发
源代码为:
增加了一个方法是不需要传filePath
这样,我上传完后的filePath就可以根据上传时间+原文件名而生成,从而实现相关功能
public static OssFile ossUpload(String url,String ossId) throws Exception {
Oss oss = ossService.getOss(ossId);
if(null != oss) {
URL urlObj = null;
try {
System.out.println(url);
urlObj = new URL(url);
DataInputStream dataInputStream = new DataInputStream(urlObj.openStream());
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
//buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = dataInputStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
String filePath = (MinioUtil.getOriginalFileName(url));
String contentType = MediaTypeFactory.getMediaType(filePath).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
filePath = MinioUtil.extractFilename(filePath,contentType);
byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果
OssFile ossFile = MinioUtil.uploadFileToMinio(oss, in_b, filePath);
ossFile.setOriginUrl(url);
ossFile.setCreateDate(new Date());
ossFileService.save(ossFile);
return ossFile;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}