Skip to content

Commit fcbe726

Browse files
committed
m
1 parent e73f99b commit fcbe726

File tree

6 files changed

+63
-36
lines changed

6 files changed

+63
-36
lines changed

spring-boot-fastDFS/src/main/java/com/neo/config/Configurations.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

spring-boot-fastDFS/src/main/java/com/neo/controller/UploadController.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.neo.controller;
22

3-
import com.neo.config.Configurations;
43
import com.neo.fastdfs.FastDFSClient;
54
import com.neo.fastdfs.FastDFSFile;
65
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
8-
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.stereotype.Controller;
108
import org.springframework.web.bind.annotation.GetMapping;
119
import org.springframework.web.bind.annotation.PostMapping;
@@ -19,8 +17,6 @@
1917
@Controller
2018
public class UploadController {
2119
private static Logger logger = LoggerFactory.getLogger(UploadController.class);
22-
@Autowired
23-
private Configurations configuration;
2420

2521
@GetMapping("/")
2622
public String index() {
@@ -34,7 +30,6 @@ public String singleFileUpload(@RequestParam("file") MultipartFile file,
3430
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
3531
return "redirect:uploadStatus";
3632
}
37-
3833
try {
3934
// Get the file and save it somewhere
4035
String path=saveFile(file);
@@ -73,13 +68,13 @@ public String saveFile(MultipartFile multipartFile) throws IOException {
7368
FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
7469
try {
7570
fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs
76-
} catch (Exception e1) {
77-
e1.printStackTrace();
71+
} catch (Exception e) {
72+
logger.error("upload file Exception!",e);
7873
}
7974
if (fileAbsolutePath==null) {
80-
System.out.println("upload file failed,please upload again!");
75+
logger.error("upload file failed,please upload again!");
8176
}
82-
String path=configuration.getFdfsUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
77+
String path=FastDFSClient.getTrackerUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
8378
return path;
8479
}
8580
}

spring-boot-fastDFS/src/main/java/com/neo/fastdfs/FastDFSClient.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ public class FastDFSClient {
1717

1818
static {
1919
try {
20-
Resource resource = new ClassPathResource("fdfs_client.conf");
21-
File file = resource.getFile();
22-
String configFile = file.getAbsolutePath();
23-
24-
ClientGlobal.init(configFile);
20+
String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
21+
ClientGlobal.init(filePath);
2522
trackerClient = new TrackerClient();
2623
trackerServer = trackerClient.getConnection();
2724
storageServer = trackerClient.getStoreStorage(trackerServer);
@@ -100,4 +97,8 @@ public static ServerInfo[] getFetchStorages(String groupName,
10097
String remoteFileName) throws IOException {
10198
return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
10299
}
100+
101+
public static String getTrackerUrl() {
102+
return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
103+
}
103104
}

spring-boot-fastDFS/src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
spring.http.multipart.max-file-size=10MB
44
spring.http.multipart.max-request-size=10MB
55

6-
fastdfs.base.url=http://192.168.53.85:8080/

spring-boot-fastDFS/src/main/resources/fdfs_client.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
connect_timeout = 2
2-
network_timeout = 30
1+
connect_timeout = 60
2+
network_timeout = 60
33
charset = UTF-8
44
http.tracker_http_port = 8080
55
http.anti_steal_token = no
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<form id='myupload' action='http://localhost:8080/uploadSign' method='post' enctype='multipart/form-data'>
2+
<div class="demo">
3+
<div class="btn">
4+
<span>添加附件</span>
5+
<input id="fileupload" type="file" name="file1"></div>
6+
<div class="progress">
7+
<span class="bar"></span>
8+
<span class="percent">0%</span></div>
9+
<!-- 显示已上传的文件名 -->
10+
<div class="files"></div>
11+
<!-- 显示已上传的图片-->
12+
<div class="showimg"></div>
13+
</div>
14+
<input type="submit" onclick="gosubmit2()" /></form>
15+
<script src="https://cdn.bootcss.com/jquery/1.6.4/jquery.js"></script>
16+
<script type="text/javascript" src="https://cdn.bootcss.com/jquery.form/4.1.0/jquery.form.min.js"></script>
17+
<script type="text/javascript">var bar = $('.bar'); //进度条
18+
var percent = $('.percent'); //获取上传百分比
19+
var showimg = $('.showimg'); //显示图片的div
20+
var progress = $('.progress'); //显示进度的div
21+
var files = $('.files'); //文件上传控件的input元素
22+
var btn = $('.btn span'); //按钮文本
23+
function gosubmit2() {
24+
$("#myupload").ajaxSubmit({
25+
dataType: 'json',
26+
//返回数据类型
27+
beforeSend: function() {
28+
showimg.empty();
29+
progress.show();
30+
var percentVal = '0%';
31+
bar.width(percentVal);
32+
percent.html(percentVal);
33+
btn.html('上传中..');
34+
},
35+
//更新进度条事件处理代码
36+
uploadProgress: function(event, position, total, percentComplete) {
37+
var percentVal = percentComplete + '%';
38+
bar.width(percentVal);
39+
percent.html(percentVal);
40+
},
41+
success: function(data) { //图片上传成功时
42+
//获取服务器端返回的文件数据
43+
alert(data.name + "," + data.pic + "," + data.size);
44+
},
45+
error: function(xhr) {
46+
btn.html(上传失败);
47+
bar.width('0');
48+
files.html(xhr.responseText);
49+
}
50+
});
51+
}</script>

0 commit comments

Comments
 (0)