SpringMVC implements file upload and download




Insert image description here

SpringMVC is a Java-based web framework that provides convenient file upload and download functions. The following is a brief description of its implementation principle:

  1. File Upload:

The client selects and submits files to the server through the form (HTML tag).
After the server receives the request, SpringMVC will encapsulate the file content in the form of byte stream into a MultipartFile object.
SpringMVC uses the MultipartResolver component to parse the request and save the file in a temporary directory on the server.
The controller method uses the @RequestParam annotation or directly declares the MultipartFile parameter to receive the uploaded file.
After receiving the file, the controller method can process it, such as saving it to the database or transferring it to a specified directory.


  1. Download Document:

The client sends a download request, such as by clicking a link or submitting a form.
After the server receives the download request, it processes the request through the controller method.
The controller method uses the HttpServletResponse object to set the content type and header information of the response, specifying it as a file download.
The controller method uses OutputStream to write the file contents to the HttpServletResponse's output stream.
After the client receives the response, it displays a download dialog box and allows the user to choose the location to save the file.
To sum up, the principle of file upload is to submit the file to the server through the form, SpringMVC encapsulates it into a MultipartFile object, and then processes it; the principle of file download is to write the file content to the output of HttpServletResponse Stream, allowing the client to receive and save the file. SpringMVC provides convenient components and annotations to simplify file upload and download operations.

1. File download

Use ResponseEntity to implement the function of downloading files

@RequestMapping("/testDown")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
    
    
    //获取ServletContext对象
    ServletContext servletContext = session.getServletContext();
    //获取服务器中文件的真实路径
    String realPath = servletContext.getRealPath("/static/img/1.jpg");
    //创建输入流
    InputStream is = new FileInputStream(realPath);
    //创建字节数组
    byte[] bytes = new byte[is.available()];
    //将流读到字节数组中
    is.read(bytes);
    //创建HttpHeaders对象设置响应头信息
    MultiValueMap<String, String> headers = new HttpHeaders();
    //设置要下载方式以及下载文件的名字
    headers.add("Content-Disposition", "attachment;filename=1.jpg");
    //设置响应状态码
    HttpStatus statusCode = HttpStatus.OK;
    //创建ResponseEntity对象
    ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
    //关闭输入流
    is.close();
    return responseEntity;
}

2. File upload

File upload requires that the request method of the form form must be post, and add the attribute enctype="multipart/form-data"
In SpringMVC, the uploaded file is encapsulated into the MultipartFile object. File-related information can be obtained through this object
Upload steps:
a>Add dependencies:

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

b>Add configuration in SpringMVC configuration file:

<!--必须通过文件解析器的解析才能将文件转换为MultipartFile对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

c>Controller method:

@RequestMapping("/testUp")
public String testUp(MultipartFile photo, HttpSession session) throws IOException {
    
    
    //获取上传的文件的文件名
    String fileName = photo.getOriginalFilename();
    //处理文件重名问题
    String hzName = fileName.substring(fileName.lastIndexOf("."));
    fileName = UUID.randomUUID().toString() + hzName;
    //获取服务器中photo目录的路径
    ServletContext servletContext = session.getServletContext();
    String photoPath = servletContext.getRealPath("photo");
    File file = new File(photoPath);
    if(!file.exists()){
    
    
        file.mkdir();
    }
    String finalPath = photoPath + File.separator + fileName;
    //实现上传功能
    photo.transferTo(new File(finalPath));
    return "success";
}

3. Good book recommendations

Insert image description here

Introduction to inclusion

This book comprehensively and systematically explains information security technology represented by face recognition, which can reduce user data information security risks. The main content of this book includes an overview of artificial intelligence and information security, key technologies for mining face identifiable information, solutions to problems such as unconstrained face recognition, small sample face recognition, cost-sensitive faces, fast regularized joint classification, etc., in-depth The construction scheme of local dictionary and joint weighted kernel sparse classifier, various schemes to improve the security of user information network, the future of user information network security, etc.

This book has clear organization, strong logic, substantial content, and wide coverage. It is highly academic and practical and can be used as a study and reference for the majority of artificial intelligence beginners and teachers and students in related majors.


Table of contents

Chapter 1 Overview of Artificial Intelligence and Information Security 1

1.1 User information network security technology based on face recognition 1

1.1.1 Introduction of face recognition technology 2

1.1.2 Development History of Face Recognition Technology 5

1.1.3 User information network security 12

1.2 Integration of machine learning with artificial intelligence, data mining and network security 16

1.2.1 Machine learning and artificial intelligence 16

1.2.2 Machine learning and data mining 17

1.2.3 Machine learning and network security 18

1.3 Machine learning and information security development trends 20

1.4 Summary of this chapter 22

Chapter 2 Methods of Mining User-Identifiable Information 23

2.1 Sparse representation 23

2.2 Collaborative representation 26

2.3 Kernel sparse representation and kernel collaborative representation 27

2.4 Sparse dictionary learning 29

2.5 Deep Learning 31

2.5.1 Deep learning model 31

2.5.2 Multi-layer perceptron 33

2.5.3 Activation function and loss function 35

2.5.4 Optimization algorithm 38

2.5.5 Convolutional Neural Network 40

2.6 Summary of this chapter 42

Chapter 3 Methods for identifying non-binding users 43

3.1 Unconstrained face recognition problem 43

3.2 Review of related work 46

3.2.1 Robust sparse representation 46

3.2.2 Robust sparse coding algorithm 47

3.3 Variable occlusion detection and iterative recovery sparse representation model 47

3.3.1 VOD process 49

3.3.2 IR process 50

3.3.3 VOD&IR algorithm description 50

3.4 Experimental results and analysis 52

3.4.1 Parameter settings 52

3.4.2 Simulating block occlusion 53

3.4.3 Real occlusion of AR face database 58

3.4.4 Variable occlusion map accuracy assessment 62

3.5 Summary of this chapter 63

Chapter 4 Identification Method of Small Sample Users 65

4.1 Small sample user identification problem 65

4.2 Sample group dislocated atom dictionary joint kernel cooperative representation classification model 67

4.2.1 Affine transformation principle 68

4.2.2 Sample group dislocated atom dictionary 69

4.2.3 Joint core collaborative representation model 70

4.3 Experimental results and analysis 71

4.3.1 Georgia Tech Face Database 72

4.3.2 Labeled Faces in the Wild face database 74

4.3.3 Caltech face database 75

4.3.4 Comparison of similar methods 77

4.3.5 Evaluation of Dislocated Atom Schemes for Sample Groups 78

4.4 Summary of this chapter 80

Chapter 5 Cost-Sensitive Face Authentication Security System 81

5.1 Cost-sensitive face recognition problem 81

5.2 Weighted binary dictionary based on Gaussian similarity relationship 83

5.2.1 Gaussian weighted sparse representation algorithm 83

5.2.2 Establishment of shallow global weighted double dictionary 84

5.3 Cost-sensitive face authentication model based on limited expression and action patterns 85

5.3.1 Principle of CSFV_LEP model 85

5.3.2 CSFV_LEP algorithm description 87

5.3.3 CSFV_LEP algorithm complexity analysis 89

5.4 Experimental results and analysis 89

5.4.1 Parameter settings 89

5.4.2 Security and practical performance analysis of the model 91

5.5 Summary of this chapter 99

Chapter 6 Manifold Regularization Method for Fast Face Recognition 101

6.1 Fast face recognition problem 101

6.2 Kernel collaborative manifold regularization model 103

6.3 Experimental simulation and result analysis 105

6.3.1 Parameter settings 105

6.3.2 Face recognition experiment on Extended Yale B face database 105

6.3.3 Face recognition experiment on AR face database 110

6.3.4 Face recognition experiment on FERET face database 112

6.3.5 Face recognition experiment on Lab2 face database 114

6.3.6 Impact of parameters 116

6.4 Summary of this chapter 119

Chapter 7 Hierarchical modeling large-scale face authentication method 121

7.1 Large-scale face recognition problem 121

7.2 Deep learning framework 123

7.2.1 Convolutional Neural Network 123

7.2.2 Classic convolutional neural network structure 124

7.2.3 Transfer learning 126

7.3 Creation of deep local dictionary 128

7.4 Joint weighted kernel collaborative representation 130

7.5 Some experimental results 131

7.5.1 Face recognition experiment on CMU-PIE face database 131

7.5.2 Face recognition experiment with noise and occlusion on CMU-PIE face database 132

7.5.3 Unobstructed face recognition experiment on LFW face database 133

7.5.4 Same-origin occlusion face recognition experiment on LFW face database 134

7.6 Summary of this chapter 135

Chapter 8 Methods to Improve User Information Network Security 137

8.1 Information security system with face recognition as the main body 137

8.2 Protection methods for user information network security 141

8.3 Suggestions for improving user information security in a big data environment 144

8.4 Summary of this chapter 148

Chapter 9 The future of user information network security 149

9.1 Diversification trend of user information resources 149

9.1.1 Diversity of user information resources 150

9.1.2 Differences in user information needs 155

9.2 Network information resources and their sharing and confidentiality 157

9.2.1 Network information resources 157

9.2.2 Sharing and confidentiality of network information resources 159

9.3 The development prospects of user information network security technology 161

9.4 Summary of this chapter 164

References 167

Appendix A Abbreviations 175

Purchase link https://item.jd.com/13621817.html


Insert image description here


Guess you like

Origin blog.csdn.net/m0_60915009/article/details/134631229