From fcbe72632ff542a63fa624d7bdd187a970c768f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=AF=E6=B4=81=E7=9A=84=E5=BE=AE=E7=AC=91?= Date: Sat, 13 Jan 2018 20:07:57 +0800 Subject: [PATCH] m --- .../java/com/neo/config/Configurations.java | 19 ------- .../com/neo/controller/UploadController.java | 13 ++--- .../java/com/neo/fastdfs/FastDFSClient.java | 11 ++-- .../src/main/resources/application.properties | 1 - .../src/main/resources/fdfs_client.conf | 4 +- .../main/resources/templates/from_file.html | 51 +++++++++++++++++++ 6 files changed, 63 insertions(+), 36 deletions(-) delete mode 100644 spring-boot-fastDFS/src/main/java/com/neo/config/Configurations.java create mode 100644 spring-boot-file-upload/src/main/resources/templates/from_file.html diff --git a/spring-boot-fastDFS/src/main/java/com/neo/config/Configurations.java b/spring-boot-fastDFS/src/main/java/com/neo/config/Configurations.java deleted file mode 100644 index 4e1a8f2..0000000 --- a/spring-boot-fastDFS/src/main/java/com/neo/config/Configurations.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.neo.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component -public class Configurations { - - @Value("${fastdfs.base.url}") - private String fdfsUrl; - - public String getFdfsUrl() { - return fdfsUrl; - } - - public void setFdfsUrl(String fdfsUrl) { - this.fdfsUrl = fdfsUrl; - } -} \ No newline at end of file diff --git a/spring-boot-fastDFS/src/main/java/com/neo/controller/UploadController.java b/spring-boot-fastDFS/src/main/java/com/neo/controller/UploadController.java index 06941e7..4dd3bf2 100644 --- a/spring-boot-fastDFS/src/main/java/com/neo/controller/UploadController.java +++ b/spring-boot-fastDFS/src/main/java/com/neo/controller/UploadController.java @@ -1,11 +1,9 @@ package com.neo.controller; -import com.neo.config.Configurations; import com.neo.fastdfs.FastDFSClient; import com.neo.fastdfs.FastDFSFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -19,8 +17,6 @@ import java.io.InputStream; @Controller public class UploadController { private static Logger logger = LoggerFactory.getLogger(UploadController.class); - @Autowired - private Configurations configuration; @GetMapping("/") public String index() { @@ -34,7 +30,6 @@ public class UploadController { redirectAttributes.addFlashAttribute("message", "Please select a file to upload"); return "redirect:uploadStatus"; } - try { // Get the file and save it somewhere String path=saveFile(file); @@ -73,13 +68,13 @@ public class UploadController { FastDFSFile file = new FastDFSFile(fileName, file_buff, ext); try { fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs - } catch (Exception e1) { - e1.printStackTrace(); + } catch (Exception e) { + logger.error("upload file Exception!",e); } if (fileAbsolutePath==null) { - System.out.println("upload file failed,please upload again!"); + logger.error("upload file failed,please upload again!"); } - String path=configuration.getFdfsUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1]; + String path=FastDFSClient.getTrackerUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1]; return path; } } \ No newline at end of file diff --git a/spring-boot-fastDFS/src/main/java/com/neo/fastdfs/FastDFSClient.java b/spring-boot-fastDFS/src/main/java/com/neo/fastdfs/FastDFSClient.java index a274b41..ce95550 100644 --- a/spring-boot-fastDFS/src/main/java/com/neo/fastdfs/FastDFSClient.java +++ b/spring-boot-fastDFS/src/main/java/com/neo/fastdfs/FastDFSClient.java @@ -17,11 +17,8 @@ public class FastDFSClient { static { try { - Resource resource = new ClassPathResource("fdfs_client.conf"); - File file = resource.getFile(); - String configFile = file.getAbsolutePath(); - - ClientGlobal.init(configFile); + String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();; + ClientGlobal.init(filePath); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = trackerClient.getStoreStorage(trackerServer); @@ -100,4 +97,8 @@ public class FastDFSClient { String remoteFileName) throws IOException { return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName); } + + public static String getTrackerUrl() { + return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/"; + } } \ No newline at end of file diff --git a/spring-boot-fastDFS/src/main/resources/application.properties b/spring-boot-fastDFS/src/main/resources/application.properties index 7ec2bb4..45f5833 100644 --- a/spring-boot-fastDFS/src/main/resources/application.properties +++ b/spring-boot-fastDFS/src/main/resources/application.properties @@ -3,4 +3,3 @@ spring.http.multipart.max-file-size=10MB spring.http.multipart.max-request-size=10MB -fastdfs.base.url=http://192.168.53.85:8080/ \ No newline at end of file diff --git a/spring-boot-fastDFS/src/main/resources/fdfs_client.conf b/spring-boot-fastDFS/src/main/resources/fdfs_client.conf index 949c802..95cb59c 100644 --- a/spring-boot-fastDFS/src/main/resources/fdfs_client.conf +++ b/spring-boot-fastDFS/src/main/resources/fdfs_client.conf @@ -1,5 +1,5 @@ -connect_timeout = 2 -network_timeout = 30 +connect_timeout = 60 +network_timeout = 60 charset = UTF-8 http.tracker_http_port = 8080 http.anti_steal_token = no diff --git a/spring-boot-file-upload/src/main/resources/templates/from_file.html b/spring-boot-file-upload/src/main/resources/templates/from_file.html new file mode 100644 index 0000000..3f95f35 --- /dev/null +++ b/spring-boot-file-upload/src/main/resources/templates/from_file.html @@ -0,0 +1,51 @@ +
+
+
+ 添加附件 +
+
+ + 0%
+ +
+ +
+
+
+ + + \ No newline at end of file