7 changed files with 115 additions and 3 deletions
@ -0,0 +1,24 @@ |
|||||
|
package com.userinformation.backend.config; |
||||
|
|
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 虚拟目录配置 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2021-12-03 09:33 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class WebMvcConfig extends WebMvcConfigurerAdapter { |
||||
|
|
||||
|
@Value("${custom.image.path}") |
||||
|
private String path; |
||||
|
|
||||
|
@Override |
||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) { |
||||
|
registry.addResourceHandler("/product/**").addResourceLocations("file:" + path + "/"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package com.userinformation.backend.controller; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import com.userinformation.backend.service.ImageService; |
||||
|
import com.userinformation.backend.util.RequestResult; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 图片处理控制层 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2021-12-03 13:10 |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("image") |
||||
|
public class ImageController { |
||||
|
|
||||
|
private final ImageService imageService; |
||||
|
public ImageController(ImageService imageService){ |
||||
|
this.imageService = imageService; |
||||
|
} |
||||
|
|
||||
|
@PostMapping("convert") |
||||
|
public RequestResult convert(String url) throws IOException { |
||||
|
return RequestResult.success(imageService.convert(url)); |
||||
|
} |
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
package com.userinformation.backend.service; |
||||
|
|
||||
|
import javax.imageio.ImageIO; |
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.io.File; |
||||
|
import java.io.IOException; |
||||
|
import java.util.Locale; |
||||
|
|
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.util.Assert; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 图片处理 逻辑层 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2021-12-03 13:12 |
||||
|
*/ |
||||
|
@Service |
||||
|
public class ImageService { |
||||
|
|
||||
|
@Value("${custom.image.path}") |
||||
|
private String imagePath; |
||||
|
|
||||
|
@Value("${custom.image.url}") |
||||
|
private String imageUrl; |
||||
|
|
||||
|
private final static String TIF_FILE_SUFFIX = ".tif"; |
||||
|
private final static String PNG_FILE_SUFFIX = ".png"; |
||||
|
|
||||
|
public String convert(String url) throws IOException { |
||||
|
String filepath = url.replace(imageUrl, imagePath); |
||||
|
File sourceFile = new File(filepath); |
||||
|
Assert.isTrue(sourceFile.exists(), "文件不存在"); |
||||
|
Assert.isTrue(filepath.toLowerCase(Locale.ROOT).endsWith(TIF_FILE_SUFFIX), "文件格式不正确"); |
||||
|
String targetFilePath = filepath.replace(TIF_FILE_SUFFIX, PNG_FILE_SUFFIX); |
||||
|
File targetFile = new File(targetFilePath); |
||||
|
if (!targetFile.exists()){ |
||||
|
BufferedImage image = ImageIO.read(sourceFile); |
||||
|
ImageIO.write(image, "png", targetFile); |
||||
|
} |
||||
|
return targetFilePath.replace(imagePath, imageUrl); |
||||
|
} |
||||
|
} |
@ -1,8 +1,12 @@ |
|||||
server: |
server: |
||||
port: 8001 |
port: 8002 |
||||
spring: |
spring: |
||||
datasource: |
datasource: |
||||
url: jdbc:mysql://112.124.40.88:33306/user_information?useUnicode=true&characteEncoding=utf-8 |
url: jdbc:mysql://112.124.40.88:33306/user_information?useUnicode=true&characteEncoding=utf-8 |
||||
username: root |
username: root |
||||
password: 3cqscbr@only1 |
password: 3cqscbr@only1 |
||||
driver-class-name: com.mysql.cj.jdbc.Driver |
driver-class-name: com.mysql.cj.jdbc.Driver |
||||
|
custom: |
||||
|
image: |
||||
|
url: http://112.124.40.88:8002/product |
||||
|
path: D:/Deployments/LamanRadar/product |
||||
|
Loading…
Reference in new issue