实现多文件上传,按钮vue传值(vue2.1特性)

This commit is contained in:
F嘉阳
2018-02-04 21:26:04 +08:00
parent c1720842e1
commit cd17163b67
9 changed files with 88 additions and 59 deletions

View File

@@ -21,10 +21,10 @@ public class DownLoadController {
@Autowired
private FileService fileService;
@GetMapping("/download")
/*@GetMapping("/download")
public String toDownloadPage(){
return "download";
}
return "download/dodownload";
}*/
@GetMapping("/download/findall")
@ResponseBody
@@ -37,7 +37,7 @@ public class DownLoadController {
}
@RequestMapping("/download/dodownload")
public String download(@RequestParam String fileName , HttpServletRequest request, HttpServletResponse response){
public String download(@RequestParam Integer fileId , HttpServletRequest request, HttpServletResponse response){
response.setContentType("text/html;charset=utf-8");
try {
@@ -49,19 +49,20 @@ public class DownLoadController {
java.io.BufferedOutputStream bos = null;
TbFile file = new TbFile();
file.setColfilename(fileName);
TbFile tbFile = fileService.findFile(file);
file.setColfileid(fileId);
TbFile tbFile = fileService.findFileById(file);
//TbFile tbFile = fileService.findFile(file);
System.out.println(tbFile.getColfilepath());
String ctxPath = tbFile.getColfilepath();
String downLoadPath = ctxPath + fileName;
String downLoadPath = ctxPath;
//String downLoadPath = ctxPath + tbFile.getColfilename();
System.out.println(downLoadPath);
try {
long fileLength = new File(downLoadPath).length();
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-disposition", "attachment; filename=" + new String(tbFile.getColfilename().getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());

View File

@@ -14,4 +14,9 @@ public class NavController {
public String toTestPage(){
return "/dist/thymeleafTest";
}
@GetMapping(value = {"axiosTest"})
public String toTestAxiosPage(){
return "/dist/axiosTest";
}
}

View File

@@ -60,7 +60,8 @@ public class UpLoadController {
@RequestMapping(value = "/oneUpload")
public String oneUpload(@RequestParam("imageFile") MultipartFile imageFile, HttpServletRequest request) {
String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/";
//String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/";
String uploadUrl = serverProperties.getFilePath()+ "upload/";
String filename = imageFile.getOriginalFilename();
File dir = new File(uploadUrl);
if (!dir.exists()) {//判断目录是否存在,否则自动创建
@@ -116,13 +117,13 @@ public class UpLoadController {
* @return
*/
@RequestMapping("/moreUpload")
public String moreUpload(HttpServletRequest request) {
public void moreUpload(HttpServletRequest request) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> files = multipartHttpServletRequest.getFileMap();
String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/";
//String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/";
String uploadUrl = serverProperties.getFilePath()+ "upload/";
File dir = new File(uploadUrl);
if (!dir.exists()) {
@@ -132,13 +133,17 @@ public class UpLoadController {
List<String> fileList = new ArrayList<String>();
for (MultipartFile file : files.values()) {
File targetFile = new File(uploadUrl + file.getOriginalFilename());
String filename = file.getOriginalFilename();
File targetFile = new File(uploadUrl + filename);
System.out.println("文件上传到: " + uploadUrl + filename);
System.out.println("文件大小: " + new FormatFileSizeUtil().GetFileSize(file.getSize()));
System.out.println("文件名: " + filename);
TbFile tbFile = new TbFile();
tbFile.setColfilesize(new FormatFileSizeUtil().GetFileSize(file.getSize()));
tbFile.setColfilename(file.getName());
tbFile.setColfilepath(uploadUrl + file.getName());
tbFile.setColfilename(filename);
tbFile.setColfilepath(uploadUrl + filename);
tbFile.setColip(request.getRemoteAddr());
if (fileService.addFile(tbFile))
@@ -167,9 +172,5 @@ public class UpLoadController {
}
}
request.setAttribute("files", fileList);
return "moreUploadResult";
}
}

View File

@@ -7,6 +7,15 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "serverproperties")
public class ServerProperties {
private String portNum;
private String filePath;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getPortNum() {
return portNum;

View File

@@ -30,6 +30,10 @@ public class FileService {
return tbFileRepository.findAll();
}
public TbFile findFileById(TbFile tbFile){
return tbFileRepository.findById(tbFile.getColfileid()).get();
}
}