Sample Code for uploading and viewing SpringMVC files, and uploading springmvc files

Source: Internet
Author: User

Sample Code for uploading and viewing SpringMVC files, and uploading springmvc files

Preface

When talking about file upload, we should first talk about the business logic. If all uploaded files can be viewed (such as advertisements or banner on the homepage), we will put the images in the static resource area (with css, in js), if the file is protected (for example, users can only view their uploaded photos), then we store it in a specific image storage location on the server.

This example shows how to upload files stored in two locations. As an extension, you can also view uploaded files and download uploaded files.

Preparations

Configure SpringMVC and import commons package

Configure File Upload parser in mvc-servlet.xml

<! -- File Upload parser --> <bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. commonsMultipartResolver "> <property name =" maxUploadSize "value =" 1000000 "/> <property name =" defaultEncoding "value =" UTF-8 "/> </bean>

Stored in the static resource area

1. storage location:

Stored in the project, so the path is relative to the project path.

/{Yourproject}/webapp/static/img

2. Configure the response handler

@ Controllerpublic class UploadController {@ GetMapping ("/upload") public String UploadHandler () {return "upload" ;}@ PostMapping ("/upload/static ") public void wriToStatic (HttpServletRequest request, RedirectAttributes redirectAttributes, @ RequestParam ("fileName") MultipartFile file) {if (! File. isEmpty () {// obtain the target folder String path = request. getServletContext (). getRealPath ("/") + "static/img/"; // obtain the user-uploaded source file name String fileName = file. getOriginalFileName (); // create a File file1 = new file (path, fileName); // write the File to File. transferTo (file1); redirectAttributes. addFlashAttribute ("message", "upload to static success"); return "redirect:/upload";} else {redirectAttributes. addFlashAttribute ("message", "upload file can not be empty"); return "redirect:/upload ";}}}

Stored on server

1. storage location of this example:

It is stored in a location on the server and has nothing to do with the Project. Therefore, the address is an absolute path.

/Users/mac/Desktop/imgtemp/is the absolute directory path.

2. Configure the response handler

... @ PostMapping ("/upload/disk") public String writeToDisk (HttpServletRequest request, @ RequestParam ("fileName") MultipartFile file, RedirectAttributes redirectAttributes) {if (! File. isEmpty () {// obtain the source file name String fileName = file. getOriginalFileName (); // get the path to the folder where the files are saved: String path = "/Users/mac/Desktop/imgtemp/"; // create a File file1 = new File (path, fileName); // write the file. transferTo (file1 );}}...

Extended Part (viewing and downloading files)

Because the response is to pass files in the form of a stream, We need to correctly set the MIMIE type of the response to be correctly parsed by the browser, the default MIMIE type of application files is application/octet-stream. After MIME is set to this value, the browser does not automatically execute or ask to execute such files, the file is directly downloaded to the local device as an attachment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.