1. Realize file upload
1.1 Assign values to attributes
1.1.1 Edit pro configuration file
1.1.2 Assign values to attributes
1.1.3 Refactoring FileService
package com.jt.service;
import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.datatransfer.FlavorEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Service
@PropertySource(value = "classpath:/properties/image.properties",
encoding = "UTF-8") //将配置文件导入容器
public class FileServiceImpl implements FileService{
@Value("${image.fileDir}")
private String fileDir; // = "D:/JT-SOFT/images";
@Value("${image.urlPath}")
private String urlPath; //设定域名地址
private static Set<String> typeSet = new HashSet<>();
static {
//利用配置文件动态将图片类型获取.
typeSet.add(".jpg");
typeSet.add(".png");
typeSet.add(".gif");
}
/**
* 业务逻辑:实现文件上传
* 步骤:
* 1.校验图片的类型 jpg|png|gif.......
* 2.校验文件是否为恶意程序...
* 3.采用分目录的结构进行存储
* 4.避免文件重名 UUID
*
* 考题: UUID有多少种排列组合? 32位16进制数.... 0-9 A-F 2^4*32 2^128 hash碰撞
* hash取值有多少种可能性? 8位16进制数 2^32
*
* @param uploadFile
* @return
*/
@Override
public ImageVO upload(MultipartFile uploadFile){
//一. 校验图片类型 1.利用集合校验 2.正则表达式
//1.1 获取文件名称 1.jpg 1.JPG
String fileName = uploadFile.getOriginalFilename();
fileName = fileName.toLowerCase();
int index = fileName.lastIndexOf(".");
//1.2 获取下标 获取文件后缀类型
String fileType = fileName.substring(index);
//1.3 判断是否为图片类型
if(!typeSet.contains(fileType)){
return ImageVO.fail();
}
//二.如果是图片 高度/宽度
//2.1将数据转化为图片对象
try {
BufferedImage bufferedImage =
ImageIO.read(uploadFile.getInputStream());
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if(width == 0 || height == 0){
return ImageVO.fail();
}
//三 实现分目录存储
//3.1按照/yyyy/MM/dd/的方式进行目录划分
String dateDir = new SimpleDateFormat("/yyyy/MM/dd/")
.format(new Date());
//D:/JT-SOFT/images/2020/12/1/
String fileDirPath = fileDir + dateDir;
File dirFile = new File(fileDirPath);
//3.2 创建目录
if(!dirFile.exists()){
dirFile.mkdirs();
}
//四. 实现文件上传 20-18-3.3
//4.1 准备文件名称 UUID 522f88bb-33b2-11eb-93fb-00d861eaf238
String uuid = UUID.randomUUID()
.toString().replace("-", "");
//4.2 动态生成文件名称 uuid.jpg
String uuidName = uuid + fileType;
//4.3 实现文件上传 D:\JT-SOFT\images\2020\12\02\a.jpg
File realFile = new File(fileDirPath+uuidName);
uploadFile.transferTo(realFile);
//本地磁盘地址: D:\JT-SOFT\images\2020\12\02\a.jpg
//网络访问虚拟地址: http://image.jt.com\2020\12\02\a.jpg
String url = urlPath + dateDir + uuidName;
return ImageVO.success(url, width,height);
} catch (IOException e) {
e.printStackTrace();
return ImageVO.fail(); //报错返回
}
}
}
1.1.4 Code Test
1. The path of the file upload after uploading the picture.
2. Modify the requested prefix to the specific disk address.
Switch the prefix and check whether the file is normal
2. Reverse proxy
2.1 Reverse proxy concept
The reverse proxy server is located between the user and the target server, but for the user, the reverse proxy server is equivalent to the target server, that is, the user can directly access the reverse proxy server to obtain the resources of the target server. At the same time, the user does not need to know the address of the target server, nor does it need to make any settings on the user side. The reverse proxy server is usually used as a Web acceleration, that is, the reverse proxy is used as the front end of the Web server to reduce the load of the network and the server, and improve the access efficiency. [1]
Features:
1. The reverse proxy server is located between the user and the target server.
2. The user thinks that the reverse proxy server is the real server. The user does not know who the real server is.
3. The reverse proxy server protects the server Information is called a server-side proxy.
Reasons for the existence of the proxy: For some reason, the user cannot directly access the target server to complete the specified function.
2.2 Forward proxy
2.2.1 Introduction to Forward Agent
Forward proxy means a server located between the client and the origin server. In order to obtain content from the origin server, the client sends a request to the proxy and specifies the target (origin server), and then the proxy forwards it to the origin server Request and return the obtained content to the client. The client can use the forward proxy.
Features:
1. The proxy server is located between the user and the server.
2. When the user requests, it is very clear who the target server is. The server does not know who is accessing me. I thought it was a request directly initiated by the proxy server.
3. Forward proxy The server protects the user's information, so it is called a client proxy.
2.2.2 About the agent summary
1. The reverse proxy is a server-side proxy. As long as the user accesses the server, it is actually a reverse proxy mechanism. Realize business calls
2. The forward proxy is a client proxy. The main user uses the forward proxy to achieve network communication when going online. Head Net Wear Tool Night God Simulator/Peanut Shell
2.3 Nginx
2.3.1 Introduction to Nginx Server
2.3.2 Introduction to nginx
Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server, released under the BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx's concurrency capabilities perform better in the same type of web server. Mainland Chinese users of nginx websites include: Baidu, JD, Sina, NetEase, Tencent, Taobao, etc.
Features:
1. Take up less memory 2M tomcat starts about 200M
2. Strong concurrency capacity is 50,000/sec, actual 20,000-40,000/sec
2.3.3 Nginx installation and use
1). Start Nginx nginx will generate 2 process items
1. The main function of the main process is to provide reverse proxy services. When closing the main process with large memory
2. The daemon prevents the main process from accidentally shutting down. Turn off the daemon first
2.3.4 Nginx commands
Working directory description: It is required to execute in the root directory where nginx.exe is located
1. Start the command
start nginx Linux ./nginx
2. Rename the command
nginx -s reload Linux: ./nginx -s reload
3. Close the command
nginx -s stop Linux: ./nginx -s stop
2.3.5 Nginx reverse proxy principle
Introductory case description: http://localhost:80
2).Configuration items
http{
server { #服务1
listen 80;
server_name localhost;
location / {
#root html;
root D:/jt_images;
index index.html;
}
}
server { #服务1
listen 80;
server_name xxxxxx; #服务名称不能重复
location / {
#root html;
root D:/jt_images;
index index.html;
}
}
}
2.4 realize picture echo
2.4.1 Requirements
Realize the proxy of the image address and redirect the image address to the specific disk path.
URL address: http://image.jt.com/2020/12/02/7d7179100d1e423abc2546e77743947c.png
Local disk address: D:\JT-SOFT\images /2020/12/02/7d7179100d1e423abc2546e77743947c.png
2.4.2 Configure nginx
2.4.3 Principle of picture echo
2.4.4 Edit the HOSTS file
Function: Realize the mapping between local domain name and IP address
Path:
2.4.5 Modify HOSTS file
2.6 Realizing domain name agency
2.6.1 Requirements description
Requirements: Users are required to access the server at localhost:8091 through http://manage.jt.com.
Implementation method: Use reverse proxy mechanism
2.6.2 Configure nginx
#2.配置后端服务器 manage.jt.com:80 localhost:8091
server {
listen 80;
server_name manage.jt.com;
location / {
#发起url请求地址
proxy_pass http://localhost:8091;
}
}
2.7 Nginx implements tomcat cluster deployment
2.7.1 Principles of Cluster Construction
2.7.2 Dynamic display port number
2.7.3 Project packaging
Note: Because you need to prepare 3 tomcat servers. So the port number is 8081/8082/8083
3). Upload war package
4). Project operation
java -jar 8081.war
2.7 Nginx load balancing
2.7.1 Polling Strategy
Note: According to the order of the configuration files, access the server one by one.
2.7.2 Weighting Strategy
Description: Let the server with better performance handle more user requests.
2.7.3 IPHASH Strategy
Requirements: Need to bind the user to a server.
Principle:
Disadvantages:
1. It is easy to cause uneven load.
2. If the IP address is bound to the user, if the tomcat server goes down, it will directly affect the user.
IPhash practical scenarios: generally used for stress testing.
2.8 Nginx advanced properties
2.8.1 down attribute
Note: If the server is down, it can be identified by the down attribute, and the identified server will no longer provide support for users.
2.8.2 backup attribute
The setting of the standby machine. Under normal conditions, the standby machine is not working, but when the main machine is busy or the main machine is down, the standby machine will be accessed.
2.8.3 tomcat server high availability
Note: If the efficiency of manually adding the down attribute is not high, can it automatically detect whether the server is down? If it is down, can it be automatically marked as down.
3 homework
Install the remote connection tool
url address: https://mobaxterm.mobatek.net/download-home-edition.html
Realize the dynamic effect of remote link