Commit 502764fb authored by xiujunlian's avatar xiujunlian

上传图片重命名

parent c0739901
...@@ -11,6 +11,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -11,6 +11,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -220,17 +221,27 @@ public class JgSubjectController extends AbstractController { ...@@ -220,17 +221,27 @@ public class JgSubjectController extends AbstractController {
//String url = request.getSession().getServletContext().getRealPath("doc/"); //String url = request.getSession().getServletContext().getRealPath("doc/");
String url ="C:/document/doc"; String url ="C:/document/doc";
String newFileName="";
if (file.isEmpty()) { if (file.isEmpty()) {
throw new RRException("上传文件不能为空"); throw new RRException("上传文件不能为空");
} }
try{ try{
url = url.replaceAll("/", "\\\\"); url = url.replaceAll("/", "\\\\");
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
int i=fileName.lastIndexOf(".");
String newext = fileName.substring(i);
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
Date date = new Date();
newFileName ="图片_"+simpleDateFormat.format(date)+"_"+file.getSize()+newext;
File file3 = new File(url); File file3 = new File(url);
if ((!file3.exists()) && (!file3.isDirectory())) { if ((!file3.exists()) && (!file3.isDirectory())) {
file3.mkdir(); file3.mkdir();
} }
File file2 = new File(file3, fileName); File file2 = new File(file3, newFileName);
InputStream in = file.getInputStream(); InputStream in = file.getInputStream();
FileOutputStream fos = new FileOutputStream(file2); FileOutputStream fos = new FileOutputStream(file2);
BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedOutputStream bos = new BufferedOutputStream(fos);
...@@ -246,7 +257,7 @@ public class JgSubjectController extends AbstractController { ...@@ -246,7 +257,7 @@ public class JgSubjectController extends AbstractController {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return R.ok(); return R.ok().put("filename", newFileName);
} }
/** /**
......
...@@ -5,6 +5,8 @@ import java.io.File; ...@@ -5,6 +5,8 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -25,16 +27,26 @@ public class SysOssController { ...@@ -25,16 +27,26 @@ public class SysOssController {
@RequestMapping("/ToLeadImg") @RequestMapping("/ToLeadImg")
public R ToLeadImg(String editorid, @RequestParam("upfile") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { public R ToLeadImg(String editorid, @RequestParam("upfile") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = ""; String path = "";
String newFileName="";
try { try {
//path = request.getSession().getServletContext().getRealPath("statics/plugins/umeditor/docroot/attachments/article/"); //path = request.getSession().getServletContext().getRealPath("statics/plugins/umeditor/docroot/attachments/article/");
path ="C:/document/attachments/"; path ="C:/document/attachments/";
path = path.replaceAll("/", "\\\\"); path = path.replaceAll("/", "\\\\");
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
int i=fileName.lastIndexOf(".");
String newext = fileName.substring(i);
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
Date date = new Date();
newFileName ="图片_"+simpleDateFormat.format(date)+"_"+file.getSize()+newext;
File file3 = new File(path); File file3 = new File(path);
if ((!file3.exists()) && (!file3.isDirectory())) { if ((!file3.exists()) && (!file3.isDirectory())) {
file3.mkdir(); file3.mkdir();
} }
File file2 = new File(file3, fileName); File file2 = new File(file3, newFileName);
InputStream in = file.getInputStream(); InputStream in = file.getInputStream();
FileOutputStream fos = new FileOutputStream(file2); FileOutputStream fos = new FileOutputStream(file2);
BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedOutputStream bos = new BufferedOutputStream(fos);
...@@ -50,7 +62,7 @@ public class SysOssController { ...@@ -50,7 +62,7 @@ public class SysOssController {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return R.ok().put("url","docroot/attachments/article/"+file.getOriginalFilename()); return R.ok().put("url","docroot/attachments/article/"+newFileName);
} }
@RequestMapping("/ToLead") @RequestMapping("/ToLead")
public String imgController(Map<String, Object> params) { public String imgController(Map<String, Object> params) {
......
...@@ -182,8 +182,7 @@ ...@@ -182,8 +182,7 @@
<div class="form-group"> <div class="form-group">
<div class="col-sm-2 control-label">附件</div> <div class="col-sm-2 control-label">附件</div>
<div class="col-sm-10"> <div class="col-sm-10">
<el-upload <el-upload class="upload-demo"
class="upload-demo"
action="../jk/jgsubject/uploadfile" action="../jk/jgsubject/uploadfile"
:on-exceed="handleExceed" :on-exceed="handleExceed"
:on-preview="handlePreview" :on-preview="handlePreview"
...@@ -195,8 +194,6 @@ ...@@ -195,8 +194,6 @@
<el-button size="small" type="primary">点击上传</el-button> <el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传10个文件,且单个文件不超过10M</div> <div slot="tip" class="el-upload__tip">只能上传10个文件,且单个文件不超过10M</div>
</el-upload> </el-upload>
<!--<input type="text" class="form-control" v-model="jgSubject.file" style="display:none"/>
<div id="file" class="dropzone" style="border: 1px solid #ccc;width:300px;height:30px;float: left;"></div>-->
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
......
...@@ -80,13 +80,16 @@ $(function () { ...@@ -80,13 +80,16 @@ $(function () {
this.on("addedfile", function(file) { this.on("addedfile", function(file) {
}); });
this.on("success", function(file) { this.on("success", function(file,data) {
photo=file.name; console.log(data)
console.log(file)
photo=data.filename;
isUsedPhotoOrFile=true; isUsedPhotoOrFile=true;
}); });
this.on("removedfile", function(file) { this.on("removedfile", function(file) {
var fileName = file.name; photo=null;
remove(fileName) //var fileName = file.name;
//remove(fileName)
}); });
} }
}); });
...@@ -107,8 +110,8 @@ $(function () { ...@@ -107,8 +110,8 @@ $(function () {
isUsedPhotoOrFile=true; isUsedPhotoOrFile=true;
}); });
this.on("removedfile", function(file) { this.on("removedfile", function(file) {
var fileName = file.name; //var fileName = file.name;
remove(fileName) //remove(fileName)
}); });
} }
}); });
...@@ -210,6 +213,7 @@ var vm = new Vue({ ...@@ -210,6 +213,7 @@ var vm = new Vue({
vm.reload(); vm.reload();
}, },
add: function(){ add: function(){
vm.fileList = [];
isUsedPhotoOrFile=false; isUsedPhotoOrFile=false;
vm.showList = false; vm.showList = false;
vm.title = "新增"; vm.title = "新增";
...@@ -355,6 +359,7 @@ var vm = new Vue({ ...@@ -355,6 +359,7 @@ var vm = new Vue({
}); });
}, },
reload: function (event) { reload: function (event) {
vm.fileList = [];
if(isUsedPhotoOrFile){ if(isUsedPhotoOrFile){
window.location.reload(); window.location.reload();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment