提交 5dd9e464 作者: ZhangJingKun

文件上传 zhangjingkun

上级 ff4c923a
...@@ -26,6 +26,8 @@ import org.springframework.web.util.WebUtils; ...@@ -26,6 +26,8 @@ import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Map;
/** /**
* @Description: 知识 * @Description: 知识
...@@ -83,13 +85,14 @@ public class KnowledgeController { ...@@ -83,13 +85,14 @@ public class KnowledgeController {
@PostMapping(value = "/uploadKnowledge") @PostMapping(value = "/uploadKnowledge")
public Result<?> uploadKnowledge(HttpServletRequest request, Knowledge knowledge) { public Result<?> uploadKnowledge(HttpServletRequest request, Knowledge knowledge) {
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
MultipartFile file = multipartRequest.getFile("file");
Result<KnowFile> upload = localFileService.upload(file, knowledge.getId()); Map<String,MultipartFile> fileMap = multipartRequest.getFileMap();
if(null==upload||upload.getResult()==null){ List<Result<KnowFile>> resultList = localFileService.upload(fileMap);
return Result.error("上传文件失败");
} // if(null==upload||upload.getResult()==null){
knowledgeService.addKnowledge(upload.getResult(),knowledge); // return Result.error("上传文件失败");
// }
//knowledgeService.addKnowledge(upload.getResult(),knowledge);
return Result.OK("添加成功!"); return Result.OK("添加成功!");
} }
......
...@@ -2,8 +2,13 @@ package com.zzsn.knowbase.service; ...@@ -2,8 +2,13 @@ package com.zzsn.knowbase.service;
import com.zzsn.knowbase.entity.KnowFile; import com.zzsn.knowbase.entity.KnowFile;
import com.zzsn.knowbase.vo.Result; import com.zzsn.knowbase.vo.Result;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
/** /**
* @Version 1.0 * @Version 1.0
* @Author: ZhangJingKun * @Author: ZhangJingKun
...@@ -12,5 +17,27 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -12,5 +17,27 @@ import org.springframework.web.multipart.MultipartFile;
*/ */
public interface ILocalFileService { public interface ILocalFileService {
Result<KnowFile> upload(MultipartFile file, String uid); /**
* 文件上传
* @param file
* @return
*/
Result<KnowFile> upload(MultipartFile file);
/**
* 批量文件上传
* @param fileMap
* @return
*/
List<Result<KnowFile>> upload(Map<String,MultipartFile> fileMap);
/**
* 文件下载
* @param fileName
* @param filePath
* @return
*/
ResponseEntity<Resource> download(String fileName, String filePath);
} }
...@@ -7,15 +7,22 @@ import com.zzsn.knowbase.vo.Result; ...@@ -7,15 +7,22 @@ import com.zzsn.knowbase.vo.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
...@@ -35,7 +42,7 @@ public class LocalFileServiceImpl implements ILocalFileService { ...@@ -35,7 +42,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
String filesStorage; String filesStorage;
@Override @Override
public Result<KnowFile> upload(MultipartFile file, String uid) { public Result<KnowFile> upload(MultipartFile file) {
try { try {
String fullFileName = file.getOriginalFilename(); // get file name String fullFileName = file.getOriginalFilename(); // get file name
String fileExtension = fileUtility.getFileExtension(fullFileName); // get file extension String fileExtension = fileUtility.getFileExtension(fullFileName); // get file extension
...@@ -46,7 +53,6 @@ public class LocalFileServiceImpl implements ILocalFileService { ...@@ -46,7 +53,6 @@ public class LocalFileServiceImpl implements ILocalFileService {
Result result = Result.error("文件大小不正确!"); Result result = Result.error("文件大小不正确!");
return result; return result;
} }
// check if file extension is supported by the editor // check if file extension is supported by the editor
if (!fileUtility.getFileExts().contains(fileExtension)) { if (!fileUtility.getFileExts().contains(fileExtension)) {
Result result = Result.error("不支持的文件类型!"); Result result = Result.error("不支持的文件类型!");
...@@ -56,7 +62,7 @@ public class LocalFileServiceImpl implements ILocalFileService { ...@@ -56,7 +62,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
String fileSuffix = getFileSuffix(fileName); String fileSuffix = getFileSuffix(fileName);
uid = UUID.randomUUID().toString(); String uid = UUID.randomUUID().toString();
String filePath = getFilePath() + uid + "." + fileSuffix; String filePath = getFilePath() + uid + "." + fileSuffix;
byte[] bytes = file.getBytes(); // get file in bytes byte[] bytes = file.getBytes(); // get file in bytes
...@@ -79,6 +85,41 @@ public class LocalFileServiceImpl implements ILocalFileService { ...@@ -79,6 +85,41 @@ public class LocalFileServiceImpl implements ILocalFileService {
return result; return result;
} }
@Override
public List<Result<KnowFile>> upload(Map<String, MultipartFile> fileMap) {
List<Result<KnowFile>> list = new ArrayList<>();
for (Map.Entry<String,MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
Result<KnowFile> result = upload(file);
list.add(result);
}
return list;
}
/**
* 文件下载
*/
@Override
public ResponseEntity<Resource> download(String fileName, String filePath) {
Path path = Paths.get(filePath);
try {
Resource resource = new UrlResource(path.toUri());
if (resource.exists() || resource.isReadable()) {
return ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"")
.body(resource);
} else {
throw new RuntimeException("文件不存在或不可读");
}
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("文件读取失败");
}
}
//获取文件夹路径
private String getFilePath(){ private String getFilePath(){
LocalDate currentDate = LocalDate.now(); LocalDate currentDate = LocalDate.now();
//System.out.println("当前日期: " + currentDate); //System.out.println("当前日期: " + currentDate);
...@@ -97,7 +138,7 @@ public class LocalFileServiceImpl implements ILocalFileService { ...@@ -97,7 +138,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
} }
return filePath; return filePath;
} }
//获取文件后缀
private String getFileSuffix(String fileName){ private String getFileSuffix(String fileName){
int lastIndexOfDot = fileName.lastIndexOf('.'); int lastIndexOfDot = fileName.lastIndexOf('.');
String fileExtension = ""; String fileExtension = "";
...@@ -107,5 +148,4 @@ public class LocalFileServiceImpl implements ILocalFileService { ...@@ -107,5 +148,4 @@ public class LocalFileServiceImpl implements ILocalFileService {
} }
return fileExtension; return fileExtension;
} }
} }
package com.zzsn.knowbase; package com.zzsn.knowbase;
import com.zzsn.knowbase.constant.Constants;
import com.zzsn.knowbase.service.ILocalFileService;
import com.zzsn.knowbase.util.CodeGenerateUtil;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import java.io.File;
import java.io.IOException;
@SpringBootTest @SpringBootTest
class KnowBaseApplicationTests { class KnowBaseApplicationTests {
@Autowired
private CodeGenerateUtil codeGenerateUtil;
@Autowired
private ILocalFileService localFileService;
@Test @Test
void contextLoads() { void contextLoads() throws IOException {
ResponseEntity<Resource> re = localFileService.download("abc.docx", "E:/aaa/abc.docx");
File file = re.getBody().getFile();
System.out.println(re);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论