最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過簡單步驟實(shí)現(xiàn)SpringMVC文件上傳

 更新時(shí)間:2019年11月14日 15:04:34   作者:EXTRA·  
這篇文章主要介紹了通過簡單步驟實(shí)現(xiàn)SpringMVC文件上傳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了通過簡單步驟實(shí)現(xiàn)SpringMVC文件上傳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、創(chuàng)建文件上傳FileController類

package com.byzore.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController {
  @RequestMapping("/fileUpload")
  /**
   * MultipartFile 選擇文件
   */
  public String fileupload(HttpSession session, MultipartFile file,String author)throws IOException{
    System.out.println("作者:"+author);
    System.out.println(file);
    /**
     * 如何處理文件
     */
    if (!file.isEmpty()){
      //獲取文件名稱
      String fileName=file.getOriginalFilename();
      //獲取到需要上傳的路徑
      String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
      //創(chuàng)建文件對象
      File uploadfile=new File(realPath+"\\"+fileName);
      //如何上傳文件
      file.transferTo(uploadfile);
    }
    return "index";
  }



  @RequestMapping("/fileUploads")
  /**
   * 多文件上傳
   */
  public String fileuploads(HttpSession session, MultipartFile[] uploadFiles,String author)throws IOException{
    System.out.println("作者:"+author);
    System.out.println(uploadFiles);
    for (MultipartFile file: uploadFiles) {
      /**
       * 如何處理文件
       */
      if (!file.isEmpty()){
        //獲取文件名稱
        String fileName=file.getOriginalFilename();
        //獲取到需要上傳的路徑
        String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
        //創(chuàng)建文件對象
        File uploadfile=new File(realPath+"\\"+fileName);
        //如何上傳文件
        file.transferTo(uploadfile);
      }
    }

    return "index";
  }
}

二、編輯applicationContext.xml文件

<?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:aop="http://www.springframework.org/schema/aop"
      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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--將Controller注入到容器當(dāng)中  id就是瀏覽器請求地址-->
    <!--<bean id="/firstController" class="com.springmvc.controller.FirstController"></bean>-->

    <!--配置包掃描器-->
    <context:component-scan base-package="com.byzore"/>
    <!--Spring支持SpringMVC-->
    <mvc:annotation-driven/>

  <!--配置視圖解析器-->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <!--利用DefaultServlet放行資源-->
  <mvc:default-servlet-handler/>

  <!--從Spring3.0.4版本提供資源放行的方式-->
  <!--<mvc:resources mapping="/**" location="/img"/>-->
<!--文件上傳解析器-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--編碼-->
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="maxUploadSize" value="5000000000"/>
  </bean>
</beans>

三、創(chuàng)建fileUpload.jsp頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>文件上傳</title>
</head>
<body>
  <form action="/file/fileUpload" method="post" enctype="multipart/form-data">
    <input type="file" name="fileUpload"/>
作者:<input type="text" name="author"/>
    <input type="submit" value="提交"/>
  </form>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Sping中如何處理@Bean注解bean同名的問題

    Sping中如何處理@Bean注解bean同名的問題

    這篇文章主要介紹了Sping中如何處理@Bean注解bean同名的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • java編程 中流對象選取規(guī)律詳解

    java編程 中流對象選取規(guī)律詳解

    下面小編就為大家?guī)硪黄猨ava編程 中流對象選取規(guī)律詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Idea中如何修改項(xiàng)目的SVN地址

    Idea中如何修改項(xiàng)目的SVN地址

    這篇文章主要介紹了Idea中如何修改項(xiàng)目的SVN地址問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • java中FileOutputStream中文亂碼問題解決辦法

    java中FileOutputStream中文亂碼問題解決辦法

    這篇文章主要介紹了java中FileOutputStream中文亂碼問題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 解決使用IDEA時(shí)跳轉(zhuǎn)到.class的問題

    解決使用IDEA時(shí)跳轉(zhuǎn)到.class的問題

    這篇文章主要介紹了解決使用IDEA時(shí)跳轉(zhuǎn)到.class的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring Cloud服務(wù)安全連接方式

    Spring Cloud服務(wù)安全連接方式

    這篇文章主要介紹了Spring Cloud服務(wù)安全連接方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器

    Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器

    這篇文章主要介紹了Spring Boot項(xiàng)目實(shí)戰(zhàn)之?dāng)r截器與過濾器,文中給大家詳細(xì)介紹了springboot 攔截器和過濾器的基本概念,過濾器的配置,需要的朋友可以參考下
    2018-01-01
  • java實(shí)現(xiàn)快速排序算法

    java實(shí)現(xiàn)快速排序算法

    快速排序算法是基于分治策略的另一個(gè)排序算法。其基本思想是:對輸入的子數(shù)組a[p:r],按以下三個(gè)步驟進(jìn)行排序。 1) 分解(Divide)(2) 遞歸求解(Conquer) (3) 合并(Merge)
    2015-04-04
  • springboot?集成identityserver4身份驗(yàn)證的過程解析

    springboot?集成identityserver4身份驗(yàn)證的過程解析

    這篇文章主要介紹了springboot?集成identityserver4身份驗(yàn)證的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • Java編寫緩存工具類的示例代碼

    Java編寫緩存工具類的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何基于Java編寫一個(gè)緩存工具類,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下
    2023-07-07

最新評論

韶山市| 库尔勒市| 达日县| 平定县| 洛隆县| 全椒县| 泽库县| 准格尔旗| 东乡县| 江陵县| 襄垣县| 宝应县| 新巴尔虎左旗| 潢川县| 仪陇县| 六盘水市| 体育| 吴堡县| 习水县| 佛教| 勐海县| 新竹市| 谢通门县| 平遥县| 措勤县| 湘潭县| 社旗县| 札达县| 响水县| 胶南市| 九龙坡区| 大埔县| 山东省| 遵义县| 永春县| 郧西县| 建德市| 巴南区| 庆云县| 武强县| 上犹县|