Commit c43a0f75 authored by 单奇雲's avatar 单奇雲

新增pdf 导航查看及 文本预案下载接口

parent 8ea74792
......@@ -95,6 +95,11 @@
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -14,12 +14,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -234,8 +237,17 @@ public class FileController extends BaseController {
processData.put("html", "/" + filePath.substring(0, filePath.lastIndexOf(".")) + ".html");
return new CommonResponse(SUCCESS, processData, "查询成功");
} else {
}if(fileName.endsWith(".pdf")) {
Map<String, Object> processData = new HashMap<String, Object>();
MyNode node = PdfUtil.getPdfNavs(fileName);
processData.put("nodes", node.getChildren());
String filePath = obj.getString("file");
processData.put("html", "/" + filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf");
return new CommonResponse(SUCCESS, processData, "查询成功");
}else {
return new CommonResponse(SUCCESS, "文件类型不为doc/docx", "查询成功");
}
}
}
package com.yeejoin.amos.fas.business.controller;
import com.yeejoin.amos.fas.business.service.intfc.IPlanVisual3dService;
import com.yeejoin.amos.fas.dao.entity.TextPlan;
import com.yeejoin.amos.op.core.common.response.CommonResponse;
import com.yeejoin.amos.op.core.util.CommonResponseUtil;
import com.yeejoin.amos.security.authorization.Authorization;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
......@@ -22,9 +37,18 @@ public class PlanVisual3dController extends BaseController {
private final Logger log = LoggerFactory.getLogger(PlanVisual3dController.class);
// 上传路径
@Value("${file.uploadUrl}")
private String fileUploadDir;
@Autowired
private IPlanVisual3dService planVisual3dService;
@Autowired
HttpServletResponse response;
@Autowired
HttpServletRequest request;
@ApiOperation(value = "预案应用树", notes = "预案应用树")
@GetMapping(value = "/plan/tree")
public CommonResponse getPlanTree() {
......@@ -37,6 +61,51 @@ public class PlanVisual3dController extends BaseController {
return CommonResponseUtil.success(planVisual3dService.getTextPlanInfoById(id));
}
@Authorization(ingore = true)
@RequestMapping(value = "/plan/text/downloadFile/{id}", method = RequestMethod.GET)
public HttpServletResponse download(@PathVariable("id") Long id) {
TextPlan testPlan = planVisual3dService.getTextPlanInfoById(id);
if(testPlan != null) {
String path = testPlan.getFilePath();
if( path != null && !"".equals(path)) {
try {
// path是指欲下载的文件的路径。
File file = new File(fileUploadDir + path);
if(file.exists()) {
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(fileUploadDir + path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
// response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/x-download");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
}else {
response.setStatus(404);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}else {
response.setStatus(404);
}
return response;
}
/**
* 资源设备信息查询
*/
......
......@@ -149,4 +149,5 @@ public class FileUtil {
}
return CommonResponseUtil.failure("上传失败");
}
}
......@@ -12,6 +12,8 @@ public class MyNode {
private String text;
private List<MyNode> children = new ArrayList<>();
private MyNode parent;
private String pageInfo;
public String getId() {
return id;
}
......@@ -65,4 +67,16 @@ public class MyNode {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "MyNode [id=" + id + ", text=" + text + ", children=" + children + "]";
}
public String getPageInfo() {
return pageInfo;
}
public void setPageInfo(String pageInfo) {
this.pageInfo = pageInfo;
}
}
package com.yeejoin.amos.fas.core.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.SimpleBookmark;
public class PdfUtil {
private static Integer index = 1;
public static MyNode getPdfNavs(String pdfUrl) {
MyNode root = new MyNode();
try {
PdfReader reader = new PdfReader(pdfUrl);
List<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader);
if(list!=null && list.size()>0) {
index = 1;
root.setId(String.valueOf(index));
for (Iterator<HashMap<String, Object>> i = list.iterator(); i.hasNext();) {
showBookmark(i.next(), root);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return root;
}
// 获取标题
public static void showBookmark(HashMap<String, Object> bookmark, MyNode parentNode) {
List<MyNode> children = parentNode.getChildren();
MyNode n = new MyNode();
n.setId(String.valueOf(index++));
n.setPageInfo("page=" + getPageNumbers(bookmark));
n.setText((String) bookmark.get("Title"));
children.add(n);
// System.out.println (bookmark.get ( "Title" )) ;
// System.out.println(getPageNumbers( bookmark));
@SuppressWarnings("unchecked")
ArrayList<HashMap<String, Object>> kids = (ArrayList<HashMap<String, Object>>) bookmark.get("Kids");
if (kids == null)
return;
for (Iterator<HashMap<String, Object>> i = kids.iterator(); i.hasNext();) {
showBookmark(i.next(), n);
}
}
// 获取页码
public static Integer getPageNumbers(HashMap<String, Object> bookmark) {
if (bookmark == null)
return null;
if ("GoTo".equals(bookmark.get("Action"))) {
String page = (String) bookmark.get("Page");
if (page != null) {
page = page.trim();
int idx = page.indexOf(' ');
int pageNum;
if (idx < 0) {
pageNum = Integer.parseInt(page);
} else {
pageNum = Integer.parseInt(page.substring(0, idx));
}
return pageNum;
}
}
return null;
}
}
\ No newline at end of file
......@@ -258,6 +258,7 @@
</pluginRepositories>
<build>
<plugins>
<plugin>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment