Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
K
know-base
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
张京坤
know-base
Commits
5dd9e464
提交
5dd9e464
authored
1月 10, 2024
作者:
ZhangJingKun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
文件上传 zhangjingkun
上级
ff4c923a
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
102 行增加
和
13 行删除
+102
-13
KnowledgeController.java
...ava/com/zzsn/knowbase/controller/KnowledgeController.java
+9
-6
ILocalFileService.java
...ain/java/com/zzsn/knowbase/service/ILocalFileService.java
+28
-1
LocalFileServiceImpl.java
.../com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
+45
-5
KnowBaseApplicationTests.java
...test/java/com/zzsn/knowbase/KnowBaseApplicationTests.java
+20
-1
没有找到文件。
src/main/java/com/zzsn/knowbase/controller/KnowledgeController.java
浏览文件 @
5dd9e464
...
...
@@ -26,6 +26,8 @@ import org.springframework.web.util.WebUtils;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
/**
* @Description: 知识
...
...
@@ -83,13 +85,14 @@ public class KnowledgeController {
@PostMapping
(
value
=
"/uploadKnowledge"
)
public
Result
<?>
uploadKnowledge
(
HttpServletRequest
request
,
Knowledge
knowledge
)
{
MultipartHttpServletRequest
multipartRequest
=
WebUtils
.
getNativeRequest
(
request
,
MultipartHttpServletRequest
.
class
);
MultipartFile
file
=
multipartRequest
.
getFile
(
"file"
);
Result
<
KnowFile
>
upload
=
localFileService
.
upload
(
file
,
knowledge
.
getId
());
if
(
null
==
upload
||
upload
.
getResult
()==
null
){
return
Result
.
error
(
"上传文件失败"
);
}
knowledgeService
.
addKnowledge
(
upload
.
getResult
(),
knowledge
);
Map
<
String
,
MultipartFile
>
fileMap
=
multipartRequest
.
getFileMap
();
List
<
Result
<
KnowFile
>>
resultList
=
localFileService
.
upload
(
fileMap
);
// if(null==upload||upload.getResult()==null){
// return Result.error("上传文件失败");
// }
//knowledgeService.addKnowledge(upload.getResult(),knowledge);
return
Result
.
OK
(
"添加成功!"
);
}
...
...
src/main/java/com/zzsn/knowbase/service/ILocalFileService.java
浏览文件 @
5dd9e464
...
...
@@ -2,8 +2,13 @@ package com.zzsn.knowbase.service;
import
com.zzsn.knowbase.entity.KnowFile
;
import
com.zzsn.knowbase.vo.Result
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.util.List
;
import
java.util.Map
;
/**
* @Version 1.0
* @Author: ZhangJingKun
...
...
@@ -12,5 +17,27 @@ import org.springframework.web.multipart.MultipartFile;
*/
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
);
}
src/main/java/com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
浏览文件 @
5dd9e464
...
...
@@ -7,15 +7,22 @@ import com.zzsn.knowbase.vo.Result;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
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.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.MalformedURLException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
/**
...
...
@@ -35,7 +42,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
String
filesStorage
;
@Override
public
Result
<
KnowFile
>
upload
(
MultipartFile
file
,
String
uid
)
{
public
Result
<
KnowFile
>
upload
(
MultipartFile
file
)
{
try
{
String
fullFileName
=
file
.
getOriginalFilename
();
// get file name
String
fileExtension
=
fileUtility
.
getFileExtension
(
fullFileName
);
// get file extension
...
...
@@ -46,7 +53,6 @@ public class LocalFileServiceImpl implements ILocalFileService {
Result
result
=
Result
.
error
(
"文件大小不正确!"
);
return
result
;
}
// check if file extension is supported by the editor
if
(!
fileUtility
.
getFileExts
().
contains
(
fileExtension
))
{
Result
result
=
Result
.
error
(
"不支持的文件类型!"
);
...
...
@@ -56,7 +62,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
String
fileName
=
file
.
getOriginalFilename
();
String
fileSuffix
=
getFileSuffix
(
fileName
);
uid
=
UUID
.
randomUUID
().
toString
();
String
uid
=
UUID
.
randomUUID
().
toString
();
String
filePath
=
getFilePath
()
+
uid
+
"."
+
fileSuffix
;
byte
[]
bytes
=
file
.
getBytes
();
// get file in bytes
...
...
@@ -79,6 +85,41 @@ public class LocalFileServiceImpl implements ILocalFileService {
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
(){
LocalDate
currentDate
=
LocalDate
.
now
();
//System.out.println("当前日期: " + currentDate);
...
...
@@ -97,7 +138,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
}
return
filePath
;
}
//获取文件后缀
private
String
getFileSuffix
(
String
fileName
){
int
lastIndexOfDot
=
fileName
.
lastIndexOf
(
'.'
);
String
fileExtension
=
""
;
...
...
@@ -107,5 +148,4 @@ public class LocalFileServiceImpl implements ILocalFileService {
}
return
fileExtension
;
}
}
src/test/java/com/zzsn/knowbase/KnowBaseApplicationTests.java
浏览文件 @
5dd9e464
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.springframework.beans.factory.annotation.Autowired
;
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
class
KnowBaseApplicationTests
{
@Autowired
private
CodeGenerateUtil
codeGenerateUtil
;
@Autowired
private
ILocalFileService
localFileService
;
@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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论