| github代码地址 | https://github.com/Tom-shushu/work-study |
| 接口文档有道云 | https://note.youdao.com/s/GShGsYE8 |
| 接口文档离线版本 | https://files.cnblogs.com/files/Tom-shushu/%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.rar?t=1682958343&download=true |
怎么说呢,从去年十二月份(就是我发最后一篇文章时间)到现在已经四五个月了,这段时间感觉生活很乱,我在安安心心上班、边上班边学习新知识新技术然后跳槽到大厂、边上班边考《系统架构设计师》这三件事情之间徘徊犹豫一直持续到现在,所以导致一样事情也没有干好 ------- 总结一句:为什么没有更博客呢?一个字,就是懒,嘿嘿~
还有一个原因:就是最近朋友给介绍了一个对象,比较忙(*^▽^*)
为什么发布这篇文档转换的文章呢?因为上周我要将一个PDF转换为Word,结果百度谷歌了所有文章,最终的结果都是“能转换,但是只能转换一点点,多了就要收费”,于是乎我突发奇想、心血来潮在放假的那天打算开发一款小程序实现各种文档的转换,在百度了一下午后发现目前都是借助Aspose实现的,但是好像要收费,在我新建项目时偶然间发现原来Maven仓库里面居然有人将破解好的Jar包上传到Maven中央仓库了,于是我测试了一下,哈哈真香,于是就有了这篇文章。至于小程序做的怎么样了呢?暂时又搁置了,因为我调查了一下已经有现成的好多优秀的微信小程序可以实现各种文档转换了,还有就是个人小程序没法上线,可能暂时不会做小程序了,大家有想法的可以按照自己的想法使用我的源码,直接和前端对接做出优秀的小程序。
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-pdf</artifactId>
<version>23.1</version>
</dependency>
/**
* @description: 获取文件保存地址
* @return: java.lang.String
* @author: zhouhong
* @date: 2023/4/30 12:36
*/
public String getSavePath() {
ApplicationHome applicationHome = new ApplicationHome(this.getClass());
// 保存目录位置根据项目需求可随意更改
return applicationHome.getDir().getParentFile()
.getParentFile().getAbsolutePath() + "\\src\\main\\resources\\templates\\";
}
/**
* @description: 上传文件到阿里云OSS
* @return: java.lang.String
* @author: zhouhong
* @date: 2023/5/1 22:55
*/
public String uploadOssFile(String fileName, File file){
// 创建OSSClient实例。
OSS ossClient = ossConfig.getOssClient();
try {
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),
fileName, file);
putObjectRequest.setProcess("true");
// 上传文件。
PutObjectResult result = ossClient.putObject(putObjectRequest);
// 如果上传成功,则返回200。
if (result.getResponse().getStatusCode() == 200) {
return result.getResponse().getUri();
}
} catch (OSSException oe) {
} catch (ClientException ce) {
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return null;
}
/**
* @description: PDF 转其他文件
* @return: java.util.List<java.lang.String>
* @author: zhouhong
* @date: 2023/5/1 23:34
*/
@Override
public List<String> pdfToFile(MultipartFile file,String type) {
List<String> res = new ArrayList<>();
String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
if (!"pdf".equals(checkType)) {
throw new ServiceException(1, "输入文件不是PDF文件!");
}
try {
switch (type.toUpperCase()) {
case "WORD" : {
return switchFile(file, com.aspose.pdf.SaveFormat.DocX, "docx");
}
case "XML" : {
return switchFile(file, SaveFormat.PdfXml, "xml");
}
case "EXCEL" : {
return switchFile(file, com.aspose.pdf.SaveFormat.Excel, "xlsx");
}
case "PPT" : {
return switchFile(file, com.aspose.pdf.SaveFormat.Pptx, "pptx");
}
case "PNG" : {
// 图片类型的需要获取每一页PDF,一张一张转换
Document pdfDocument = new Document(file.getInputStream());
//分辨率
Resolution resolution = new Resolution(130);
PngDevice pngDevice = new PngDevice(resolution);
//
if (pdfDocument.getPages().size() <= 10) {
for (int index = 0; index < pdfDocument.getPages().size(); index++) {
String fileName = UUID.randomUUID() + ".png";
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
File tmpFile = new File(filePath);
FileOutputStream fileOS = new FileOutputStream(tmpFile);
pngDevice.process(pdfDocument.getPages().get_Item(index), fileOS);
res.add(ossUpLoadTools.uploadOssFile(fileName, tmpFile));
fileOS.close();
tmpFile.delete();
}
} else {
throw new ServiceException(2, "抱歉超过10页暂时无法转图片");
}
return res;
}
case "HTML" : {
String fileName = UUID.randomUUID() + ".html";
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
Document doc = new Document(file.getInputStream());
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setFixedLayout(true);
saveOptions.setSplitIntoPages(false);
saveOptions.setRasterImagesSavingMode(HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg);
doc.save(filePath , saveOptions);
doc.close();
File outputfile = new File(filePath);
res.add(ossUpLoadTools.uploadOssFile(fileName, outputfile));
outputfile.delete();
return res;
}
default:{}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private List<String> switchFile(MultipartFile file, SaveFormat saveFormat, String suffix) {
List<String> resUrl = new ArrayList<>();
try {
long old = System.currentTimeMillis();
// 输出路径
String fileName = UUID.randomUUID() + "." + suffix;
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
FileOutputStream os = new FileOutputStream(filePath);
Document doc = new Document(file.getInputStream());
doc.save(os, saveFormat);
os.close();
doc.close();
File outputfile = new File(filePath);
resUrl.add(ossUpLoadTools.uploadOssFile(fileName, outputfile));
outputfile.delete();
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒");
}catch (IOException e) {
e.printStackTrace();
}
return resUrl;
}
/**
* @description: 合并两个PDF文件
* @return: java.lang.String
* @author: zhouhong
* @date: 2023/5/1 23:40
*/
@Override
public String mergeTwoPdfFile(MultipartFile file1, MultipartFile file2) {
try {
Document doc1 = new Document(file1.getInputStream());
Document doc2 = new Document(file2.getInputStream());
doc1.getPages().add(doc2.getPages());
String fileName = UUID.randomUUID() + ".pdf";
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
doc1.save(filePath);
doc1.close();
File outputfile = new File(filePath);
String res = ossUpLoadTools.uploadOssFile(fileName, outputfile);
outputfile.delete();
return res;
} catch (IOException e){
e.printStackTrace();
}
return null;
}
/**
* @description: 合并对个PDF文件
* @return: java.lang.String
* @author: zhouhong
* @date: 2023/5/1 23:40
*/
@Override
public String mergeMorePdfFile(MultipartFile ... file) {
try {
String mergeFileName = UUID.randomUUID() + ".pdf";
String mergePdfPath = ossUpLoadTools.getSavePath() + "/" + mergeFileName;
String[] chilPdfPath = new String[file.length];
// 读取PDF并获取路径
for (int i = 0; i < file.length; i++) {
String fileName = UUID.randomUUID() + ".pdf";
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
FileOutputStream os = new FileOutputStream(filePath);
Document doc = new Document(file[i].getInputStream());
doc.save(os);
chilPdfPath[i] = filePath;
os.close();
doc.close();
}
// 合并多个PDF
PdfFileEditor pdfFileEditor = new PdfFileEditor();
pdfFileEditor.concatenate(chilPdfPath, mergePdfPath);
// 读取文件上传OSS
File outputfile = new File(mergePdfPath);
String resUrl = ossUpLoadTools.uploadOssFile(mergeFileName, outputfile);
outputfile.delete();
return resUrl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-cells</artifactId>
<version>22.10</version>
</dependency>
/**
* @description: Excel转其他文件
* @return: java.lang.String
* @author: zhouhong
* @date: 2023/5/1 23:44
*/
@Override
public String excelToFile(MultipartFile file, String type) {
String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
if (!"xlsx".equals(checkType) && !"xls".equals(checkType)) {
throw new ServiceException(1, "输入文件不是Excel文件!");
}
try {
switch (type.toUpperCase()) {
/******************** 文档类型 ***************/
case "WORD" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.DOCX, "docx");
}
case "PDF" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.PDF, "pdf");
}
case "PPT" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.PPTX, "pptx");
}
case "HTML" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.HTML, "html");
}
case "JSON" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.JSON, ".json");
}
case "MARKDOWN" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.MARKDOWN, "md");
}
/***************** 图片类型 (注意图片格式的默认只转换第一个 Sheet1)*********************/
case "PNG" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.PNG, "png");
}
case "JPG" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.JPG, "jpg");
}
case "BMP" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.BMP, "bmp");
}
case "CSV" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.CSV, "csv");
}
case "SVG" : {
return SwitchFile(file, com.aspose.cells.SaveFormat.SVG, "svg");
}
// 好像有问题,有需要大家自己调试一下
// case "XML" : {
// return SwitchFile(file, com.aspose.cells.SaveFormat.XML, "xml");
// }
default:{}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String SwitchFile(MultipartFile file, int saveFormat, String suffix) {
String url = "";
try {
long old = System.currentTimeMillis();
String fileName = UUID.randomUUID() + "." + suffix;
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
FileOutputStream os = new FileOutputStream(filePath);
//加载源文件数据
Workbook excel = new Workbook(file.getInputStream());
//设置转换文件类型并转换
excel.save(os, saveFormat);
os.close();
File outputfile = new File(filePath);
url = ossUpLoadTools.uploadOssFile(fileName, outputfile);
outputfile.delete();
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version>
</dependency>
@Override
public String wordToFile(MultipartFile file, String type) {
String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
if (!"doc".equals(checkType) && !"docx".equals(checkType)) {
throw new ServiceException(1, "输入文件不是Word文件!");
}
try {
switch (type.toUpperCase()) {
case "TEXT" : {
return switchFile(file, SaveFormat.TEXT, "txt");
}
case "PDF" : {
return switchFile(file, com.aspose.words.SaveFormat.PDF, "pdf");
}
/*************** 需要操作每一页Word文件,一般Word类的直接电脑操作,应该用不上************/
// case "PNG" : {
// return switchFile(file, com.aspose.words.SaveFormat.PNG, "png");
// }
// case "JPG" : {
// return switchFile(file, com.aspose.words.SaveFormat.JPEG, "jpg");
// }
default:{}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String switchFile(MultipartFile file, int saveFormat, String suffix){
String url = "";
try {
long old = System.currentTimeMillis();
// 输出路径
String fileName = UUID.randomUUID() + "." + suffix;
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
FileOutputStream os = new FileOutputStream(filePath);
com.aspose.words.Document doc = new com.aspose.words.Document(file.getInputStream());
doc.save(os, saveFormat);
os.close();
File outputfile = new File(filePath);
url = ossUpLoadTools.uploadOssFile(fileName, outputfile);
outputfile.delete();
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒");
}catch (Exception e) {
e.printStackTrace();
}
return url;
}
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-slides</artifactId>
<version>23.1</version>
</dependency>
@Override
public String PptToFile(MultipartFile file, String type) {
// 获取文件后缀名
String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
if (!"ppt".equals(checkType) && !"pptx".equals(checkType)) {
throw new ServiceException(1, "输入文件不是PPT文件!");
}
try {
switch (type.toUpperCase()) {
case "HTML" : {
return SwitchFile(file, com.aspose.slides.SaveFormat.Html, "html");
}
case "HTML5" : {
return SwitchFile(file, com.aspose.slides.SaveFormat.Html5, "html");
}
case "PDF" : {
return SwitchFile(file, com.aspose.slides.SaveFormat.Pdf, "pdf");
}
default:{}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String SwitchFile(MultipartFile file, int saveFormat, String suffix) {
String url = "";
try {
long old = System.currentTimeMillis();
String fileName = UUID.randomUUID() + "." + suffix;
String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
FileOutputStream os = new FileOutputStream(filePath);
//加载源文件数据
Presentation ppt = new Presentation(file.getInputStream());
//设置转换文件类型并转换
ppt.save(os, saveFormat);
os.close();
File outputfile = new File(filePath);
url = ossUpLoadTools.uploadOssFile(fileName, outputfile);
// 删除临时文件
outputfile.delete();
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒");
return url;
}catch (IOException e) {
e.printStackTrace();
}
return url;
}
我有一个 cs.pdf 的PDF文件,通过调用PDF 转其他文件的接口,将其转换为 Wprd 形式

通过访问返回的地址就可以发现,文件已经被转换为Word格式的文件啦~


我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[