Commit 000322ae authored by qinhu's avatar qinhu

pdf

parent 8cb23f81
...@@ -61,7 +61,7 @@ public class EntityEnclosureController { ...@@ -61,7 +61,7 @@ public class EntityEnclosureController {
} else { } else {
return BaseResponse.errorMsg("登录信息失效"); return BaseResponse.errorMsg("登录信息失效");
} }
if (!extName.equals(".pdf")) { if (!extName.equals(".pdf") && (extName.equals(".xls") || extName.equals(".xlsx"))) {
String pdfPath = FastDFSUtils.conventAndUpload(file.getInputStream(), fileName); String pdfPath = FastDFSUtils.conventAndUpload(file.getInputStream(), fileName);
map.put("pdfPath", pdfPath); map.put("pdfPath", pdfPath);
} else { } else {
...@@ -133,7 +133,7 @@ public class EntityEnclosureController { ...@@ -133,7 +133,7 @@ public class EntityEnclosureController {
mapSub.put("fileUrl", picUrl); mapSub.put("fileUrl", picUrl);
mapSub.put("fileName", fileName); mapSub.put("fileName", fileName);
mapSub.put("extName", extName); mapSub.put("extName", extName);
if (!extName.equals(".pdf")) { if (!extName.equals(".pdf") && (extName.equals(".xls") || extName.equals(".xlsx"))) {
String pdfPath = FastDFSUtils.conventAndUpload(file.getInputStream(), fileName); String pdfPath = FastDFSUtils.conventAndUpload(file.getInputStream(), fileName);
mapSub.put("pdfPath", pdfPath); mapSub.put("pdfPath", pdfPath);
} else { } else {
......
package cn.wise.sc.cement.business.controller; package cn.wise.sc.cement.business.controller;
import cn.wise.sc.cement.business.model.BaseResponse; import cn.wise.sc.cement.business.model.BaseResponse;
import cn.wise.sc.cement.business.model.FileExt; import cn.wise.sc.cement.business.enumation.FileExt;
import cn.wise.sc.cement.business.model.PageQuery; import cn.wise.sc.cement.business.model.PageQuery;
import cn.wise.sc.cement.business.model.query.*; import cn.wise.sc.cement.business.model.query.*;
import cn.wise.sc.cement.business.model.vo.EntrustVo; import cn.wise.sc.cement.business.model.vo.EntrustVo;
......
...@@ -5,7 +5,7 @@ import cn.hutool.core.date.DateUtil; ...@@ -5,7 +5,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.wise.sc.cement.business.entity.EntityEnclosure; import cn.wise.sc.cement.business.entity.EntityEnclosure;
import cn.wise.sc.cement.business.model.BaseResponse; import cn.wise.sc.cement.business.model.BaseResponse;
import cn.wise.sc.cement.business.model.FileExt; import cn.wise.sc.cement.business.enumation.FileExt;
import cn.wise.sc.cement.business.model.IndustrialElementKey; import cn.wise.sc.cement.business.model.IndustrialElementKey;
import cn.wise.sc.cement.business.model.IndustrialReport; import cn.wise.sc.cement.business.model.IndustrialReport;
import cn.wise.sc.cement.business.model.PageQuery; import cn.wise.sc.cement.business.model.PageQuery;
...@@ -35,7 +35,6 @@ import org.springframework.web.bind.annotation.PostMapping; ...@@ -35,7 +35,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime; import java.time.LocalDateTime;
......
package cn.wise.sc.cement.business.model; package cn.wise.sc.cement.business.enumation;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/** /**
* @description: 文件扩展名 * @description: 文件扩展名
* @author: qh * @author: qh
* @create: 2020-10-16 13:40 * @create: 2020-10-16 13:40
**/ **/
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum FileExt { public enum FileExt {
//office后缀名 //office后缀名
......
package cn.wise.sc.cement.business.util; package cn.wise.sc.cement.business.util;
import cn.wise.sc.cement.business.model.FileExt; import cn.wise.sc.cement.business.enumation.FileExt;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
......
...@@ -21,186 +21,186 @@ import java.io.OutputStream; ...@@ -21,186 +21,186 @@ import java.io.OutputStream;
*/ */
public class FastDFSUtils { public class FastDFSUtils {
static { static {
try { try {
// ClientGlobal.initByProperties(FastDFSUtils.class.getClassLoader().getResource("fastdfs-client.properties").getPath()); // ClientGlobal.initByProperties(FastDFSUtils.class.getClassLoader().getResource("fastdfs-client.properties").getPath());
ClientGlobal.initByProperties("fastdfs-client.properties"); ClientGlobal.initByProperties("fastdfs-client.properties");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (MyException e) { } catch (MyException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static String[] uploadPic(String path, String fileName, long size) { public static String[] uploadPic(String path, String fileName, long size) {
String[] fileIds = null; String[] fileIds = null;
try { try {
// ClientGloble 读配置文件 // ClientGloble 读配置文件
// 老大客户端 // 老大客户端
TrackerClient trackerClient = new TrackerClient(); TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null; StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer); StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String extName = FilenameUtils.getExtension(fileName); String extName = FilenameUtils.getExtension(fileName);
NameValuePair[] meta_list = new NameValuePair[3]; NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName", fileName); meta_list[0] = new NameValuePair("fileName", fileName);
meta_list[1] = new NameValuePair("fileExt", extName); meta_list[1] = new NameValuePair("fileExt", extName);
meta_list[2] = new NameValuePair("fileSize", String.valueOf(size)); meta_list[2] = new NameValuePair("fileSize", String.valueOf(size));
// http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png // http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg // group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
fileIds = storageClient.upload_file(path, extName, meta_list); fileIds = storageClient.upload_file(path, extName, meta_list);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return fileIds; return fileIds;
} }
public static String[] uploadPic(InputStream inStream, String fileName, long size) { public static String[] uploadPic(InputStream inStream, String fileName, long size) {
String[] fileIds = null; String[] fileIds = null;
try { try {
// ClientGloble 读配置文件 // ClientGloble 读配置文件
// 老大客户端 // 老大客户端
TrackerClient trackerClient = new TrackerClient(); TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null; StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer); StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String extName = FilenameUtils.getExtension(fileName); String extName = FilenameUtils.getExtension(fileName);
NameValuePair[] meta_list = new NameValuePair[3]; NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName", fileName); meta_list[0] = new NameValuePair("fileName", fileName);
meta_list[1] = new NameValuePair("fileExt", extName); meta_list[1] = new NameValuePair("fileExt", extName);
meta_list[2] = new NameValuePair("fileSize", String.valueOf(size)); meta_list[2] = new NameValuePair("fileSize", String.valueOf(size));
// http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png // http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg // group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
fileIds = storageClient.upload_file(null, size, new UploadFileSender(inStream), extName, meta_list); fileIds = storageClient.upload_file(null, size, new UploadFileSender(inStream), extName, meta_list);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return fileIds; return fileIds;
} }
public static byte[] fileDownload(String fileUrl) { public static byte[] fileDownload(String fileUrl) {
byte[] by = null; byte[] by = null;
TrackerClient trackerClient = null; TrackerClient trackerClient = null;
TrackerServer trackerServer = null; TrackerServer trackerServer = null;
StorageClient1 storageClient1 = null; StorageClient1 storageClient1 = null;
StorageServer storageServer = null; StorageServer storageServer = null;
try { try {
trackerClient = new TrackerClient(); trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection(); trackerServer = trackerClient.getConnection();
storageClient1 = new StorageClient1(trackerServer, storageServer); storageClient1 = new StorageClient1(trackerServer, storageServer);
// 根据文件标识下载文件 // 根据文件标识下载文件
by = storageClient1.download_file1(fileUrl); by = storageClient1.download_file1(fileUrl);
// 将数据写入输出流 // 将数据写入输出流
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (MyException e) { } catch (MyException e) {
e.printStackTrace(); e.printStackTrace();
} }
return by; return by;
} }
public static String uploadPic(byte[] pic, String fileName, long size) { public static String uploadPic(byte[] pic, String fileName, long size) {
String[] fileIds = null; String[] fileIds = null;
try { try {
// ClientGloble 读配置文件 // ClientGloble 读配置文件
// 老大客户端 // 老大客户端
TrackerClient trackerClient = new TrackerClient(); TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null; StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer); StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String extName = FilenameUtils.getExtension(fileName); String extName = FilenameUtils.getExtension(fileName);
// 设置图片meta信息 // 设置图片meta信息
NameValuePair[] meta_list = new NameValuePair[3]; NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName", fileName); meta_list[0] = new NameValuePair("fileName", fileName);
meta_list[1] = new NameValuePair("fileExt", extName); meta_list[1] = new NameValuePair("fileExt", extName);
meta_list[2] = new NameValuePair("fileSize", String.valueOf(size)); meta_list[2] = new NameValuePair("fileSize", String.valueOf(size));
// 上传且返回path // 上传且返回path
fileIds = storageClient.upload_file(pic, extName, meta_list); fileIds = storageClient.upload_file(pic, extName, meta_list);
return fileIds[0] + "/" + fileIds[1]; return fileIds[0] + "/" + fileIds[1];
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
public static boolean deletePic(String fileUrl) { public static boolean deletePic(String fileUrl) {
try { try {
TrackerClient tracker = new TrackerClient(); TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection(); TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null; StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer); StorageClient storageClient = new StorageClient(trackerServer, storageServer);
StorePath storePath = StorePath.praseFromUrl(fileUrl); StorePath storePath = StorePath.praseFromUrl(fileUrl);
int i = storageClient.delete_file(storePath.getGroup(), storePath.getPath()); int i = storageClient.delete_file(storePath.getGroup(), storePath.getPath());
System.out.println(i == 0 ? "删除成功" : "删除失败:" + i); System.out.println(i == 0 ? "删除成功" : "删除失败:" + i);
return i == 0; return i == 0;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return false; return false;
} }
public static String deletePic(String[] fileIds) { public static String deletePic(String[] fileIds) {
try { try {
TrackerClient tracker = new TrackerClient(); TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection(); TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null; StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer); StorageClient storageClient = new StorageClient(trackerServer, storageServer);
int i = storageClient.delete_file(fileIds[0], fileIds[1]); int i = storageClient.delete_file(fileIds[0], fileIds[1]);
System.out.println(i == 0 ? "删除成功" : "删除失败:" + i); System.out.println(i == 0 ? "删除成功" : "删除失败:" + i);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return fileIds[1]; return fileIds[1];
} }
private static class UploadFileSender implements UploadCallback { private static class UploadFileSender implements UploadCallback {
private InputStream inStream; private InputStream inStream;
public UploadFileSender(InputStream inStream) { public UploadFileSender(InputStream inStream) {
this.inStream = inStream; this.inStream = inStream;
} }
@Override @Override
public int send(OutputStream out) throws IOException { public int send(OutputStream out) throws IOException {
int readBytes; int readBytes;
while ((readBytes = inStream.read()) > 0) { while ((readBytes = inStream.read()) > 0) {
out.write(readBytes); out.write(readBytes);
} }
return 0; return 0;
} }
} }
public static String conventAndUpload(InputStream inputStream, String filename) { public static String conventAndUpload(InputStream inputStream, String filename) {
File outputFile = new File(filename + ".pdf"); File outputFile = new File(filename + ".pdf");
String filePath = ""; String filePath = "";
try { try {
Workbook document = new Workbook(inputStream); Workbook document = new Workbook(inputStream);
//pdf路径 //pdf路径
//操作文档保存 //操作文档保存
document.save(outputFile.getAbsolutePath(), SaveFormat.PDF); document.save(outputFile.getAbsolutePath(), SaveFormat.PDF);
FileReader fileReader = new FileReader(outputFile.getAbsolutePath()); FileReader fileReader = new FileReader(outputFile.getAbsolutePath());
filePath = uploadPic(fileReader.readBytes(), outputFile.getName(), outputFile.length()); filePath = uploadPic(fileReader.readBytes(), outputFile.getName(), outputFile.length());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
//删除创建的输出文件 //删除创建的输出文件
FileUtil.del(outputFile); FileUtil.del(outputFile);
return filePath; return filePath;
} }
} }
} }
\ No newline at end of file
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