mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-03-13 10:22:11 +08:00
m
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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()+"/";
|
||||
}
|
||||
}
|
||||
@@ -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/
|
||||
@@ -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
|
||||
|
||||
@@ -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><EFBFBD><EFBFBD><EFBFBD>Ӹ<EFBFBD><EFBFBD><EFBFBD></span>
|
||||
<input id="fileupload" type="file" name="file1"></div>
|
||||
<div class="progress">
|
||||
<span class="bar"></span>
|
||||
<span class="percent">0%</span></div>
|
||||
<!-- <20><>ʾ<EFBFBD><CABE><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD> -->
|
||||
<div class="files"></div>
|
||||
<!-- <20><>ʾ<EFBFBD><CABE><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD>ͼƬ-->
|
||||
<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'); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
var percent = $('.percent'); //<2F><>ȡ<EFBFBD>ϴ<EFBFBD><CFB4>ٷֱ<D9B7>
|
||||
var showimg = $('.showimg'); //<2F><>ʾͼƬ<CDBC><C6AC>div
|
||||
var progress = $('.progress'); //<2F><>ʾ<EFBFBD><CABE><EFBFBD>ȵ<EFBFBD>div
|
||||
var files = $('.files'); //<2F>ļ<EFBFBD><C4BC>ϴ<EFBFBD><CFB4>ؼ<EFBFBD><D8BC><EFBFBD>inputԪ<74><D4AA>
|
||||
var btn = $('.btn span'); //<2F><>ť<EFBFBD>ı<EFBFBD>
|
||||
function gosubmit2() {
|
||||
$("#myupload").ajaxSubmit({
|
||||
dataType: 'json',
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
beforeSend: function() {
|
||||
showimg.empty();
|
||||
progress.show();
|
||||
var percentVal = '0%';
|
||||
bar.width(percentVal);
|
||||
percent.html(percentVal);
|
||||
btn.html('<27>ϴ<EFBFBD><CFB4><EFBFBD>..');
|
||||
},
|
||||
//<2F><><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
uploadProgress: function(event, position, total, percentComplete) {
|
||||
var percentVal = percentComplete + '%';
|
||||
bar.width(percentVal);
|
||||
percent.html(percentVal);
|
||||
},
|
||||
success: function(data) { //ͼƬ<CDBC>ϴ<EFBFBD><CFB4>ɹ<EFBFBD>ʱ
|
||||
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><CBB7>ص<EFBFBD><D8B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
||||
alert(data.name + "," + data.pic + "," + data.size);
|
||||
},
|
||||
error: function(xhr) {
|
||||
btn.html(<EFBFBD>ϴ<EFBFBD>ʧ<EFBFBD><EFBFBD>);
|
||||
bar.width('0');
|
||||
files.html(xhr.responseText);
|
||||
}
|
||||
});
|
||||
}</script>
|
||||
Reference in New Issue
Block a user