本地pdf文件读取

//本地图片到前端响应spring

@RequestMapping("/getImage")
@ResponseBody
public void getImagesId(HttpServletResponse rp) {
String filePath = "G:\\1.jpg";
File imageFile = new File(filePath);
if (imageFile.exists()) {
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(imageFile);
os = rp.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];
while ((count = fis.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

//本地图片到前端响应struts action

public String getImagesId() throws IOException {
HttpServletResponse request = ServletActionContext.getResponse();
File imageFile = new File(filePath);
if (imageFile.exists()) {
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(imageFile);
os = request.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];
while ((count = fis.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
return null;
}

注意这里一定要return null

 

 

 

 投机取巧取出了报告名字和字符串中的年份file:///G:/QJF/git/lrm/src/main/webapp/reportYear/2017/2016-13_%E8%8D%AF%E5%AD%A6.pdf

匹配两个字符串A与B中间的字符串包含A与B:  表达式: A.*?B(“.“表示任意字符,“?”表示匹配0个或多个)  示例: Abaidu.comB  结果: Awww.apizl.comB  匹配两个字符串A与B中间的字符串包含A但是不包含B:  表达式: A.*?(?=B)  示例: Awww.apizl.comB  结果: Awww.apizl.com  匹配两个字符串A与B中间的字符串且不包含A与B:  表达式: (?<=A).*?(?=B)  这种写法没看懂,我猜测是如果不包含前面匹配的字符写法(?<=要匹配的开始字符),不包含后面要匹配的字符写法(?=要匹配的结束字符)  示例: Awww.baidu.comB  结果: www.baidu.com
-
著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:Guozeping
源地址:https://www.cnblogs.com/guozepingboke/articles/10763676.html
来源:博客园cnblogs
© 版权声明:本文为博主原创文章,转载请附上博文链接!

//正则保留文件名中的汉字
public String chineseReplace(String filename) {
String firstCellHzDec = filename.replaceAll("[^\u4E00-\u9FA5]", "");
return firstCellHzDec;
}
//正则取出年份
public String getYear(String filename) {
String reg="(?<=r\\\\).*?(?=\\\\)";
Pattern pattern = Pattern.compile(reg);

// 内容 与 匹配规则 的测试
Matcher matcher = pattern.matcher(filename);

if( matcher.find() ){
return matcher.group(0);
}else{
return null;
}
}

 

/**
  *
  * 读取某个文件夹下的所有文件夹和文件, 返回所有文件名
 * @param filepath String
  * @throws FileNotFoundException
  * @throws IOException
  * @return List<String>
  *
  */
public static void readfile(String filepath,List<String> fileList) throws Exception {
File file=new File(filepath);
if(file.isDirectory()==true) {
String[] files=file.list();
for (String filename : files) {

File subfile=new File(filepath+"\\"+filename);
if(subfile.isDirectory()==true) {
readfile(filepath+"\\"+filename,fileList);
}else {
if(!filename.equals("年度报告")) {
fileList.add(filepath+"\\"+filename);
}

}
}
}
}

posted @ 2020-07-09 16:34  单例  阅读(292)  评论(0编辑  收藏  举报