SpringMVC框架實(shí)現(xiàn)上傳圖片的示例代碼
一.創(chuàng)建圖片虛擬目錄
在上傳圖片之前,先要設(shè)置虛擬目錄(以IDEA為例)
- 打開工具欄的運(yùn)行配置Edit Configurations
- 添加物理目錄和并設(shè)置虛擬目錄路徑


添加img圖片在img文件夾內(nèi)

測試訪問:http://localhost:8080/img/img.jpg

二.SpringMVC上傳頭像
1.SpringMVC對多部件類型的解析
上傳圖片SpringMVC.xml配置
在頁面form中提交enctype="multipart/form-data"的數(shù)據(jù)時(shí),需要springmvc對multipart類型的數(shù)據(jù)進(jìn)行解析。在springmvc.xml中配置multipart類型解析器。
<!--文件上傳-->
<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
2.添加依賴
<!-- 文件上傳 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
3. 在Login1.jsp頁面form中提交enctype="multipart/form-data"的數(shù)據(jù)
<form action="/userController/insertUser" method="post" enctype="multipart/form-data">
<input type="text" required="required" placeholder="用戶名" name="userName">
<input type="password" required="required" placeholder="密碼" name="passWord">
<input type="file" name = "imgFile">
<div id="bt">
<input class="but" type="submit" value="注冊">
<a href="register.jsp" rel="external nofollow" ><input class="but" type="button" value="返回登錄"></a>
</div>
</form>

4.處理請求UserController.java
@RequestMapping("insertUser")
public String insertUser (HttpServletRequest request, User user, MultipartFile imgFile) throws IOException {
//獲取文件原始名稱
String originalFilename = imgFile.getOriginalFilename();
//上傳圖片
if(imgFile!=null && originalFilename!=null && originalFilename.length()>0){
//存儲圖片的物理路徑
String pic_path = "/home/ubuntu/IDEA/SSM/img/";
//新的圖片名稱
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//新圖片
File newFile = new File(pic_path+newFileName);
//將內(nèi)存中的數(shù)據(jù)寫入磁盤
imgFile.transferTo(newFile);
userService.insertUser(user,newFileName);
HttpSession session = request.getSession();
session.setAttribute("imgUrl", newFileName);
}
return "item/success";
}

上傳成功
成功跳轉(zhuǎn)頁面success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>成功頁面</h1>
<img style="width: 150px; height: 200px"
src="http://localhost:8080/img/<%=session.getAttribute("imgUrl")%>">
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot Maven打包如何根據(jù)環(huán)境排除文件
文章介紹了在SpringBoot項(xiàng)目中,根據(jù)不同的環(huán)境(開發(fā)、測試、生產(chǎn))進(jìn)行JSP文件打包處理的方法,通過配置`pom.xml`文件中的``標(biāo)簽,可以實(shí)現(xiàn)開發(fā)環(huán)境保留`index.jsp`文件,測試環(huán)境和生產(chǎn)環(huán)境排除該文件2024-12-12
java swagger ui 添加header請求頭參數(shù)的方法
今天小編就為大家分享一篇java swagger ui 添加header請求頭參數(shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
SpringBoot接收請求參數(shù)的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于SpringBoot接收請求參數(shù)的四種方式,文中通過代碼以及圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SpringBoot具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題
這篇文章主要介紹了完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用
Java圖形驗(yàn)證碼,支持gif、中文、算術(shù)等類型,可用于Java Web、JavaSE等項(xiàng)目,下面就跟隨小編一起來了解一下2021-08-08
Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題
我們Pojo類的屬性名和數(shù)據(jù)庫中的字段名不一致的現(xiàn)象時(shí)有發(fā)生,本文就詳細(xì)的介紹一下Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題,感興趣的可以了解一下2021-10-10
springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Mybatis自動(dòng)創(chuàng)建表和更新表結(jié)構(gòu)
這篇文章主要介紹了Mybatis自動(dòng)創(chuàng)建表和更新表結(jié)構(gòu)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

