search
Homephp教程PHP开发SpringMVC implements image upload

SpringMVC implements image upload

Nov 23, 2016 pm 03:05 PM
springmvc

We implement file upload. If we use byte stream, the code volume is large and the efficiency is low, so springMVC provides us with our own method.

SpringMVC specifically provides the CommonMultipartResolver component to implement file upload:

maxUploadSize Maximum file limit, unit is byte

maxInMemorySize Files smaller than this size are temporarily stored in memory

defaultEncoding Default encoding

For example, configured like this:

 

 

 

the above In the configuration, the id must be multipartResolver, so that uploading is guaranteed. You can't name it arbitrarily

SpringMVC implements image upload

This is why the beans must have the same id to work properly.

At this time we start to configure the form form. We must add enctype="multipart/form-data":

SpringMVC implements image upload

Then remember to introduce two jar packages:

commons-fileupload.jar

commens -io-1.4.jar

Then we start writing java code:

1. First change the request into MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

2. Then we can use getFile to get the file:

CommonsMultipartFile file = (CommonsMultipartFile)multipartRequest.getFile("file");

This file must be consistent with the value of the name entered in the form.

3. Finally, move the file to the target address:

FileCopyUtil.cope(file.getByte(),uploadFile);

The above is a single file upload. For multiple file uploads, you can use getFileMap() of MultipartHttpServletRequest. Get all the files passed by the form

Then use a for loop to traverse and upload the files in sequence:

SpringMVC implements image upload

After talking about file uploading, let’s talk about how to download:

File downloading mainly uses the form of byte stream, there are three key points:

1. Set the encoding format to: text/html; charset=utf-8

2. Set the Content-disposition attribute value in the header to attachment; filename = file name (this file name is the name of the client pop-up box file)

3. Set the Context-Length attribute in the header, the value is the size of the file

The above is a small example.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)