InputStream 内容读取
生成空文件
判断操作系统
空pdf文件生成
InputStream 获取
生成空文件
@RequestMapping("fileFree")
public String fileFree() throws Exception {
String winLocationpath = "D:/transfer/";
String linuxLocationpath = "/user/transfer/";
String path = "";
String fileName = "签名文本.txt";
OsInfo osInfo = SystemUtil.getOsInfo();//获取当前操作系统信息
if (osInfo.isWindows()) {
path = winLocationpath;
}
if (osInfo.isLinux()) {
path = winLocationpath;
}
File file = new File(path + fileName);
//判断文本文件是否存在(注意是文本文件)
if (!FileUtil.exist(file)) {
FileUtil.touch(file);//创建空文本文件
}
FileUtil.appendUtf8String("我是追加的内容", file);
InputStream inputstream = new FileInputStream(file);
//获取流信息,可以定位问题,到底有没有获取文件流信息
System.out.println("文件size:" + String.valueOf(inputstream.available() / 1000) + "k");
IoUtil.close(inputstream);//注意要关流
return "";
}
空pdf文件生成
@RequestMapping("fileFout")
public String fileFout() throws Exception {
String winLocationpath = "D:/transfer/";
String linuxLocationpath = "/user/transfer/";
String path = "";
String fileName = "签名文本.pdf";
OsInfo osInfo = SystemUtil.getOsInfo();//获取当前操作系统信息
if (osInfo.isWindows()) {
path = winLocationpath;
}
if (osInfo.isLinux()) {
path = winLocationpath;
}
File file = new File(path + fileName);
//判断文本文件是否存在(注意是pdf文件)
if (!FileUtil.exist(file)) {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file));//生成pdf模板
document.open();
document.add(new Paragraph(" "));//这地方必须要有内容
document.close();
}
InputStream inputstream = new FileInputStream(file);
System.out.println("文件size:" + String.valueOf(inputstream.available() / 1000) + "k");
IoUtil.close(inputstream);
return "";
}
pdf参考:https://www.jianshu.com/p/9050cb2ae707
pdf参考:https://www.jianshu.com/p/8865433102cf
InputStream 获取
File file = new File(path + fileName);
InputStream inputstream = new FileInputStream(defaultfile);
用到的依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>