添加了上传图片到服务器的接口和查看图片信息的接口
This commit is contained in:
parent
a2cf66488e
commit
ac9a7c8840
17
pom.xml
17
pom.xml
@ -33,6 +33,8 @@
|
||||
<poi.version>5.0.0</poi.version>
|
||||
<poi-ooxml.version>5.0.0</poi-ooxml.version>
|
||||
<dysmsapi20170525.version>2.0.4</dysmsapi20170525.version>
|
||||
<hutool-all.version>4.4.3</hutool-all.version>
|
||||
<tus-client.version>0.4.0</tus-client.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -214,6 +216,21 @@
|
||||
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool-all.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.tus.java.client</groupId>
|
||||
<artifactId>tus-java-client</artifactId>
|
||||
<version>${tus-client.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
<version>2.5.10.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
@ -55,6 +55,8 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.antMatchers(HttpMethod.GET,"/selectFirePoint").permitAll()
|
||||
.antMatchers(HttpMethod.POST,"/updateTypeByFireCode").permitAll()
|
||||
.antMatchers(HttpMethod.GET,"/api/user/verificationCode").permitAll()
|
||||
.antMatchers(HttpMethod.POST,"/uploadFileMore").permitAll()
|
||||
.antMatchers(HttpMethod.POST,"/uploadFile").permitAll()
|
||||
// 所有其它请求需要身份认证
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
116
src/main/java/com/xkrs/controller/FileController.java
Normal file
116
src/main/java/com/xkrs/controller/FileController.java
Normal file
@ -0,0 +1,116 @@
|
||||
package com.xkrs.controller;
|
||||
|
||||
import com.xkrs.common.encapsulation.PromptMessageEnum;
|
||||
import com.xkrs.dao.FilePathDao;
|
||||
import com.xkrs.model.entity.FilePath;
|
||||
import com.xkrs.utils.FileFastDfs;
|
||||
import com.xkrs.utils.FileUtil;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
||||
import static com.xkrs.utils.FileUtil.getUploadInfo;
|
||||
|
||||
/**
|
||||
* @author XinYi Song
|
||||
* 文件上传
|
||||
*/
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
@Resource
|
||||
private FileFastDfs fileFastDfs;
|
||||
|
||||
@Resource
|
||||
private FilePathDao filePathDao;
|
||||
|
||||
/**
|
||||
* 上传多张图片
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/uploadFileMore")
|
||||
public String uploadFileMore(MultipartFile[] files){
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
if (null == files || files.length == 0){
|
||||
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "result or fileType is null", locale);
|
||||
}
|
||||
List list = new ArrayList();
|
||||
for (MultipartFile file :
|
||||
files) {
|
||||
boolean m = FileUtil.checkFileSize(file.getSize(), 100, "M");
|
||||
if(m == false){
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "图片大小不能超过100M", locale);
|
||||
}
|
||||
String dir = "fire_point";
|
||||
String info = fileFastDfs.uploadFile(file, dir);
|
||||
Map<String, String> map = getUploadInfo(info);
|
||||
FilePath filePath = new FilePath();
|
||||
filePath.setFilePath(map.get("path"));
|
||||
filePath.setFileName(map.get("fileName"));
|
||||
filePath.setSize(map.get("size"));
|
||||
filePath.setMtime(map.get("mtime"));
|
||||
filePath.setUrl(map.get("url"));
|
||||
filePath.setMd5(map.get("md5"));
|
||||
filePath.setScene(map.get("scene"));
|
||||
filePathDao.save(filePath);
|
||||
}
|
||||
//List<Map<String, String>> path = filePathDao.findPath();
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "上传成功!", locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传单张图片
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/uploadFile")
|
||||
public String uploadFile(MultipartFile file){
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
if (null == file){
|
||||
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "result or fileType is null", locale);
|
||||
}
|
||||
boolean m = FileUtil.checkFileSize(file.getSize(), 100, "M");
|
||||
if(m == false){
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "图片大小不能超过100M", locale);
|
||||
}
|
||||
String dir = "fire_point";
|
||||
String info = fileFastDfs.uploadFile(file, dir);
|
||||
Map<String, String> map = getUploadInfo(info);
|
||||
FilePath filePath = new FilePath();
|
||||
filePath.setFilePath(map.get("path"));
|
||||
filePath.setFileName(map.get("fileName"));
|
||||
filePath.setSize(map.get("size"));
|
||||
filePath.setMtime(map.get("mtime"));
|
||||
filePath.setUrl(map.get("url"));
|
||||
filePath.setMd5(map.get("md5"));
|
||||
filePath.setScene(map.get("scene"));
|
||||
filePathDao.save(filePath);
|
||||
//List<Map<String, String>> path = filePathDao.findPath();
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "上传成功!", locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询火点图片路径
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/findPath")
|
||||
public String findPath(){
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
List<Map<String, String>> path = filePathDao.findPath();
|
||||
if(path == null || path.size() == 0){
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "还未上传任何图片信息!", locale);
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, path, locale);
|
||||
}
|
||||
}
|
21
src/main/java/com/xkrs/dao/FilePathDao.java
Normal file
21
src/main/java/com/xkrs/dao/FilePathDao.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.xkrs.dao;
|
||||
|
||||
import com.xkrs.model.entity.FilePath;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author XinYi Song
|
||||
*/
|
||||
public interface FilePathDao extends JpaRepository<FilePath,Long> {
|
||||
|
||||
/**
|
||||
* 查询全部路径
|
||||
* @return
|
||||
*/
|
||||
@Query(value = "select file_path filepath from file_path",nativeQuery = true)
|
||||
List<Map<String,String>> findPath();
|
||||
}
|
135
src/main/java/com/xkrs/model/entity/FilePath.java
Normal file
135
src/main/java/com/xkrs/model/entity/FilePath.java
Normal file
@ -0,0 +1,135 @@
|
||||
package com.xkrs.model.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* @author XinYi Song
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "file_path")
|
||||
public class FilePath {
|
||||
|
||||
/**
|
||||
* 主键id
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "file_path_seq_gen")
|
||||
@SequenceGenerator(name = "file_path_seq_gen", sequenceName = "file_path_id_seq",allocationSize = 1)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 图片路径
|
||||
*/
|
||||
@Column(length = 500,columnDefinition = "varchar(500)")
|
||||
private String filePath;
|
||||
|
||||
@Column(length = 88,columnDefinition = "varchar(88)")
|
||||
private String fileName;
|
||||
|
||||
@Column(length = 65,columnDefinition = "varchar(65)")
|
||||
private String size;
|
||||
|
||||
@Column(length = 85,columnDefinition = "varchar(85)")
|
||||
private String mtime;
|
||||
|
||||
@Column(length = 88,columnDefinition = "varchar(88)")
|
||||
private String url;
|
||||
|
||||
@Column(length = 85,columnDefinition = "varchar(85)")
|
||||
private String md5;
|
||||
|
||||
@Column(length = 65,columnDefinition = "varchar(65)")
|
||||
private String scene;
|
||||
|
||||
public FilePath() {
|
||||
}
|
||||
|
||||
public FilePath(Integer id, String filePath, String fileName, String size, String mtime, String url, String md5, String scene) {
|
||||
this.id = id;
|
||||
this.filePath = filePath;
|
||||
this.fileName = fileName;
|
||||
this.size = size;
|
||||
this.mtime = mtime;
|
||||
this.url = url;
|
||||
this.md5 = md5;
|
||||
this.scene = scene;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void setFilePath(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public String getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(String size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getMtime() {
|
||||
return mtime;
|
||||
}
|
||||
|
||||
public void setMtime(String mtime) {
|
||||
this.mtime = mtime;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getMd5() {
|
||||
return md5;
|
||||
}
|
||||
|
||||
public void setMd5(String md5) {
|
||||
this.md5 = md5;
|
||||
}
|
||||
|
||||
public String getScene() {
|
||||
return scene;
|
||||
}
|
||||
|
||||
public void setScene(String scene) {
|
||||
this.scene = scene;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FilePath{" +
|
||||
"id=" + id +
|
||||
", filePath='" + filePath + '\'' +
|
||||
", fileName='" + fileName + '\'' +
|
||||
", size='" + size + '\'' +
|
||||
", mtime='" + mtime + '\'' +
|
||||
", url='" + url + '\'' +
|
||||
", md5='" + md5 + '\'' +
|
||||
", scene='" + scene + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -117,8 +117,6 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
|
||||
relRoleAuthorityDao.save(relRoleAuthorityEntity);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"注册成功!",locale);
|
||||
|
||||
//sysRoleDao.insertRelUserRole(sysUserEntity.getUserName(),"role_general_user");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -167,6 +167,14 @@ public class DateTimeUtil {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间 yyyy-MM-ss hh:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static String getNowTimeStr(){
|
||||
return COMMON_FORMATTER_DATETIME.format(getNowTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断日期格式是否合法 "yyyy-MM-dd"
|
||||
* @param strDate
|
||||
|
477
src/main/java/com/xkrs/utils/FileFastDfs.java
Normal file
477
src/main/java/com/xkrs/utils/FileFastDfs.java
Normal file
@ -0,0 +1,477 @@
|
||||
package com.xkrs.utils;
|
||||
|
||||
import cn.hutool.core.io.resource.InputStreamResource;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.tus.java.client.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author dong
|
||||
* @date 2020/12/31
|
||||
*/
|
||||
@Component
|
||||
public class FileFastDfs {
|
||||
|
||||
@Value("${dfs.ip}")
|
||||
public String ip;
|
||||
@Value("${dfs.port}")
|
||||
public String port;
|
||||
|
||||
// @Value("${dfs.upload}")
|
||||
// public String upload;
|
||||
//
|
||||
// @Value("${dfs.delete}")
|
||||
// public String delete;
|
||||
|
||||
@Resource
|
||||
private KafkaTemplate<String, Object> kafkaTemplate;
|
||||
private static final Logger log = LoggerFactory.getLogger(FileFastDfs.class);
|
||||
private static final String STATUS = "status";
|
||||
private static final String UPLOAD_BIG_PATH = "http://192.168.2.166:2001/group1/big/upload";
|
||||
private static final String UPLOAD_PATH = "http://192.168.2.166:2001/group1/upload";
|
||||
|
||||
/**
|
||||
* 文件上传到dfs服务器
|
||||
* @param file
|
||||
* @param dir
|
||||
* @return
|
||||
*/
|
||||
public String uploadFile(MultipartFile file, String dir) {
|
||||
File file1 = null;
|
||||
InputStreamResource isr = null;
|
||||
try {
|
||||
file1 = multipartFileToFile(file);
|
||||
isr=new InputStreamResource(file.getInputStream(),
|
||||
file.getOriginalFilename());
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//文件地址
|
||||
//声明参数集合
|
||||
HashMap<String, Object> paramMap = new HashMap<>(7);
|
||||
//文件
|
||||
paramMap.put("file", isr);
|
||||
//输出
|
||||
paramMap.put("output", "json");
|
||||
//自定义路径
|
||||
paramMap.put("path", dir);
|
||||
//场景 文件分类
|
||||
if (null == file1) {
|
||||
return null;
|
||||
}
|
||||
String name = file1.getName();
|
||||
System.err.println("file:" + file1.getName());
|
||||
paramMap.put("scene", name.substring(name.lastIndexOf(".") + 1));
|
||||
paramMap.put("fileName", System.currentTimeMillis() + ""
|
||||
+ Objects.requireNonNull(file.getOriginalFilename())
|
||||
.substring(file.getOriginalFilename().lastIndexOf(".")));
|
||||
System.err.println(paramMap);
|
||||
//
|
||||
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/upload", paramMap);
|
||||
System.err.println(result);
|
||||
|
||||
// 拿出状态码,判断上传状态
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode node = null;
|
||||
try {
|
||||
node = mapper.readTree(result);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final String fail = "fail";
|
||||
if (null != node) {
|
||||
if (fail.equals(node.path(STATUS).asText())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 文件上传到dfs服务器
|
||||
* @params [file:File 文件, dir 文件存放路径]
|
||||
* @author: wd
|
||||
* @time: 2020/3/31 2020/3/31
|
||||
*/
|
||||
public String uploadFile(File file, String dir) {
|
||||
//文件地址
|
||||
//声明参数集合
|
||||
HashMap<String, Object> paramMap = new HashMap<>(7);
|
||||
//文件
|
||||
paramMap.put("file", file);
|
||||
//输出
|
||||
paramMap.put("output", "json");
|
||||
//自定义路径
|
||||
paramMap.put("path", dir);
|
||||
//场景
|
||||
// System.err.println(file.getName());
|
||||
|
||||
// paramMap.put("fileName", System.currentTimeMillis() + "" + file.getName().substring(file.getName().lastIndexOf(".")));
|
||||
paramMap.put("fileName", file.getName());
|
||||
System.err.println(paramMap);
|
||||
//上传
|
||||
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/upload", paramMap);
|
||||
System.err.println(result);
|
||||
// 拿出状态码,判断上传状态
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode node = null;
|
||||
try {
|
||||
node = mapper.readTree(result);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final String status = "status";
|
||||
final String statusFail = "fail";
|
||||
if (null != node) {
|
||||
if (statusFail.equals(node.path(status).asText())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 文件上传到dfs服务器
|
||||
* @params [file:File 文件, dir 文件存放路径]
|
||||
* @author: wd
|
||||
* @time: 2020/3/31 2020/3/31
|
||||
*/
|
||||
public String uploadFile(File file, String dir, String fileType) {
|
||||
//文件地址
|
||||
//声明参数集合
|
||||
HashMap<String, Object> paramMap = new HashMap<>(7);
|
||||
//文件
|
||||
paramMap.put("file", file);
|
||||
//输出
|
||||
paramMap.put("output", "json");
|
||||
//自定义路径
|
||||
paramMap.put("path", dir);
|
||||
//场景
|
||||
System.err.println(file.getName());
|
||||
|
||||
// paramMap.put("fileName", System.currentTimeMillis() + "" + file.getName().substring(file.getName().lastIndexOf(".")));
|
||||
paramMap.put("fileName", file.getName());
|
||||
paramMap.put("scene", fileType);
|
||||
System.err.println(paramMap);
|
||||
//上传
|
||||
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/upload", paramMap);
|
||||
System.err.println(result);
|
||||
// 拿出状态码,判断上传状态
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode node = null;
|
||||
try {
|
||||
node = mapper.readTree(result);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final String status = "status";
|
||||
final String statusFail = "fail";
|
||||
if (null != node) {
|
||||
if (statusFail.equals(node.path(status).asText())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String uploadBigFile(File file, String dir, String fileType) {
|
||||
// 下面这个一定要注意,如果不设置为true,将会直接返回301
|
||||
System.setProperty("http.strictPostRedirect", Boolean.toString(true));
|
||||
TusClient tusClient = new TusClient();
|
||||
try {
|
||||
tusClient.setUploadCreationURL(new URL(UPLOAD_BIG_PATH));
|
||||
|
||||
// tusClient.enableResuming(new TusRedisUrlStrore());
|
||||
tusClient.enableResuming(new TusURLMemoryStore());
|
||||
|
||||
final TusUpload upload = new TusUpload(file);
|
||||
System.out.println("start upload......");
|
||||
TusExecutor executor = new TusExecutor() {
|
||||
@Override
|
||||
protected void makeAttempt() throws ProtocolException, IOException {
|
||||
TusUploader uploader = tusClient.resumeOrCreateUpload(upload);
|
||||
uploader.setChunkSize(1024 * 1024);
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
do {
|
||||
long totalBytes = upload.getSize();
|
||||
long bytesUploaded = uploader.getOffset();
|
||||
double progress = (double) bytesUploaded / totalBytes * 100;
|
||||
|
||||
System.out.printf("Upload at %06.2f%%.\n", progress);
|
||||
} while (uploader.uploadChunk() > -1);
|
||||
|
||||
uploader.finish();
|
||||
|
||||
String uploadUrl = uploader.getUploadURL().toString();
|
||||
System.out.println("Upload finished.");
|
||||
System.out.format("Upload available at: %s\n", uploadUrl);
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
System.out.println((end - start) + "ms");
|
||||
// 使用hutool进行秒传置换url
|
||||
String fileId = StrUtil.subAfter(uploadUrl, UPLOAD_BIG_PATH + "/", true);
|
||||
System.out.println("fileId: " + fileId);
|
||||
String url = StrUtil.format("{}?md5={}&output=json", UPLOAD_PATH, fileId);
|
||||
System.out.println("url: " + url);
|
||||
// 上传大文件的时候(1.xG)需要sleep一下,要不然会有问题
|
||||
ThreadUtil.sleep(5000);
|
||||
String result = HttpUtil.get(url);
|
||||
}
|
||||
};
|
||||
executor.makeAttempts();
|
||||
} catch (IOException | ProtocolException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 文件base64流获取
|
||||
* @params [fileName : 文件路径]
|
||||
* @return: java.lang.String
|
||||
* @author: chqf
|
||||
* @time: 2020/3/31 2020/3/31
|
||||
*/
|
||||
public String getBase64(String fileName) {
|
||||
if (fileName == null || "".equals(fileName)) {
|
||||
return null;
|
||||
}
|
||||
InputStream fis = null;
|
||||
URL url = null;
|
||||
try {
|
||||
url = new URL("http://" + ip + ":" + port + "/" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
|
||||
System.err.println(url);
|
||||
fis = url.openStream();
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}
|
||||
final ByteArrayOutputStream data = new ByteArrayOutputStream();
|
||||
String imgStr = "";
|
||||
try {
|
||||
if (fis == null) {
|
||||
return null;
|
||||
}
|
||||
int len = -1;
|
||||
byte[] buf = new byte[2048];
|
||||
while (-1 != (len = fis.read(buf, 0, buf.length))) {
|
||||
data.write(buf, 0, len);
|
||||
}
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
imgStr = encoder.encodeToString(data.toByteArray());
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}finally {
|
||||
try {
|
||||
if (null != fis) {
|
||||
fis.close();
|
||||
}
|
||||
data.close();
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}
|
||||
}
|
||||
|
||||
return "data:image/jpeg;base64," + imgStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 文件格式转换multipartFil 转为 File 格式
|
||||
* @params [file]
|
||||
* @return: java.io.File
|
||||
* @author: dong
|
||||
* @time: 2020/3/31 2020/3/31
|
||||
*/
|
||||
public File multipartFileToFile(MultipartFile file) throws Exception {
|
||||
|
||||
File toFile = null;
|
||||
if ("".equals(file) || file.getSize() <= 0) {
|
||||
file = null;
|
||||
} else {
|
||||
InputStream ins;
|
||||
ins = file.getInputStream();
|
||||
toFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
|
||||
inputStreamToFile(ins, toFile);
|
||||
ins.close();
|
||||
}
|
||||
return toFile;
|
||||
}
|
||||
|
||||
private static void inputStreamToFile(InputStream ins, File file) {
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = new FileOutputStream(file);
|
||||
int bytesRead = 0;
|
||||
byte[] buffer = new byte[8192];
|
||||
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}finally {
|
||||
try {
|
||||
if (null != os) {
|
||||
os.close();
|
||||
}
|
||||
if (null != ins) {
|
||||
ins.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 文件base64流获取
|
||||
* @params [fileName : 文件路径]
|
||||
* @return: java.lang.String
|
||||
* @author: dong
|
||||
* @time: 2020/3/31 2020/3/31
|
||||
*/
|
||||
public Boolean getFile(String fileName, HttpServletResponse response) {
|
||||
if (fileName == null || "".equals(fileName)) {
|
||||
return null;
|
||||
}
|
||||
InputStream fis = null;
|
||||
URL url = null;
|
||||
try {
|
||||
url = new URL("http://" + ip + ":" + port + "/" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
|
||||
System.err.println(url);
|
||||
fis = url.openStream();
|
||||
response.reset();
|
||||
response.setContentType("bin");
|
||||
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}
|
||||
//
|
||||
byte[] b = new byte[256];
|
||||
int len;
|
||||
try {
|
||||
assert fis != null;
|
||||
while ((len = fis.read(b)) > 0){
|
||||
response.getOutputStream().write(b, 0, len);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}finally {
|
||||
try {
|
||||
if (null != fis) {
|
||||
fis.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.info("IO错误{}", (Object) e.getStackTrace());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件 path
|
||||
* @param filePath 文件路径 上传返回的path
|
||||
* @return
|
||||
*/
|
||||
public Boolean deleteFileByPath(String filePath) {
|
||||
if (filePath == null || "".equals(filePath)) {
|
||||
return false;
|
||||
}
|
||||
HashMap<String, Object> paramMap = new HashMap<>(1);
|
||||
// 参数
|
||||
paramMap.put("path", filePath);
|
||||
|
||||
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/delete", paramMap);
|
||||
System.out.println(result);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode node = null;
|
||||
try {
|
||||
node = mapper.readTree(result);
|
||||
} catch (JsonProcessingException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
final String statusOk = "ok";
|
||||
|
||||
if (null == node) {
|
||||
log.error("返回结果为空");
|
||||
return false;
|
||||
}
|
||||
if (!statusOk.equals(node.path(STATUS).asText())) {
|
||||
log.error("未删除成功");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件 path
|
||||
* @param md5 上传返回的md5
|
||||
* @return
|
||||
*/
|
||||
public Boolean deleteFileByMd5(String md5) {
|
||||
if (md5 == null || "".equals(md5)) {
|
||||
return false;
|
||||
}
|
||||
HashMap<String, Object> paramMap = new HashMap<>(1);
|
||||
// 参数
|
||||
paramMap.put("md5", md5);
|
||||
|
||||
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/delete", paramMap);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode node = null;
|
||||
try {
|
||||
node = mapper.readTree(result);
|
||||
} catch (JsonProcessingException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
final String status = "status";
|
||||
final String statusOk = "ok";
|
||||
|
||||
if (null == node) {
|
||||
log.error("返回结果为空");
|
||||
return false;
|
||||
}
|
||||
if (!statusOk.equals(node.path(status).asText())) {
|
||||
log.error("未删除成功");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
File file = new File("C:\\Users\\dong\\Desktop\\遥感影像\\遥感影像\\GF1\\GF1_PMS1_E116.8_N36.6_20190528_L1A0004026837-MSS1_ORTHO_MS.tif");
|
||||
FileFastDfs fileFastDfs = new FileFastDfs();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,127 +1,251 @@
|
||||
package com.xkrs.utils;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.xkrs.utils.DateTimeUtil.getNowTimeStr;
|
||||
|
||||
/**
|
||||
* @author XinYi Song
|
||||
*/
|
||||
public class FileUtil {
|
||||
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
|
||||
|
||||
/**
|
||||
* 根据base64判断图片是否为tiff
|
||||
* @param base64
|
||||
* 判断目录是否存在, 不存在创建
|
||||
* @param path 路径,目录最后带 File.separator
|
||||
* @return
|
||||
*/
|
||||
public boolean checkImageBase64Format(String base64) {
|
||||
byte[] b=java.util.Base64.getDecoder().decode(base64);
|
||||
try {
|
||||
// 判断是否为tiff格式
|
||||
if((b[0] & 0xFF) == 0x49 && (b[1] & 0xFF)==0x49 && (b[2] & 0xFF)==0x2A){
|
||||
public static boolean isFileExit(String path){
|
||||
path = path.replace("/",File.separator);
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
log.info("文件已存在");
|
||||
return true;
|
||||
}
|
||||
if(path.endsWith(File.separator)) {
|
||||
log.info("创建单个文件" + path + "失败,目标不能是目录!");
|
||||
return false;
|
||||
}
|
||||
if(!file.getParentFile().exists()) {
|
||||
log.info("目标文件所在路径不存在,准备创建。。。");
|
||||
if (file.getParentFile().mkdirs()) {
|
||||
if (file.mkdir()) {
|
||||
log.info("创建目录文件成功!");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
log.info("目标文件所在路径存在");
|
||||
if (file.mkdir()) {
|
||||
log.info("创建目录文件成功!");
|
||||
}
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据BASE64字符串生成图片文件,此方法生成tiff格式
|
||||
* @param base64
|
||||
* @param fileName
|
||||
* @param dictionary
|
||||
* 获取目录下的所有文件
|
||||
* @param filePath 目录
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public void base64ToFile(String base64,String fileName,String dictionary) {
|
||||
File file =null;
|
||||
File dir=new File(dictionary);
|
||||
// 无目录的情况下创建一个目录,会受权限影响,最好是已存在的目录
|
||||
if(!dir.exists() && dir.isDirectory()) {
|
||||
dir.mkdirs();
|
||||
public static List<String> showListFile(String filePath) {
|
||||
File dir = new File(filePath);
|
||||
List<String> list = new ArrayList<>();
|
||||
// 查找参数文件是否存在,只检查第一个入参
|
||||
if(!dir.exists()) {
|
||||
log.error("找不到文件");
|
||||
}
|
||||
FileOutputStream fos=null;
|
||||
BufferedOutputStream bos=null;
|
||||
|
||||
try {
|
||||
byte [] bytes=java.util.Base64.getDecoder().decode(base64);
|
||||
//目录+文件名作为输出文件的全路径
|
||||
file=new File(dictionary+fileName);
|
||||
fos= new FileOutputStream(file);
|
||||
bos=new BufferedOutputStream(fos);
|
||||
bos.write(bytes);
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if(bos!=null) {
|
||||
try {
|
||||
bos.close();
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
// 如果是目录那么进行递归调用
|
||||
if(dir.isDirectory()) {
|
||||
// 获取目录下的所有文件
|
||||
File[] f = dir.listFiles();
|
||||
// 进行递归调用,最后总会返回一个list
|
||||
assert f != null;
|
||||
for (File file : f) {
|
||||
list.addAll(showListFile(file.getPath()));
|
||||
}
|
||||
}else {
|
||||
// 不是目录直接添加进去,判断是否为xml文件
|
||||
list.add(dir.getPath());
|
||||
}
|
||||
if(fos!=null) {
|
||||
try {
|
||||
fos.close();
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将tiff图片转化为jpg,生成新的文件
|
||||
* @param oldPath 原图片的全路径
|
||||
* @param newPath 生成新的图片的全路径
|
||||
*/
|
||||
public void tiffToJpg(String oldPath,String newPath) {
|
||||
try {
|
||||
BufferedImage bufferegImage= ImageIO.read(new File(oldPath));
|
||||
//可以是png等其它图片格式
|
||||
ImageIO.write(bufferegImage,"jpg",new File(newPath));
|
||||
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任意图片文件转为base64,读字节是最快的方式
|
||||
* @param filePath 图片文件的全路径
|
||||
* 移动文件到目标目录
|
||||
* @param path 文件路径
|
||||
* @return
|
||||
*/
|
||||
public String imageToBase64(String filePath) {
|
||||
byte [] data =null;
|
||||
/*public static FileEntity mvFile(String path){
|
||||
String savePath = "D:" + File.separator + "dms" + File.separator + "data" ;
|
||||
|
||||
File file = new File(path);
|
||||
if (!file.isFile()) {
|
||||
log.info("{}不是文件。",path);
|
||||
return null;
|
||||
}
|
||||
// 从文件名获取到入库信息 暂时不知道
|
||||
String[] infos = path.split("/");
|
||||
String fileNameUnHandler = infos[infos.length - 1];
|
||||
String[] fileNameUnHandlerSplitWithPoint = fileNameUnHandler.split(".");
|
||||
String fileName = fileNameUnHandlerSplitWithPoint[0];
|
||||
String scene = fileNameUnHandlerSplitWithPoint[1];
|
||||
String[] fileNames = fileNameUnHandler.split("_");
|
||||
long length = file.length();
|
||||
String filePath = File.separator + scene + File.separator + fileName;
|
||||
// savePath = savePath + File.separator + dir + File.separator + fileName;
|
||||
if (isFileExit(savePath + File.separator + scene)) {
|
||||
if (file.renameTo(new File(filePath))) {
|
||||
log.info("移动文件{}到{}目录",file.getName(), savePath + filePath);
|
||||
} else {
|
||||
log.info("文件{}移动失败",file.getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 读取txt文件内容
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static String txt2String(File file){
|
||||
StringBuilder result = new StringBuilder();
|
||||
try{
|
||||
InputStream in =new FileInputStream(filePath);
|
||||
data=new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
//构造一个BufferedReader类来读取文件
|
||||
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||
String s = null;
|
||||
//使用readLine方法,一次读一行
|
||||
while((s = br.readLine())!=null){
|
||||
result.append(System.lineSeparator()).append(s);
|
||||
}
|
||||
br.close();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 确定上传文件路径遥感数据 栅格
|
||||
* @param file 文件
|
||||
* @return
|
||||
*/
|
||||
public static String fileRsSaveType(File file) {
|
||||
String name = file.getName();
|
||||
String suffix = name.substring(name.indexOf("."));
|
||||
// 判断是否为栅格数据
|
||||
if (!suffix.contains("tif")) {
|
||||
return null;
|
||||
}
|
||||
// GF1_PMS1_E116.8_N36.6_20190528_L1A0004026837-MSS1_ORTHO_MS.tif
|
||||
String[] s = name.split("_");
|
||||
// 卫星种类
|
||||
String satelliteType = s[0];
|
||||
// 传感器种类
|
||||
String sensorType = s[1];
|
||||
// 中心点经度
|
||||
String lat = s[2];
|
||||
// 中心点纬度
|
||||
String lon = s[3];
|
||||
// 时间
|
||||
String time = s[4];
|
||||
String year = time.substring(0, 4);
|
||||
String month = time.substring(4, 6);
|
||||
String day = time.substring(6);
|
||||
// 产品号
|
||||
String product = s[5];
|
||||
String productLevel = product.substring(0, 3);
|
||||
String productCode = product.substring(3);
|
||||
// 种类
|
||||
String type = s[6];
|
||||
// 文件路径
|
||||
String filePath = "/" + satelliteType + "/" + sensorType + "/" + productLevel + "/" + year + "/" + month + "/"
|
||||
+ day + "/" + productCode + "/";
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 矢量数据路径
|
||||
* C:\Users\dong\Desktop\2018年山东省矢量数据\2018年山东省矢量数据\2018年动态\分县动态\山东.gdb\sa.gdb
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static String fileShpSavePath(File file) {
|
||||
if (file.exists()) {
|
||||
if (file.isFile()) {
|
||||
String path = file.getPath();
|
||||
String[] dirNames = path.split("\\\\");
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int length = dirNames.length;
|
||||
final int minLength = 5;
|
||||
if (length >= minLength) {
|
||||
for (int i = 5; i > 1; i--) {
|
||||
sb.append("/").append(dirNames[length - i]);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return new String(Base64.encodeBase64(data));
|
||||
public static Map<String, String> getUploadInfo(String info) {
|
||||
final String statusFail = "fail";
|
||||
final String status = "status";
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode resultNode = null;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
try {
|
||||
resultNode = mapper.readTree(info);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!statusFail.equals(resultNode.path(status).asText())) {
|
||||
String path = resultNode.path("path").asText();
|
||||
String fileName = path.substring(path.lastIndexOf("/") + 1);
|
||||
map.put("md5", resultNode.path("md5").asText());
|
||||
map.put("mtime", getNowTimeStr());
|
||||
map.put("path", path);
|
||||
map.put("scene", resultNode.path("scene").asText());
|
||||
map.put("size", resultNode.path("size").asText());
|
||||
map.put("url", resultNode.path("url").asText());
|
||||
map.put("fileName", fileName);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static boolean checkFileSize(Long len, int size, String unit) {
|
||||
// long len = file.length();
|
||||
double fileSize = 0;
|
||||
if ("B".equals(unit.toUpperCase())) {
|
||||
fileSize = (double) len;
|
||||
} else if ("K".equals(unit.toUpperCase())) {
|
||||
fileSize = (double) len / 1024;
|
||||
} else if ("M".equals(unit.toUpperCase())) {
|
||||
fileSize = (double) len / 1048576;
|
||||
} else if ("G".equals(unit.toUpperCase())) {
|
||||
fileSize = (double) len / 1073741824;
|
||||
}
|
||||
if (fileSize > size) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String oldPath = "C:/Users/xkrs/Desktop/WF/分布/maize";
|
||||
String newPath = "E:/img";
|
||||
try {
|
||||
BufferedImage bufferegImage=ImageIO.read(new File(oldPath));
|
||||
//可以是png等其它图片格式
|
||||
ImageIO.write(bufferegImage,"jpg",new File(newPath));
|
||||
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String s = "C:/Users/dong/Desktop/2018年山东省矢量数据/2018年山东省矢量数据/2018年动态/分县动态/山东.gdb/a0000000a.gdbindexes";
|
||||
fileShpSavePath(new File(s));
|
||||
}
|
||||
}
|
||||
|
@ -56,9 +56,9 @@ spring.messages.encoding = UTF-8
|
||||
# 上传文件配置
|
||||
spring.servlet.multipart.enabled=true
|
||||
# 最大文件大小
|
||||
spring.servlet.multipart.max-file-size = 64MB
|
||||
spring.servlet.multipart.max-file-size = 100MB
|
||||
# 最大请求大小
|
||||
spring.servlet.multipart.max-request-size = 70MB
|
||||
spring.servlet.multipart.max-request-size = 100MB
|
||||
|
||||
spring.main.allow-bean-definition-overriding = true
|
||||
|
||||
@ -69,6 +69,9 @@ my.GeoserverAdress = http://139.199.98.175:9080/geoserver/
|
||||
my.FileServerAdress = http://139.199.98.175:4096/
|
||||
my.FileServerAdminAdress = http://127.0.0.1:4096/
|
||||
|
||||
dfs.ip = 192.168.2.9
|
||||
dfs.port = 4096
|
||||
|
||||
## 自定义用户最大登录错误尝试次数
|
||||
my.MaxLoginErrorCount = 5
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user