Skip to content

Commit

Permalink
Merge pull request #89 from gitxiaofeng/master
Browse files Browse the repository at this point in the history
feature:增加图片功能
  • Loading branch information
gitxiaofeng authored Apr 29, 2021
2 parents 01ab75c + ab8afab commit 7bef203
Show file tree
Hide file tree
Showing 29 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.xiaoju.framework.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoju.framework.constants.SystemConstant;
import com.xiaoju.framework.constants.enums.StatusCode;
import com.xiaoju.framework.entity.exception.CaseServerException;
Expand All @@ -10,15 +13,24 @@
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* 文件上传与导出
Expand Down Expand Up @@ -64,6 +76,45 @@ public Response<Long> importXmind(@RequestParam MultipartFile file, String creat
}
}

@Value("${web.upload-path}")
private String uploadPath;

@PostMapping(value = "/uploadAttachment", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public JSONObject uploadAttachment(@RequestParam MultipartFile file, HttpServletRequest request) {

JSONObject ret = new JSONObject();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
String format = sdf.format(new Date());
File folder = new File(uploadPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
// 对上传的文件重命名,避免文件重名
String oldName = file.getOriginalFilename();
String newName = UUID.randomUUID().toString()
+ oldName.substring(oldName.lastIndexOf("."), oldName.length());

try {
// 文件保存
file.transferTo(new File(folder, newName));

// 返回上传文件的访问路径
String filePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + "/" + format + newName;
JSONArray datas = new JSONArray();
JSONObject data = new JSONObject();
data.put("url", filePath);
ret.put("success", 1);
datas.add(data);
ret.put("data", datas);
return ret;
} catch (IOException e) {
LOGGER.error("上传文件失败, 请重试。", e);
ret.put("success", 0);
ret.put("data", "");
return ret;
}
}
/**
* 根据caseId导出用例
* response 文件在http响应中输出
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public String tcCase(){
return "index";
}

@RequestMapping(value="/api/file/*")
public String file() {
System.out.println("pre request");
return "index";
}
}
17 changes: 15 additions & 2 deletions case-server/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ spring.datasource.username=root
spring.datasource.password=brishenhan
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 数据库ORM配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
Expand All @@ -18,4 +17,18 @@ spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000

logging.config=classpath:log4j2.xml

spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.mvc.static-path-pattern=/**
# 配置本地图片文件保存目录
web.upload-path=/Users/xxx/Documents/
spring.resources.static-locations=classpath:/web/dist/,file:${web.upload-path}
spring.thymeleaf.prefix=classpath:/web/dist/
spring.thymeleaf.suffix=.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion case-server/src/main/resources/web/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
</head>
<body>
<div id="root"></div>
<script th:src="@{/umi.f5392ca2.js}"></script>
<script th:src="@{/umi.ab5c5558.js}"></script>
</body>
</html>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions case-server/src/main/resources/web/dist/umi.ab5c5558.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion case-server/src/main/resources/web/dist/umi.f5392ca2.js

This file was deleted.

Large diffs are not rendered by default.

This file was deleted.

2 changes: 1 addition & 1 deletion case-server/src/main/resources/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"rc-scroll-anim": "^2.7.4",
"rc-texty": "^0.2.0",
"react": "^16.8.6",
"react-agiletc-editor": "0.1.17-beta.9",
"react-agiletc-editor": "^0.1.17-beta.10",
"react-dom": "^16.8.6",
"react-resize-panel": "^0.3.5"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,13 @@ export default class CaseMgt extends React.Component {
mediaShow={!progressShow}
editorStyle={{ height: 'calc(100vh - 100px)' }}
toolbar={{
image: false,
image: true,
theme: ['classic-compact', 'fresh-blue', 'fresh-green-compat'],
template: ['default', 'right', 'fish-bone'],
noteTemplate: '# test',
}}
baseUrl="/"
uploadUrl="/api/projmgr/common/uploadAttachment"
baseUrl=""
uploadUrl="/api/file/uploadAttachment"
wsUrl={`ws://${window.location.host}/api/case/${caseId}/${itemid}/${iscore}/${user}`}
onSave={
Number(iscore) !== 2
Expand Down

0 comments on commit 7bef203

Please sign in to comment.