纯洁的微笑 7 years ago
parent
commit
fcbe72632f

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

@@ -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;
-    }
-}

+ 4 - 9
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;
     }
 }

+ 6 - 5
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()+"/";
+	}
 }

+ 0 - 1
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/

+ 2 - 2
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

+ 51 - 0
spring-boot-file-upload/src/main/resources/templates/from_file.html

@@ -0,0 +1,51 @@
+<form id='myupload' action='http://localhost:8080/uploadSign' method='post' enctype='multipart/form-data'>
+  <div class="demo">
+    <div class="btn">
+      <span>添加附件</span>
+      <input id="fileupload" type="file" name="file1"></div>
+    <div class="progress">
+      <span class="bar"></span>
+      <span class="percent">0%</span></div>
+    <!-- 显示已上传的文件名 -->
+    <div class="files"></div>
+    <!-- 显示已上传的图片-->
+    <div class="showimg"></div>
+  </div>
+  <input type="submit" onclick="gosubmit2()" /></form>
+<script src="https://cdn.bootcss.com/jquery/1.6.4/jquery.js"></script>
+<script type="text/javascript" src="https://cdn.bootcss.com/jquery.form/4.1.0/jquery.form.min.js"></script>
+<script type="text/javascript">var bar = $('.bar'); //进度条
+  var percent = $('.percent'); //获取上传百分比
+  var showimg = $('.showimg'); //显示图片的div
+  var progress = $('.progress'); //显示进度的div
+  var files = $('.files'); //文件上传控件的input元素
+  var btn = $('.btn span'); //按钮文本
+  function gosubmit2() {
+    $("#myupload").ajaxSubmit({
+      dataType: 'json',
+      //返回数据类型
+      beforeSend: function() {
+        showimg.empty();
+        progress.show();
+        var percentVal = '0%';
+        bar.width(percentVal);
+        percent.html(percentVal);
+        btn.html('上传中..');
+      },
+      //更新进度条事件处理代码
+      uploadProgress: function(event, position, total, percentComplete) {
+        var percentVal = percentComplete + '%';
+        bar.width(percentVal);
+        percent.html(percentVal);
+      },
+      success: function(data) { //图片上传成功时
+        //获取服务器端返回的文件数据
+        alert(data.name + "," + data.pic + "," + data.size);
+      },
+      error: function(xhr) {
+        btn.html(上传失败);
+        bar.width('0');
+        files.html(xhr.responseText);
+      }
+    });
+  }</script>