init
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
@@ -0,0 +1,86 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.11</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.common</groupId>
|
||||
<artifactId>backend2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>backend2</name>
|
||||
<description>backend2</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.common.backend2;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Backend2Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Backend2Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.common.backend2.config;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
public class CommonInterceptor implements HandlerInterceptor {
|
||||
private List<String> excludedUrls;
|
||||
|
||||
public List<String> getExcludedUrls() {
|
||||
return excludedUrls;
|
||||
}
|
||||
|
||||
public void setExcludedUrls(List<String> excludedUrls) {
|
||||
this.excludedUrls = excludedUrls;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 在业务处理器处理请求之前被调用 如果返回false
|
||||
* 从当前的拦截器往回执行所有拦截器的afterCompletion(),
|
||||
* 再退出拦截器链, 如果返回true 执行下一个拦截器,
|
||||
* 直到所有的拦截器都执行完毕 再执行被拦截的Controller
|
||||
* 然后进入拦截器链,
|
||||
* 从最后一个拦截器往回执行所有的postHandle()
|
||||
* 接着再从最后一个拦截器往回执行所有的afterCompletion()
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler) throws Exception {
|
||||
response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
response.setHeader("Access-Control-Allow-Methods", "*");
|
||||
response.setHeader("Access-Control-Max-Age", "3600");
|
||||
response.setHeader("Access-Control-Allow-Headers",
|
||||
"Origin, X-Requested-With, Content-Type, Accept");
|
||||
return true;
|
||||
}
|
||||
|
||||
// 在业务处理器处理请求执行完成后,生成视图之前执行的动作
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||
ModelAndView modelAndView) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 在DispatcherServlet完全处理完请求后被调用
|
||||
* 当有拦截器抛出异常时,
|
||||
* 会从当前拦截器往回执行所有的拦截器的afterCompletion()
|
||||
* @param request
|
||||
* @param response
|
||||
* @param handler
|
||||
*
|
||||
*/
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception ex) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.common.backend2.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class ResTemplateConfig {
|
||||
@Bean
|
||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
//超时设置
|
||||
factory.setReadTimeout(10000);//ms
|
||||
factory.setConnectTimeout(15000);//ms
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.common.backend2.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedHeaders("*")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
|
||||
.maxAge(3600)
|
||||
.allowCredentials(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
WebMvcConfigurer.super.addInterceptors(registry);
|
||||
registry.addInterceptor(CommonInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
/**
|
||||
* 资源映射路径
|
||||
* addResourceHandler:访问映射路径
|
||||
* addResourceLocations:资源绝对路径
|
||||
*/
|
||||
//String projectRootDirectoryPath = System.getProperty("user.dir");
|
||||
// 通过 File 对象的 getParent() 方法获取到根目录的上级目录
|
||||
//String parentPath = new File(projectRootDirectoryPath).getParent();
|
||||
|
||||
// registry.addResourceHandler("/result/**")
|
||||
// .addResourceLocations("file:///"+parentPath+"/RED-CNN-master(Lite)\\save/fig/");
|
||||
registry.addResourceHandler("/result/**")
|
||||
.addResourceLocations("file:///E:/Document/project/temp/save/");
|
||||
registry.addResourceHandler("/assets/**")
|
||||
.addResourceLocations("file:///E:/Document/project/temp/assets/");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Bean
|
||||
public CommonInterceptor CommonInterceptor() {
|
||||
return new CommonInterceptor();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.common.backend2.controller;
|
||||
|
||||
|
||||
import com.common.backend2.entity.File;
|
||||
import com.common.backend2.response.Result;
|
||||
import com.common.backend2.response.ResponseCode;
|
||||
import com.common.backend2.service.FileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
@CrossOrigin
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class FileController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
|
||||
Queue<String> queue =new LinkedList(); //test_patient
|
||||
|
||||
@RequestMapping(value="/multi/uploadMultiImage",method=RequestMethod.POST)
|
||||
public Result uploadMultiImage(@RequestParam("files") MultipartFile[] files,HttpServletRequest request){
|
||||
//files 就是前端传来的多文件数组
|
||||
if (files.length<=0) {
|
||||
return new Result(ResponseCode.FILE_EMPTY.getCode(), ResponseCode.FILE_EMPTY.getMsg(), null);
|
||||
}
|
||||
for(MultipartFile file :files){
|
||||
fileService.upLoadFiles(file,request);
|
||||
}
|
||||
return new Result(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), "数据上传成功");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/upload",method = RequestMethod.POST)
|
||||
public Result upLoadFile(@RequestParam("file") MultipartFile multipartFile,HttpServletRequest request) {
|
||||
if (multipartFile.isEmpty()) {
|
||||
return new Result(ResponseCode.FILE_EMPTY.getCode(), ResponseCode.FILE_EMPTY.getMsg(), null);
|
||||
}
|
||||
return fileService.upLoadFiles(multipartFile,request);
|
||||
|
||||
}
|
||||
@RequestMapping(value="/predict/{fileId}",method=RequestMethod.GET)
|
||||
public Result predictAndreturnResult (@PathVariable("fileId")Integer id){
|
||||
|
||||
return fileService.processFile(id);
|
||||
}
|
||||
@RequestMapping(value = "/download/{id}",method = RequestMethod.GET)
|
||||
public void downloadFiles(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response){
|
||||
OutputStream outputStream=null;
|
||||
InputStream inputStream=null;
|
||||
BufferedInputStream bufferedInputStream=null;
|
||||
byte[] bytes=new byte[1024];
|
||||
File file = fileService.getFileById(id);
|
||||
String fileName = file.getFileName();
|
||||
// 获取输出流
|
||||
try {
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||
response.setContentType("application/force-download");
|
||||
inputStream=fileService.getFileInputStream(file);
|
||||
bufferedInputStream=new BufferedInputStream(inputStream);
|
||||
outputStream = response.getOutputStream();
|
||||
int i=bufferedInputStream.read(bytes);
|
||||
while (i!=-1){
|
||||
outputStream.write(bytes,0,i);
|
||||
i=bufferedInputStream.read(bytes);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
if (inputStream!=null){
|
||||
inputStream.close();
|
||||
}
|
||||
if (outputStream!=null){
|
||||
outputStream.close();
|
||||
}
|
||||
if (bufferedInputStream!=null){
|
||||
bufferedInputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value="/getResult",method=RequestMethod.GET)
|
||||
public Result getUrlsByIp(HttpServletRequest request,HttpServletResponse response){
|
||||
return fileService.returnUrls(request,response);
|
||||
}
|
||||
@RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
|
||||
public Result deleteOriginAndProcess(@PathVariable("id")Integer id ){
|
||||
return fileService.deleteOne(id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.common.backend2.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotations.TableId;
|
||||
import com.baomidou.mybatisplus.annotations.TableName;
|
||||
import com.baomidou.mybatisplus.enums.IdType;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode
|
||||
@TableName(value = "file")
|
||||
public class File implements Serializable {
|
||||
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
/**
|
||||
* 文件id
|
||||
*/
|
||||
@TableId(value="id",type = IdType.AUTO)
|
||||
private Integer fileId;
|
||||
/**
|
||||
* 文件存储路径
|
||||
*/
|
||||
private String filePath;
|
||||
/**
|
||||
* 文件存储路径
|
||||
*/
|
||||
private String savePath;
|
||||
/**
|
||||
* 文件名称
|
||||
*/
|
||||
private String fileName;
|
||||
/**
|
||||
*
|
||||
* 是否处理过
|
||||
*
|
||||
*/
|
||||
private Integer processed;
|
||||
|
||||
private String result;
|
||||
|
||||
|
||||
public File(Object o, String saveChildPath, String resultChildPath, String fileName) {
|
||||
this.fileId=null;
|
||||
this.filePath= saveChildPath;
|
||||
this.savePath = resultChildPath;
|
||||
this.fileName = fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.common.backend2.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.mapper.BaseMapper;
|
||||
|
||||
import com.common.backend2.entity.File;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface FileMapper extends BaseMapper<File> {
|
||||
@Select("select * from file")
|
||||
List<File>findAll();
|
||||
@Select("SELECT LAST_INSERT_ID();")
|
||||
Integer getId();
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.common.backend2.response;
|
||||
|
||||
|
||||
public enum ResponseCode {
|
||||
// 系统模块
|
||||
SUCCESS(0, "操作成功"),
|
||||
ERROR(1, "操作失败"),
|
||||
SERVER_ERROR(500, "服务器异常"),
|
||||
|
||||
// 通用模块 1xxxx
|
||||
ILLEGAL_ARGUMENT(10000, "参数不合法"),
|
||||
REPETITIVE_OPERATION(10001, "请勿重复操作"),
|
||||
ACCESS_LIMIT(10002, "请求太频繁, 请稍后再试"),
|
||||
MAIL_SEND_SUCCESS(10003, "邮件发送成功"),
|
||||
|
||||
// 用户模块 2xxxx
|
||||
NEED_LOGIN(20001, "登录失效"),
|
||||
USERNAME_OR_PASSWORD_EMPTY(20002, "用户名或密码不能为空"),
|
||||
USERNAME_OR_PASSWORD_WRONG(20003, "用户名或密码错误"),
|
||||
USER_NOT_EXISTS(20004, "用户不存在"),
|
||||
WRONG_PASSWORD(20005, "密码错误"),
|
||||
|
||||
// 文件模块 3xxxx
|
||||
FILE_EMPTY(30001,"文件不能空"),
|
||||
FILE_NAME_EMPTY(30002,"文件名称不能为空"),
|
||||
FILE_MAX_SIZE(30003,"文件大小超出"),
|
||||
;
|
||||
|
||||
ResponseCode(Integer code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String msg;
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.common.backend2.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Result {
|
||||
private int code;
|
||||
private String message;
|
||||
private Object data;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.common.backend2.service;
|
||||
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.service.IService;
|
||||
import com.common.backend2.entity.File;
|
||||
import com.common.backend2.response.Result;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
||||
public interface FileService extends IService<File> {
|
||||
|
||||
/**
|
||||
* 文件上传接口
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
Result upLoadFiles(MultipartFile file, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 根据id获取文件
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
File getFileById(String id);
|
||||
|
||||
/**
|
||||
* 根据id获取数据流
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
InputStream getFileInputStream(File file);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 对文件进行处理
|
||||
* @param
|
||||
* @return: 返回处理完的文件的id
|
||||
*/
|
||||
Result processFile(Integer id);
|
||||
|
||||
|
||||
Result returnUrls(HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
Result deleteOne(Integer id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package com.common.backend2.service.Impl;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
||||
|
||||
import com.common.backend2.entity.File;
|
||||
import com.common.backend2.mapper.FileMapper;
|
||||
import com.common.backend2.response.ResponseCode;
|
||||
import com.common.backend2.response.Result;
|
||||
|
||||
import com.common.backend2.service.FileService;
|
||||
import com.common.backend2.utils.IpUtil;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {
|
||||
|
||||
|
||||
@Value("${file.data-path}")
|
||||
private String dataPath;
|
||||
// @Value("${file.parent-path}")
|
||||
// private String parentPath;
|
||||
@Value("${file.result-path}")
|
||||
private String resultPath;
|
||||
@Value("${url.get-result-url}")
|
||||
private String getResultUrl;
|
||||
@Autowired
|
||||
private FileMapper fileMapper;
|
||||
|
||||
@Autowired
|
||||
private RestTemplateMethods restTemplateMethods;
|
||||
|
||||
@Transactional
|
||||
public Result upLoadFiles(MultipartFile file, HttpServletRequest request) {
|
||||
long MAX_SIZE = 2097152L;
|
||||
|
||||
String fileName = file.getOriginalFilename(); //获取原名
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
return new Result(ResponseCode.FILE_NAME_EMPTY.getCode(), ResponseCode.FILE_NAME_EMPTY.getMsg(), null);
|
||||
} // 判断不为空
|
||||
if (file.getSize() > MAX_SIZE) {
|
||||
return new Result(ResponseCode.FILE_MAX_SIZE.getCode(), ResponseCode.FILE_MAX_SIZE.getMsg(), null);
|
||||
} // 判断大小是否超出限制
|
||||
// String suffixName = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".")) : null; //获取没有后缀的文件名
|
||||
// 获取当前时间戳作为文件名
|
||||
Date date = new Date();
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
||||
String dateString = formatter.format(date);
|
||||
|
||||
String saveChildPath = dataPath ;//+ "/" + dateString; // 使用时间戳字符串存储
|
||||
|
||||
String resultChildPath = resultPath ;//+ "/" + dateString;
|
||||
java.io.File newFile = new java.io.File(saveChildPath, fileName); //新建一个文件进行存储
|
||||
if (!newFile.getParentFile().exists()) {
|
||||
newFile.getParentFile().mkdirs();
|
||||
} //文件创建
|
||||
try {
|
||||
//文件写入--文件的转存
|
||||
file.transferTo(newFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} // 转存到文件中
|
||||
File files = new File(null, saveChildPath, resultChildPath, fileName); // 信息包装成对象准备存入数据库
|
||||
fileMapper.insert(files); //插入到数据库
|
||||
int fildId = fileMapper.getId();
|
||||
|
||||
return new Result(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), fildId);
|
||||
}
|
||||
|
||||
public File getFileById(String id) {
|
||||
return fileMapper.selectById(id);
|
||||
|
||||
}
|
||||
|
||||
public InputStream getFileInputStream(File files) {
|
||||
java.io.File file = new java.io.File(files.getFilePath());
|
||||
try {
|
||||
return new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public Result processFile(Integer id) {
|
||||
//
|
||||
// File file = fileMapper.selectById(id);
|
||||
// String fileName = file.getFileName();
|
||||
// String classAndScore=restTemplateMethods.RestTemplatePost(fileName);
|
||||
// String resultUrl=getResultUrl+"/result/"+fileName;
|
||||
// ArrayList result =new ArrayList<>();
|
||||
// result.add(resultUrl);
|
||||
// result.add(classAndScore);
|
||||
// //从结果文件夹发回--这个可以用一个get来请求或者使用轮询
|
||||
// return new Result(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), result);
|
||||
|
||||
File file = fileMapper.selectById(id);
|
||||
Integer processed = file.getProcessed();
|
||||
String fileName = file.getFileName();
|
||||
ArrayList result =new ArrayList<>();
|
||||
String resultUrl=getResultUrl+"/result/"+fileName;
|
||||
result.add(resultUrl);
|
||||
if(processed!=0){
|
||||
result.add(file.getResult());
|
||||
}
|
||||
else{
|
||||
String classAndScore=restTemplateMethods.RestTemplatePost(fileName);
|
||||
//将结果字符串保存至数据库
|
||||
file.setResult(classAndScore);
|
||||
file.setProcessed(1);
|
||||
fileMapper.updateById(file);
|
||||
|
||||
result.add(classAndScore);
|
||||
|
||||
//从结果文件夹发回--这个可以用一个get来请求或者使用轮询
|
||||
|
||||
}
|
||||
return new Result(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result returnUrls(HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
String ip = IpUtil.getClientIpAddr(request);
|
||||
String path = resultPath + "/" + ip; //python 图片目录
|
||||
String RetPath = getResultUrl + "/result/" + ip + "/";
|
||||
java.io.File file = new java.io.File(path);
|
||||
java.io.File[] files = file.listFiles(); //获取所有的文件名
|
||||
List<String> ResultUrls = new ArrayList<>();
|
||||
for (java.io.File file1 : files) {
|
||||
ResultUrls.add(RetPath + file1.getName());
|
||||
}
|
||||
return new Result(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), ResultUrls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result deleteOne(Integer id) {
|
||||
File file =fileMapper.selectById(id);
|
||||
// 获取路径
|
||||
String originPath = file.getFilePath();
|
||||
String resultPath = file.getSavePath();
|
||||
String fileName = file.getFileName();
|
||||
String originFullPath = originPath+"/"+fileName;
|
||||
String resultFullPath = resultPath+"/"+fileName;
|
||||
|
||||
//删除文件
|
||||
java.io.File originFile = new java.io.File(originFullPath);
|
||||
java.io.File resultFile = new java.io.File(resultFullPath);
|
||||
|
||||
// 检查文件是否存在
|
||||
if (originFile.exists()) {
|
||||
// 尝试删除文件
|
||||
boolean deleted = originFile.delete();
|
||||
|
||||
if (deleted) {
|
||||
System.out.println("Origin文件已成功删除");
|
||||
} else {
|
||||
System.out.println("无法删除Origin文件");
|
||||
return new Result(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), "Origin删除失败");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Origin文件不存在");
|
||||
return new Result(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), "Origin文件不存在");
|
||||
}
|
||||
|
||||
if (resultFile.exists()) {
|
||||
// 尝试删除文件
|
||||
boolean deleted = resultFile.delete();
|
||||
|
||||
if (deleted) {
|
||||
System.out.println("Result文件已成功删除");
|
||||
} else {
|
||||
System.out.println("无法删除Result文件");
|
||||
return new Result(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), "Result删除失败");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Result文件不存在");
|
||||
}
|
||||
//删除数据库数据
|
||||
int result = fileMapper.deleteById(file);
|
||||
|
||||
if (result > 0) {
|
||||
// 删除成功
|
||||
return new Result(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), "删除成功");
|
||||
} else {
|
||||
// 删除失败
|
||||
return new Result(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), "数据库删除失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.common.backend2.service.Impl;
|
||||
|
||||
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
@Service
|
||||
public class RestTemplateMethods {
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Value("${url.predict-url}")
|
||||
String predictUrl ;
|
||||
|
||||
public String RestTemplatePost(String fileName) {
|
||||
|
||||
|
||||
|
||||
LinkedMultiValueMap<String, String> request = new LinkedMultiValueMap<>();
|
||||
//request.set("className",dateString);
|
||||
request.set("fileName", fileName);
|
||||
try {
|
||||
URL url1 = new URL(predictUrl);
|
||||
HttpURLConnection con =(HttpURLConnection)url1.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setRequestProperty("Content-Type","application/from-data");
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//请求
|
||||
String result = restTemplate.postForObject(predictUrl, request, String.class);
|
||||
|
||||
System.out.println(result);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.common.backend2.utils;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
|
||||
public class IpUtil {
|
||||
/**
|
||||
* @description: 获取请求的ip
|
||||
* @param request
|
||||
* @return: java.lang.String
|
||||
*/
|
||||
public static String getClientIpAddr(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
|
||||
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
|
||||
int index = ip.indexOf(',');
|
||||
if (index != -1) {
|
||||
ip = ip.substring(0, index);
|
||||
}
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_X_FORWARDED");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_CLIENT_IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_FORWARDED_FOR");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_FORWARDED");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_VIA");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("REMOTE_ADDR");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("X-Real-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
|
||||
//根据网卡取本机配置的IP
|
||||
InetAddress inet = null;
|
||||
try {
|
||||
inet = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ip = inet.getHostAddress();
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
server:
|
||||
port: 5000
|
||||
#port: 80
|
||||
servlet:
|
||||
encoding:
|
||||
charset: utf-8
|
||||
enabled: true
|
||||
force: true
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
name: defaultDataSource
|
||||
# url: jdbc:mysql://175.178.186.100:31441/DiatomRecognition?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
|
||||
url: jdbc:mysql://192.168.161.130:3306/DiatomRecognition?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
|
||||
username: root
|
||||
# password: xiyi409@
|
||||
password: 123456
|
||||
|
||||
mybatis-plus:
|
||||
typeAliasesPackage: com.common.backend2.entity
|
||||
mapperLocations: classpath:mapper/*.xml
|
||||
file: # 服务器
|
||||
data-path: /home/hejinwen/detect/data/upload
|
||||
result-path: /home/hejinwen/detect/data/save
|
||||
url: # 服务器
|
||||
predict-url: http://127.0.0.1:2000/predict
|
||||
get-result-url: http://192.168.161.130:80 # 走nginx的代理
|
||||
#url: # 服务器测试
|
||||
# predict-url: http://175.178.186.100:5000/predict
|
||||
# get-result-url: wadouri:http://175.178.186.100:6000
|
||||
#file: # 本地测试
|
||||
# parent-path: E:\Document\project\temp
|
||||
# data-path: E:/Document/project/temp/data
|
||||
# result-path: E:\Document\project\temp/save
|
||||
#url: # 本地测试
|
||||
# predict-url: http://127.0.0.1:2000/predict
|
||||
# get-result-url: http://127.0.0.1:5000
|
||||
logging:
|
||||
level:
|
||||
root: INFO
|
||||
web: DEBUG
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.common.backend2;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class Backend2ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user