提交方式必須是post、post、post
最後 附有網盤鏈接(程序打包+數據庫)
spring文件上傳
1、jar包
commons-io-2.5.jar
commons-fileupload-1.3.1.jar
全是Apache的
pom的dependency
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
2、要點
1)提交方式必須是post
2)from表單添加屬性
“multipart/form-data”
3)在配置文件bean中定義multipartResolver
4)在handler中用MultipartFile接收用戶上傳文件
3、前端的表單
文件名:/springmvc/src/main/webapp/views/fileupload/fileupload.jsp
注意post的提交方式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--
文件上傳:
1、提交方法必須爲post
2、在form標籤中添加屬性:enctype="multipart/form-data"
-->
<form action="${pageContext.request.contextPath}/fileUpload" enctype="multipart/form-data" method="post">
<input type="text" name="userName"><br>
<input type="file" name="avatar"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
4、bean定義
代碼33行是核心
文件名:/springmvc/src/main/resources/config/applicationContext-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<context:component-scan base-package="init.wuji.springboot.mvc.*.action"></context:component-scan>
<!--
配置視圖解析器
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上傳文件的最大值, 單位爲:字節 50*1024*1024
-->
<property name="maxUploadSize" value="#{50*1024*1024}" />
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
5、handler
文件名:/springmvc/src/main/java/init/wuji/springboot/mvc/fileupload/action/FileUploadAction.java
package init.wuji.springboot.mvc.fileupload.action;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadAction {
// public void doPost(HttpServletRequest request, HttpServletResponse response) {
// DiskFileItemFactory dfif = new DiskFileItemFactory();
//
// ServletFileUpload sfu = new ServletFileUpload(dfif);
//
// try {
// List<FileItem> fiList = sfu.parseRequest(request);
// } catch (FileUploadException e) {
// e.printStackTrace();
// }
// }
@RequestMapping("/fileUploadPage")
public String fileUploadPage() {
return "fileupload/fileupload";
}
/**
* 文件上傳總結:
* 1、jsp
* 1>、提交方法必須爲post
2>、在form標籤中添加屬性:enctype="multipart/form-data"
*
* 2、在mvc的handler方法中定義MultipartFile類型來接收用戶上傳的文件
*
* 3、在應用上下文中配置Multipart解析器
*
*
* @param avatar
* @param userName
* @return
*/
@RequestMapping("/fileUpload")
public String fileUpload(MultipartFile avatar, String userName) {
//判斷是否爲空
if(avatar.isEmpty()) {
return "error";
}
//獲取源文件名稱
String orginName = avatar.getOriginalFilename();
String name = avatar.getName();
System.out.println("===================>" + name);
String destFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(orginName);
File destFile = new File("D:\\DATA\\IMG\\" + destFileName);
try {
avatar.transferTo(destFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}
總結你把我上面的都做了,可以解決圖片上傳問題,下課
鏈接
鏈接:https://pan.baidu.com/s/1_8Hs6V06GdX7ia5fYV4MlQ
提取碼:7s8u