Spring MVC realizes file upload and copy

Triton 123 2022-05-22 11:37:26 阅读数:345

springmvcrealizesfileupload

zero 、 Import dependence

 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> Required for file upload jar package
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
springmvc Dependence
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.19</version>
</dependency>

One 、 A simple front-end page
Be careful : Upload file must be form Form enctype Property settings for : enctype=“multipart/form-data”

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/5/14
Time: 17:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> questionnaire </title>
</head>
<body>
<form action="form/food" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile">
<h1> What's for breakfast </h1>
<input name="breakfast"><br />
<h1> What's for lunch </h1>
<select name="lunch">
<option value="guan"> Oden </option>
<option value="ban"> Spicy mix </option>
<option value="kao"> Roast chicken leg </option>
</select>
<h2> Why eat it ?</h2>
<div>
<input type="checkbox" name="reason"> Spicy
<input type="checkbox" name="reason"> Marmalade !
<input type="checkbox" name="reason"> delicious !
</div>
<button type="submit"> Submit </button>
</form>
</body>
</html>

Two 、 Back end control layer

package com.xs.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @Author Administrator
* @Date 2022/5/14 17:14
*/
@RequestMapping("/form")
@Controller
public class FormController {
// @PostMapping("/food")
// public String foodMethod(String breakfast, String lunch){
// System.out.println(" Launched a new song :"+breakfast+" What's for lunch :"+lunch);
// return "/Today.jsp";
// }
private static final String BASE_PATH="G:\\Demo";
@PostMapping("/food")
public String foodMethod(String breakfast, String lunch, MultipartHttpServletRequest request) throws IOException {
System.out.println(" Launched a new song :"+breakfast+" What's for lunch :"+lunch);
// uploadFile Upload the front-end page file input The name of the box
MultipartFile uploadFile = request.getFile("uploadFile");
System.out.println(" file name :"+uploadFile.getName());
System.out.println(" file type :"+uploadFile.getContentType());
System.out.println(" Name of file :"+uploadFile.getOriginalFilename());
String orignalFilename = uploadFile.getOriginalFilename();
// The suffix of the cutting file
String suffix = orignalFilename.substring(orignalFilename.lastIndexOf("."));
// The name of the definition file
String fileName = " The avatars "+System.currentTimeMillis()+suffix;
// Write a file to another file in a native way
// InputStream inputStream = uploadFile.getInputStream();
// FileOutputStream fileOutputStream = new FileOutputStream(BASE_PATH + File.separator + fileName);
// int b = 0;
// while ((b=inputStream.read())!=-1){
// fileOutputStream.write(b);
// }
// Through the method provided by the framework , Just pass in a File Object can
uploadFile.transferTo(new File(BASE_PATH + File.separator + fileName));
return "/Today.jsp";
}
}

3、 ... and 、 The configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Turn on package scanning -->
<context:component-scan base-package="com.xs"></context:component-scan>
<!-- Turn on SpringMvc Annotation development model , Actually, it's with SpringMVc Registration of two important components in -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- Release static resources -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- Must be called multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- The maximum number of files in a request , Company byte-->
<property name="maxUploadSize" value="1000000"></property>
<!-- There may be more than one request stream at a time , The maximum value of a single file , The unit is also byte -1 No restrictions -->
<property name="maxUploadSizePerFile" value="2000000"></property>
</bean>
</beans>

 Insert picture description here
 Insert picture description here
 Insert picture description here

copyright:author[Triton 123],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211734381740.html