|
@ -20,12 +20,11 @@ import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.*;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
*文件服务
|
|
|
* 文件服务
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping(HealthyHouseMapping.api_healthyHouse_common)
|
|
@ -39,16 +38,20 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
@Autowired
|
|
|
private FileResourceService fileResourceService;
|
|
|
|
|
|
@Value("${img.path}")
|
|
|
private String img_path;
|
|
|
|
|
|
/**
|
|
|
* 文件上传 - 返回相关索引信息
|
|
|
*
|
|
|
* @param file
|
|
|
* @param creator
|
|
|
* @param objectId
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping(value =HealthyHouseMapping.HealthyHouse.FastDFS.UPLOAD )
|
|
|
@PostMapping(value = HealthyHouseMapping.HealthyHouse.FastDFS.UPLOAD)
|
|
|
@ApiOperation(value = "文件上传", notes = "返回相关索引信息,以及HttpUrl下载连接")
|
|
|
public ObjEnvelop<FileResource> upload (
|
|
|
public ObjEnvelop<FileResource> upload(
|
|
|
@ApiParam(name = "file", value = "文件", required = true)
|
|
|
@RequestPart(value = "file") MultipartFile file,
|
|
|
@ApiParam(name = "creator", value = "创建者", required = true)
|
|
@ -61,31 +64,34 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
fileExtension = "file";
|
|
|
}
|
|
|
//上传
|
|
|
ObjectNode objectNode = fastDFSUtil.upload(file.getInputStream(), fileExtension, "svr-dfs");
|
|
|
String groupName = objectNode.get(FastDFSUtil.GROUP_NAME).toString();
|
|
|
String remoteFileName = objectNode.get(FastDFSUtil.REMOTE_FILE_NAME).toString();
|
|
|
int fileSize = objectNode.get(FastDFSUtil.FILE_SIZE).asInt();
|
|
|
String path = groupName.substring(1, groupName.length() - 1) + ":" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
//需要随机生成一个uuid作为文件名,防止冲突
|
|
|
String path = upload(file.getInputStream(), fileName);
|
|
|
// ObjectNode objectNode = fastDFSUtil.upload(file.getInputStream(), fileExtension, "svr-dfs");
|
|
|
// String groupName = objectNode.get(FastDFSUtil.GROUP_NAME).toString();
|
|
|
// String remoteFileName = objectNode.get(FastDFSUtil.REMOTE_FILE_NAME).toString();
|
|
|
// int fileSize = objectNode.get(FastDFSUtil.FILE_SIZE).asInt();
|
|
|
// String path = groupName.substring(1, groupName.length() - 1) + ":" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
//路径存储到mysql数据库
|
|
|
FileResource fileResource =new FileResource();
|
|
|
FileResource fileResource = new FileResource();
|
|
|
fileResource.setObjectId(objectId);
|
|
|
fileResource.setStoragePath(path);
|
|
|
fileResource.setFileSize(String.valueOf(fileSize));
|
|
|
// fileResource.setFileSize(String.valueOf(fileSize));
|
|
|
fileResource.setFileName(fileName);
|
|
|
fileResource.setCreateUser(creator);
|
|
|
fileResource= fileResourceService.save(fileResource);
|
|
|
path = groupName.substring(1, groupName.length() - 1) + "/" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
fileResource = fileResourceService.save(fileResource);
|
|
|
// path = groupName.substring(1, groupName.length() - 1) + "/" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
fileResource.setStoragePath(path);
|
|
|
return success(fileResource);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 文件上传 - 返回相关索引信息,兼容旧接口
|
|
|
*
|
|
|
* @param jsonData
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@PostMapping(value = HealthyHouseMapping.HealthyHouse.FastDFS.UPLOADJSON,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
@PostMapping(value = HealthyHouseMapping.HealthyHouse.FastDFS.UPLOADJSON, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
@ApiOperation(value = "文件上传", notes = "返回相关索引信息,以及HttpUrl下载连接(兼容旧接口)")
|
|
|
public ObjEnvelop<FileResource> upload(
|
|
|
@ApiParam(name = "jsonData", value = "文件资源", required = true)
|
|
@ -106,19 +112,20 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
String remoteFileName = objectNode.get(FastDFSUtil.REMOTE_FILE_NAME).toString();
|
|
|
String path = groupName.substring(1, groupName.length() - 1) + ":" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
//路径存储到mysql数据库
|
|
|
FileResource fileResource =new FileResource();
|
|
|
FileResource fileResource = new FileResource();
|
|
|
fileResource.setObjectId(paramMap.get("object"));
|
|
|
fileResource.setStoragePath(path);
|
|
|
fileResource.setFileSize(String.valueOf(fileSize));
|
|
|
fileResource.setFileName(fileName);
|
|
|
fileResource= fileResourceService.save(fileResource);
|
|
|
path = groupName.substring(1, groupName.length() - 1) + "/" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
fileResource.setStoragePath( path);
|
|
|
fileResource = fileResourceService.save(fileResource);
|
|
|
path = groupName.substring(1, groupName.length() - 1) + "/" + remoteFileName.substring(1, remoteFileName.length() - 1);
|
|
|
fileResource.setStoragePath(path);
|
|
|
return success(fileResource);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除资源表对应关系,并且删除fastDfs相对应当文件
|
|
|
*
|
|
|
* @param id
|
|
|
* @return
|
|
|
* @throws Exception
|
|
@ -128,10 +135,10 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
public Envelop deleteById(
|
|
|
@ApiParam(name = "id", value = "id", required = true)
|
|
|
@RequestParam(value = "id") String id) throws Exception {
|
|
|
FileResource fileResource= fileResourceService.findById(id);
|
|
|
if(null==fileResource){
|
|
|
return failed("无相关文件资源");
|
|
|
}
|
|
|
FileResource fileResource = fileResourceService.findById(id);
|
|
|
if (null == fileResource) {
|
|
|
return failed("无相关文件资源");
|
|
|
}
|
|
|
|
|
|
String storagePath = fileResource.getStoragePath();
|
|
|
String groupName = storagePath.split(":")[0];
|
|
@ -144,6 +151,7 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
|
|
|
/**
|
|
|
* 删除资源表对应关系,并且删除fastDfs相对应当文件
|
|
|
*
|
|
|
* @param path
|
|
|
* @return
|
|
|
* @throws Exception
|
|
@ -158,10 +166,10 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
}
|
|
|
// 删除文件
|
|
|
fastDFSUtil.delete(path.split(":")[0], path.split(":")[1]);
|
|
|
List<FileResource> fileResourceList= fileResourceService.findByField("storagePath",path);
|
|
|
List<FileResource> fileResourceList = fileResourceService.findByField("storagePath", path);
|
|
|
StringBuilder ids = new StringBuilder();
|
|
|
for (FileResource fileResource : fileResourceList) {
|
|
|
String id= fileResource.getId();
|
|
|
String id = fileResource.getId();
|
|
|
ids.append(id + ",");
|
|
|
}
|
|
|
// 删除mysql数据
|
|
@ -171,16 +179,17 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
|
|
|
/**
|
|
|
* 下载文件
|
|
|
*
|
|
|
* @param id
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@GetMapping(value = HealthyHouseMapping.HealthyHouse.FastDFS.DOWNLOAD_BY_ID)
|
|
|
@ApiOperation(value = "下载文件(byId)")
|
|
|
public Envelop downloadById (
|
|
|
public Envelop downloadById(
|
|
|
@ApiParam(name = "id", value = "id", required = true)
|
|
|
@RequestParam(value = "id") String id) throws Exception {
|
|
|
FileResource fileResource= fileResourceService.findById(id);
|
|
|
FileResource fileResource = fileResourceService.findById(id);
|
|
|
String storagePath = fileResource.getStoragePath();
|
|
|
String groupName = storagePath.split(":")[0];
|
|
|
String remoteFileName = storagePath.split(":")[1];
|
|
@ -194,6 +203,7 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
|
|
|
/**
|
|
|
* 下载文件
|
|
|
*
|
|
|
* @param path
|
|
|
* @return
|
|
|
* @throws Exception
|
|
@ -217,4 +227,40 @@ public class FastDFSController extends EnvelopRestEndpoint {
|
|
|
}
|
|
|
|
|
|
|
|
|
public String upload(InputStream inputStream, String fileName) {
|
|
|
OutputStream outputStream = null;
|
|
|
String path1 = img_path + "\\" + fileName;
|
|
|
try {
|
|
|
//文件源
|
|
|
File files = new File(img_path);
|
|
|
if (!files.exists()) {//判断文件夹是否创建,没有创建则创建新文件夹
|
|
|
files.mkdirs();
|
|
|
}
|
|
|
File toFile = new File(path1);
|
|
|
outputStream = new FileOutputStream(toFile);
|
|
|
byte[] fulsh = new byte[1024];
|
|
|
int len;
|
|
|
while ((len = inputStream.read(fulsh)) != -1) {
|
|
|
outputStream.write(fulsh, 0, len);
|
|
|
}
|
|
|
} catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
if (null != outputStream) {
|
|
|
outputStream.close();
|
|
|
}
|
|
|
if (null != inputStream) {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return path1;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|