温红权 9 лет назад
Родитель
Сommit
4e4e653f12

+ 24 - 0
VisualInspection/dist/view/login.html

@@ -0,0 +1,24 @@
+<!doctype html>
+<html>
+
+<head>
+    <meta charset="utf-8">
+    <title>登陆</title>
+
+    <link rel="stylesheet" type="text/css" href="/node_modules/zui/dist/css/zui.min_02a83cc.css">
+</head>
+
+<body>
+    <div class="container-fluid ">
+
+        <nav class="navbar navbar-inverse" role="navigation">
+
+        </nav>
+
+    </div>
+    <script src="/node_modules/zui/dist/lib/jquery/jquery_78bc357.js"></script>
+    <script src="/node_modules/zui/dist/js/zui.min_604098c.js"></script>
+</body>
+
+
+</html>

+ 38 - 0
VisualInspection/view/login.html

@@ -0,0 +1,38 @@
+<!doctype html>
+<html>
+
+<head>
+    <meta charset="utf-8">
+    <title>登陆</title>
+
+    <link rel="stylesheet" type="text/css" href="/node_modules/zui/dist/css/zui.min.css">
+</head>
+
+<body>
+
+    <div class="navbar-header navbar-inverse">
+        <div class="container-fluid">
+
+            <ul class="nav navbar-nav navbar-right">
+                <li><a href="your/nice/url">帮助</a></li>
+            </ul>
+
+
+        </div>
+
+        <div class="collapse navbar-collapse navbar-collapse-example">
+
+            <!-- 右侧的导航项目 -->
+            <ul class="nav navbar-nav navbar-right">
+                <li><a href="your/nice/url">帮助</a></li>
+            </ul>
+        </div>
+    </div>
+
+
+    <script src="/node_modules/zui/dist/lib/jquery/jquery.js"></script>
+    <script src="/node_modules/zui/dist/js/zui.min.js"></script>
+</body>
+
+
+</html>

+ 1 - 0
VisualInspection_server/src/main/java/com/xintong/visualinspection/controller/BaseController.java

@@ -137,4 +137,5 @@ public class BaseController {
        	}
        	return sb.toString();
     }
+
 }

+ 162 - 0
VisualInspection_server/src/main/java/com/xintong/visualinspection/controller/FileController.java

@@ -0,0 +1,162 @@
+package com.xintong.visualinspection.controller;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+
+@RestController
+@RequestMapping(value = "file")
+public class FileController extends BaseController {
+
+	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(FileController.class);
+
+	@Value("${storage.file.path}")
+	private String path;
+
+
+	private void initfile(){
+		File file = new File(path);
+		if (!file.exists()) {
+			file.mkdirs();
+		}
+	}
+	
+	@RequestMapping(value = "upload")
+	public void UploadFile(HttpServletRequest req, MultipartHttpServletRequest multiReq) {
+		initfile();
+		// 获取上传文件的路径
+		String uploadFilePath = multiReq.getFile("file1").getOriginalFilename();
+		System.out.println("uploadFlePath:" + uploadFilePath);
+		// 截取上传文件的文件名
+		String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1,
+				uploadFilePath.indexOf('.'));
+		System.out.println("multiReq.getFile()" + uploadFileName);
+		// 截取上传文件的后缀
+		String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
+		System.out.println("uploadFileSuffix:" + uploadFileSuffix);
+		FileOutputStream fos = null;
+		FileInputStream fis = null;
+		try {
+			fis = (FileInputStream) multiReq.getFile("file1").getInputStream();
+			fos = new FileOutputStream(new File(path + uploadFileName + ".") + uploadFileSuffix);
+			byte[] temp = new byte[1024];
+			int i = fis.read(temp);
+			while (i != -1) {
+				fos.write(temp, 0, temp.length);
+				fos.flush();
+				i = fis.read(temp);
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			if (fis != null) {
+				try {
+					fis.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+			if (fos != null) {
+				try {
+					fos.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+	}
+
+	@RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
+	@ResponseBody
+	public void handleFileUpload(HttpServletRequest request) {
+		initfile();
+		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
+		MultipartFile file = null;
+		BufferedOutputStream stream = null;
+		for (int i = 0; i < files.size(); ++i) {
+			file = files.get(i);
+			if (!file.isEmpty()) {
+				try {
+					String uploadFilePath = file.getOriginalFilename();
+					System.out.println("uploadFlePath:" + uploadFilePath);
+					// 截取上传文件的文件名
+					String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1,
+							uploadFilePath.indexOf('.'));
+					System.out.println("multiReq.getFile()" + uploadFileName);
+					// 截取上传文件的后缀
+					String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1,
+							uploadFilePath.length());
+					System.out.println("uploadFileSuffix:" + uploadFileSuffix);
+					stream = new BufferedOutputStream(
+							new FileOutputStream(new File(path + uploadFileName + "." + uploadFileSuffix)));
+					byte[] bytes = file.getBytes();
+					stream.write(bytes, 0, bytes.length);
+				} catch (Exception e) {
+					e.printStackTrace();
+				} finally {
+					try {
+						if (stream != null) {
+							stream.close();
+						}
+					} catch (IOException e) {
+						e.printStackTrace();
+					}
+				}
+			} else {
+				System.out.println("上传文件为空");
+			}
+		}
+		System.out.println("文件接受成功了");
+	}
+
+	@RequestMapping(value = "/download/{filename:[a-zA-Z0-9.]+}", method = RequestMethod.GET)
+	public void Download(HttpServletResponse res, @PathVariable String filename) {
+		String fileName = filename;
+		res.setHeader("content-type", "application/octet-stream");
+		res.setContentType("application/octet-stream");
+		res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
+		byte[] buff = new byte[1024];
+		BufferedInputStream bis = null;
+		OutputStream os = null;
+		try {
+			os = res.getOutputStream();
+			bis = new BufferedInputStream(new FileInputStream(new File(path + fileName)));
+			int i = bis.read(buff);
+			while (i != -1) {
+				os.write(buff, 0, buff.length);
+				os.flush();
+				i = bis.read(buff);
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			if (bis != null) {
+				try {
+					bis.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		System.out.println("success");
+	}
+
+}

+ 1 - 1
VisualInspection_server/src/main/java/com/xintong/visualinspection/securityTools/WebSecurityConfig.java

@@ -65,7 +65,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                  "/**/*.js"
          ).permitAll()
          // 对于获取token的rest api要允许匿名访问
-         .antMatchers("/user/auth/**").permitAll()
+         .antMatchers("/user/auth/**","/file/**").permitAll()
          // 除上面外的所有请求全部需要鉴权认证
          .anyRequest().authenticated();
     	

+ 6 - 0
VisualInspection_server/src/main/resources/application.properties

@@ -87,6 +87,12 @@ jwt.expiration=604800
 jwt.tokenHead=XinTong 
 
 
+
+
+# File path
+storage.file.path=/Users/wenhongquan/Desktop/file/
+
+
 management.shell.auth.simple.user.name=wen
 management.shell.auth.simple.user.password=wen
 management.shell.auth.type=simple

+ 16 - 47
VisualInspection_server/src/main/resources/templates/login.html

@@ -1,52 +1,21 @@
-<!DOCTYPE html>
-<html xmlns:th="http://www.thymeleaf.org">
+<html>
 <head>
-    <meta content="text/html;charset=UTF-8"/>
-    <title>登录页面</title>
-    <link rel="stylesheet" th:href="@{css/bootstrap.min.css}"/>
-    <style type="text/css">
-        body {
-            padding-top: 50px;
-        }
-        .starter-template {
-            padding: 40px 15px;
-            text-align: center;
-        }
-    </style>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+<title>Insert title here</title>
 </head>
 <body>
-
-<nav class="navbar navbar-inverse navbar-fixed-top">
-    <div class="container">
-        <div class="navbar-header">
-            <a class="navbar-brand" href="#">Spring Security演示</a>
-        </div>
-        <div id="navbar" class="collapse navbar-collapse">
-            <ul class="nav navbar-nav">
-                <li><a th:href="@{/}"> 首页 </a></li>
-
-            </ul>
-        </div><!--/.nav-collapse -->
-    </div>
-</nav>
-<div class="container">
-
-    <div class="starter-template">
-        <p th:if="${param.logout}" class="bg-warning">已成功注销</p><!-- 1 -->
-        <p th:if="${param.error}" class="bg-danger">有错误,请重试</p> <!-- 2 -->
-        <h2>使用账号密码登录</h2>
-        <form name="form" th:action="@{/login}" action="/login" method="POST"> <!-- 3 -->
-            <div class="form-group">
-                <label for="username">账号</label>
-                <input type="text" class="form-control" name="username" value="" placeholder="账号" />
-            </div>
-            <div class="form-group">
-                <label for="password">密码</label>
-                <input type="password" class="form-control" name="password" placeholder="密码" />
-            </div>
-            <input type="submit" id="login" value="Login" class="btn btn-primary" />
-        </form>
-    </div>
-</div>
+<form action="http://localhost:8089/file/upload" method="POST" enctype="multipart/form-data">
+    <p>单文件上传:</p><br/>
+    <input type="file" name="file1"/>
+    <input type="submit" value = "上传"/>
+</form>
+<form method="POST" enctype="multipart/form-data" 
+    action="http://localhost:8089/file/batch/upload">
+    <p>多文件上传:</p>
+    <p>文件1:<input type="file" name="file" /></p>
+    <p>文件2:<input type="file" name="file" /></p>
+    <p><input type="submit" value="上传" /></p>
+</form>
+<a href="http://localhost:8089/file/download/5.jpg">下载</a>
 </body>
 </html>

+ 52 - 0
VisualInspection_server/src/main/resources/templates/login1.html

@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta content="text/html;charset=UTF-8"/>
+    <title>登录页面</title>
+    <link rel="stylesheet" th:href="@{css/bootstrap.min.css}"/>
+    <style type="text/css">
+        body {
+            padding-top: 50px;
+        }
+        .starter-template {
+            padding: 40px 15px;
+            text-align: center;
+        }
+    </style>
+</head>
+<body>
+
+<nav class="navbar navbar-inverse navbar-fixed-top">
+    <div class="container">
+        <div class="navbar-header">
+            <a class="navbar-brand" href="#">Spring Security演示</a>
+        </div>
+        <div id="navbar" class="collapse navbar-collapse">
+            <ul class="nav navbar-nav">
+                <li><a th:href="@{/}"> 首页 </a></li>
+
+            </ul>
+        </div><!--/.nav-collapse -->
+    </div>
+</nav>
+<div class="container">
+
+    <div class="starter-template">
+        <p th:if="${param.logout}" class="bg-warning">已成功注销</p><!-- 1 -->
+        <p th:if="${param.error}" class="bg-danger">有错误,请重试</p> <!-- 2 -->
+        <h2>使用账号密码登录</h2>
+        <form name="form" th:action="@{/login}" action="/login" method="POST"> <!-- 3 -->
+            <div class="form-group">
+                <label for="username">账号</label>
+                <input type="text" class="form-control" name="username" value="" placeholder="账号" />
+            </div>
+            <div class="form-group">
+                <label for="password">密码</label>
+                <input type="password" class="form-control" name="password" placeholder="密码" />
+            </div>
+            <input type="submit" id="login" value="Login" class="btn btn-primary" />
+        </form>
+    </div>
+</div>
+</body>
+</html>