Commit f3ae73b0 authored by 位宇华's avatar 位宇华

代码提交

parent ce625f1d
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
/**
* @Package ${PACKAGE_NAME}
* @author 九黎
* @date ${DATE} ${TIME}
*/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteTargetsManager">
<targets>
<target name="root@192.168.111.229:22" type="ssh/sftp" uuid="1738cc89-2334-47a5-b176-1f1020e63798">
<config>
<option name="projectRootOnTarget" value="/root/ofd-service" />
<option name="serverName" value="root@192.168.111.229:22 password" />
</config>
<ContributedStateBase type="JavaLanguageRuntime">
<config>
<option name="homePath" value="/usr/local/jdk1.8" />
<option name="javaVersionString" value="1.8.0_201" />
</config>
</ContributedStateBase>
</target>
</targets>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -3,6 +3,7 @@ package com.tianyi.config; ...@@ -3,6 +3,7 @@ package com.tianyi.config;
import com.tianyi.interceptor.SignInterceptor; import com.tianyi.interceptor.SignInterceptor;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -34,6 +35,7 @@ public class WebMvcConfig extends WebMvcConfigurationSupport { ...@@ -34,6 +35,7 @@ public class WebMvcConfig extends WebMvcConfigurationSupport {
excludeUrlList.add("/swagger-resources/**"); excludeUrlList.add("/swagger-resources/**");
excludeUrlList.add("/v2/**"); excludeUrlList.add("/v2/**");
excludeUrlList.add("/favicon.ico"); excludeUrlList.add("/favicon.ico");
excludeUrlList.add("/getfile/**");
//生成应用id和密钥接口不拦截 //生成应用id和密钥接口不拦截
excludeUrlList.add("/secret"); excludeUrlList.add("/secret");
registry.addInterceptor(signInterceptor) registry.addInterceptor(signInterceptor)
...@@ -43,4 +45,21 @@ public class WebMvcConfig extends WebMvcConfigurationSupport { ...@@ -43,4 +45,21 @@ public class WebMvcConfig extends WebMvcConfigurationSupport {
.excludePathPatterns(excludeUrlList); .excludePathPatterns(excludeUrlList);
super.addInterceptors(registry); super.addInterceptors(registry);
} }
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations(
CLASSPATH_RESOURCE_LOCATIONS);
}
}
} }
package com.tianyi.controller;
import com.tianyi.service.FileGetResourceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
@RestController
public class FileGetResourceController extends HttpServlet {
private final FileGetResourceService fileGetResourceService;
public FileGetResourceController(FileGetResourceService fileGetResourceService) {
this.fileGetResourceService = fileGetResourceService;
}
/**
* 下载文件
*
* @param fileName
* @param response
*/
@GetMapping("/download")
public void getFile(String fileName, HttpServletResponse response) {
fileGetResourceService.getFile(fileName, response);
}
}
...@@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.PostMapping; ...@@ -7,6 +7,8 @@ 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.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController @RestController
public class PdfToOfdController { public class PdfToOfdController {
...@@ -23,7 +25,7 @@ public class PdfToOfdController { ...@@ -23,7 +25,7 @@ public class PdfToOfdController {
* @return * @return
*/ */
@PostMapping("/pdf/convert") @PostMapping("/pdf/convert")
public Object pdfToOfd(@RequestBody RequestModel requestModel) { public Object pdfToOfd(@RequestBody RequestModel requestModel, HttpServletRequest request) {
return pdfToOfdService.convert(requestModel); return pdfToOfdService.convert(requestModel,request);
} }
} }
...@@ -24,18 +24,21 @@ public class SignInterceptor implements HandlerInterceptor { ...@@ -24,18 +24,21 @@ public class SignInterceptor implements HandlerInterceptor {
@SneakyThrows @SneakyThrows
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String appId ;
String timestampStr;
String signature ;
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); response.setContentType("text/html;charset=UTF-8");
//分配的应用id //分配的应用id
String appId = request.getHeader("appId"); appId = request.getHeader("appId");
//时间戳 //时间戳
String timestampStr = request.getHeader("timestamp"); timestampStr = request.getHeader("timestamp");
//签名 //签名
String signature = request.getHeader("signature"); signature = request.getHeader("signature");
if (StringUtils.isBlank(appId) || StringUtils.isBlank(timestampStr) || StringUtils.isBlank(signature)) { if (StringUtils.isBlank(appId) || StringUtils.isBlank(timestampStr) || StringUtils.isBlank(signature)) {
response.setStatus(500); response.setStatus(500);
response.getWriter().println("参数误!"); response.getWriter().println("参数误!");
return false; return false;
} }
//这个密钥实际应该根据appId到数据库里查出来 //这个密钥实际应该根据appId到数据库里查出来
...@@ -67,5 +70,4 @@ public class SignInterceptor implements HandlerInterceptor { ...@@ -67,5 +70,4 @@ public class SignInterceptor implements HandlerInterceptor {
return true; return true;
} }
} }
...@@ -10,4 +10,6 @@ public interface SecretMapper { ...@@ -10,4 +10,6 @@ public interface SecretMapper {
int getCount(@Param("org") String org); int getCount(@Param("org") String org);
String getAppSecret(@Param("appId") String appId); String getAppSecret(@Param("appId") String appId);
SecretModel getAllByOrg(String org);
} }
package com.tianyi.service;
import javax.servlet.http.HttpServletResponse;
public interface FileGetResourceService {
void getFile(String fileName, HttpServletResponse response);
}
...@@ -2,8 +2,10 @@ package com.tianyi.service; ...@@ -2,8 +2,10 @@ package com.tianyi.service;
import com.tianyi.model.RequestModel; import com.tianyi.model.RequestModel;
import javax.servlet.http.HttpServletRequest;
public interface PdfToOfdService { public interface PdfToOfdService {
Object convert(RequestModel requestModel); Object convert(RequestModel requestModel, HttpServletRequest request);
} }
package com.tianyi.service.impl;
import com.tianyi.service.FileGetResourceService;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@Service
public class FileGetResourceServiceImpl implements FileGetResourceService {
@Override
public void getFile(String fileName, HttpServletResponse response) {
String filePath = "/home/ofd/ofddir/" + fileName;
// 设置响应头,通知浏览器以附件形式下载
response.setHeader("Content-Disposition", "attachment; filename=\"" + getFileName(filePath) + "\"");
response.setContentType("application/octet-stream");
try (FileInputStream fis = new FileInputStream(filePath); ServletOutputStream sos = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
sos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String getFileName(String filePath) {
// 从路径中提取文件名
return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
}
}
package com.tianyi.service.impl; package com.tianyi.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.tianyi.mapper.SecretMapper;
import com.tianyi.model.RequestModel; import com.tianyi.model.RequestModel;
import com.tianyi.model.SecretModel;
import com.tianyi.service.PdfToOfdService; import com.tianyi.service.PdfToOfdService;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -13,7 +16,9 @@ import org.ofdrw.graphics2d.OFDPageGraphics2D; ...@@ -13,7 +16,9 @@ import org.ofdrw.graphics2d.OFDPageGraphics2D;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.*; import java.net.*;
...@@ -26,10 +31,22 @@ public class PdfToOfdServiceImpl implements PdfToOfdService { ...@@ -26,10 +31,22 @@ public class PdfToOfdServiceImpl implements PdfToOfdService {
private static final Logger log = LoggerFactory.getLogger(PdfToOfdServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(PdfToOfdServiceImpl.class);
private final SecretMapper secretMapper;
public PdfToOfdServiceImpl(SecretMapper secretMapper) {
this.secretMapper = secretMapper;
}
@Override @Override
@SneakyThrows @SneakyThrows
public Object convert(RequestModel requestModel) { public Object convert(RequestModel requestModel, HttpServletRequest request) {
JSONObject jsonObject = new JSONObject();
SecretModel secretModel = secretMapper.getAllByOrg(request.getHeader("org"));
if (ObjectUtils.isEmpty(secretModel)) {
jsonObject.put("code", "500");
jsonObject.put("msg", "无权限访问!");
return jsonObject;
}
URL url = new URL(requestModel.getUrlStr()); URL url = new URL(requestModel.getUrlStr());
URLConnection urlConnection = url.openConnection(); URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream(); InputStream inputStream = urlConnection.getInputStream();
...@@ -46,10 +63,12 @@ public class PdfToOfdServiceImpl implements PdfToOfdService { ...@@ -46,10 +63,12 @@ public class PdfToOfdServiceImpl implements PdfToOfdService {
String os = System.getProperty("os.name").toLowerCase(); String os = System.getProperty("os.name").toLowerCase();
log.error("系统类别:{}", os); log.error("系统类别:{}", os);
if (StringUtils.contains(os, "linux")) { if (StringUtils.contains(os, "linux")) {
out = Paths.get("/home/ofd/ofddir/" + System.currentTimeMillis() + ".ofd"); long time = System.currentTimeMillis();
out = Paths.get("/home/ofd/ofddir/" + time + ".ofd");
Files.write(out, outputStream.toByteArray()); Files.write(out, outputStream.toByteArray());
Path absolutePath = out.toAbsolutePath(); jsonObject.put("code", "200");
return convertToHttpUrl(absolutePath.toString(),"http",8848,"/ofd-service"); jsonObject.put("msg", "转换成功!");
return jsonObject;
} }
if (StringUtils.contains(os, "windows")) { if (StringUtils.contains(os, "windows")) {
out = Paths.get("target", System.currentTimeMillis() + ".ofd"); out = Paths.get("target", System.currentTimeMillis() + ".ofd");
...@@ -60,36 +79,5 @@ public class PdfToOfdServiceImpl implements PdfToOfdService { ...@@ -60,36 +79,5 @@ public class PdfToOfdServiceImpl implements PdfToOfdService {
return null; return null;
} }
@SneakyThrows
public static String convertToHttpUrl(String absolutePath, String scheme, int serverPort, String contextPath) {
// 将绝对路径转换为URI对象
URI uri = URI.create(absolutePath);
// 构建HTTP URL
StringBuilder httpUrl = new StringBuilder();
httpUrl.append(scheme).append("://")
.append("192.168.111.229").append(":")
.append(serverPort).append(contextPath);
// 如果URI中包含路径,则添加到HTTP URL中
String path = uri.getPath();
if (path != null && !path.isEmpty()) {
httpUrl.append(path.startsWith("/") ? path : "/".concat(path));
}
// 如果URI中包含查询参数,则添加到HTTP URL中
String query = uri.getQuery();
if (query != null) {
httpUrl.append("?").append(query);
}
// 如果URI中包含片段标识符,则添加到HTTP URL中
String fragment = uri.getFragment();
if (fragment != null) {
httpUrl.append("#").append(fragment);
}
return httpUrl.toString();
}
} }
...@@ -35,4 +35,5 @@ public class Signature { ...@@ -35,4 +35,5 @@ public class Signature {
String calculatedHmac = signWithHmacSha1(secretKey, data); String calculatedHmac = signWithHmacSha1(secretKey, data);
return calculatedHmac.equals(hmac); return calculatedHmac.equals(hmac);
} }
} }
...@@ -15,4 +15,11 @@ ...@@ -15,4 +15,11 @@
select app_secret select app_secret
from ofd_api from ofd_api
</select> </select>
<select id="getAllByOrg" resultType="com.tianyi.model.SecretModel">
select app_id as appId,
app_secret as appSecret,
org as org
from ofd_api
where org = #{org}
</select>
</mapper> </mapper>
\ 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