Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
ityouknow committed Jan 13, 2018
1 parent e73f99b commit fcbe726
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.

This file was deleted.

@@ -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;
Expand All @@ -19,8 +17,6 @@
@Controller
public class UploadController {
private static Logger logger = LoggerFactory.getLogger(UploadController.class);
@Autowired
private Configurations configuration;

@GetMapping("/")
public String index() {
Expand All @@ -34,7 +30,6 @@ public String singleFileUpload(@RequestParam("file") MultipartFile file,
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}

try {
// Get the file and save it somewhere
String path=saveFile(file);
Expand Down Expand Up @@ -73,13 +68,13 @@ public String saveFile(MultipartFile multipartFile) throws IOException {
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;
}
}
Expand Up @@ -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);
Expand Down Expand Up @@ -100,4 +97,8 @@ public static ServerInfo[] getFetchStorages(String groupName,
String remoteFileName) throws IOException {
return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
}

public static String getTrackerUrl() {
return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
}
}
Expand Up @@ -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/
4 changes: 2 additions & 2 deletions 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
Expand Down
@@ -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>

0 comments on commit fcbe726

Please sign in to comment.