国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Springmvc實現文件上傳

2024-07-14 08:42:32
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Springmvc實現文件上傳的具體代碼,供大家參考,具體內容如下

1.環境搭建:

在maven的pom.xml文件中導入兩個依賴

1).commons-fileupload
2).commons-io

在resources目錄下的springmvc.xml文件中配置multipartResolver

<?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/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    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd">  <!--包掃描-->  <context:component-scan base-package="cn.itcast"></context:component-scan>  <!--配置multipartResolver,注意:id名稱固定為multipartResolver-->  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  </bean>  <mvc:annotation-driven ></mvc:annotation-driven>    <!--讓靜態資源不經過過濾器-->  <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>  <!--視圖解析器給controller中返回的邏輯視圖名加上前綴和后綴-->  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">    <property name="prefix" value="/WEB-INF/pages/"></property>    <property name="suffix" value=".jsp"></property>  </bean></beans>

2.編寫前臺測試jsp

<form action="/test/file" method="post" enctype="multipart/form-data">    上傳的文件:<input type="file" name="upload"><br/>    密鑰:<input type="text" name="password"><br/>    <input type="submit" value="提交"> </form>

注意頁面三要素:

      1).表單提交方式必須為post

      2).表單中必須有file域,即type="file"

      3).表單中enctype="multipart/form-data"

3.編寫后臺測試代碼

@Controller@RequestMapping("/test")public class FileUploadController {  @RequestMapping("/file")  public String testFileUpload(HttpServletRequest request, MultipartFile upload) throws IOException {      //upload是表單中文件name屬性值,必須保持一致    System.out.println("testFileUpload...");    String realPath = request.getSession().getServletContext().getRealPath("/uploads");    File file = new File(realPath);    if(!file.exists()){      file.mkdirs();//創建文件夾    }    String filename = upload.getOriginalFilename(); //獲取文件名    String name = upload.getName();//獲取表單中的name屬性值 即upload    String uuid = UUID.randomUUID().toString().replaceAll("-", "");//生成uuid避免文件名重復導致沖突覆蓋    filename=uuid+"_"+filename;    upload.transferTo(new File(file,filename));    return "forward:success.jsp";}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 惠安县| 潜江市| 汾西县| 正蓝旗| 瑞金市| 那曲县| 嘉峪关市| 临漳县| 永福县| 湾仔区| 渝北区| 天水市| 阿巴嘎旗| 锡林郭勒盟| 朝阳市| 龙江县| 惠水县| 衢州市| 海南省| 申扎县| 长沙县| 共和县| 东明县| 油尖旺区| 莱州市| 怀仁县| 利川市| 泗洪县| 朝阳县| 东平县| 墨竹工卡县| 汾西县| 邯郸县| 谷城县| 江安县| 安仁县| 呼和浩特市| 鄂托克前旗| 隆安县| 普兰县| 乌拉特后旗|