提交 3fdce195 作者: passer

文件服务器下载共享文件接口

父级
<?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.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.file</groupId>
<artifactId>fileCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fileCenter</name>
<description>file controller</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.file.center;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FileCenterApplication {
public static void main(String[] args) {
SpringApplication.run(FileCenterApplication.class, args);
}
}
package com.file.center.controller;
import com.file.center.util.CopyImageDTO;
import com.file.center.util.ImageUtils;
import com.file.center.util.OCRAPI;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 说明:
*
* @author Li
*/
@RequestMapping("/OCRTool")
@RestController
public class OCRToolController {
@PostMapping("/copyImage")
public String copyImage(@RequestBody CopyImageDTO dto) throws Exception {
return ImageUtils.downloadByURL(dto.getUrlString(), dto.getFileName(), OCRAPI.getImagePre(),dto.getLinux());
}
}
package com.file.center.util;
/**
* @author:Li
* @create: 2022-12-23 16:02
* @Description:
*/
public class CopyImageDTO {
private String urlString;
private String fileName;
private Boolean isLinux;
private String savePath = OCRAPI.getImagePre();
public String getUrlString() {
return urlString;
}
public void setUrlString(String urlString) {
this.urlString = urlString;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Boolean getLinux() {
return isLinux;
}
public void setLinux(Boolean linux) {
isLinux = linux;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
package com.file.center.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* 图片处理工具类
*
* @author Li
*/
public class ImageUtils {
/**
* 根据图片的URL从网页下载图片
*
* @param urlString 图片URL地址
* @param filename 下载后的图片名称
* @param savePath 保存路径
* @throws Exception
*/
public static String downloadByURL(String urlString, String filename, String savePath, Boolean isLinux) throws Exception {
String FileSeparator = "\\";
if (isLinux) {
FileSeparator = "/";
}
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 设置请求超时为5s
con.setConnectTimeout(5 * 1000);
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
OutputStream os = new FileOutputStream(sf.getPath() + FileSeparator + "part" + FileSeparator + filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
return savePath + FileSeparator + "part" + FileSeparator + filename;
}
}
package com.file.center.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 说明:
*
* @author Li
*/
@Component
public class OCRAPI {
private static String imagePre;
public static String getImagePre() {
return imagePre;
}
@Value("${ruoyi.profile}")
public void setImagePre(String imagePre) {
OCRAPI.imagePre = imagePre;
}
}
# ??????
ruoyi:
# ??
name: ocr_son_server
# ??
version: 3.3.0
# ????
copyrightYear: 2021
# ??????
demoEnabled: true
# ???? ??? Windows??D:/ruoyi/uploadPath?Linux?? /home/ruoyi/uploadPath?
profile: /mydata/server/ocrfile
# ??????
server:
# ????HTTP??????8080
port: 8188
servlet:
# ???????
context-path: /api
tomcat:
# tomcat?URI??
uri-encoding: UTF-8
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论