欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過簡單步驟實(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)文章

  • SpringMVC對自定義controller入?yún)㈩A(yù)處理方式

    SpringMVC對自定義controller入?yún)㈩A(yù)處理方式

    這篇文章主要介紹了SpringMVC對自定義controller入?yún)㈩A(yù)處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java中抽象類和接口的相同和不同點(diǎn)介紹

    java中抽象類和接口的相同和不同點(diǎn)介紹

    大家好,本篇文章主要講的是java中抽象類和接口的相同和不同點(diǎn)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • IDEA編譯報(bào)錯(cuò):Error:java:無效的源發(fā)行版:17的解決辦法

    IDEA編譯報(bào)錯(cuò):Error:java:無效的源發(fā)行版:17的解決辦法

    IDEA里面裝了幾個(gè)版本的JDK,導(dǎo)入工程后時(shí)不時(shí)提示一下錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于IDEA編譯報(bào)錯(cuò):Error:java:無效的源發(fā)行版:17的解決辦法,需要的朋友可以參考下
    2023-01-01
  • 分布式鎖實(shí)例教程之防止重復(fù)提交

    分布式鎖實(shí)例教程之防止重復(fù)提交

    訂單重復(fù)問題已經(jīng)是老生常談的問題了,下面這篇文章主要給大家介紹了關(guān)于分布式鎖實(shí)例教程之防止重復(fù)提交的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • Java中AML讀寫常用工具包及使用方法示例

    Java中AML讀寫常用工具包及使用方法示例

    這篇文章主要給大家介紹了關(guān)于Java中AML讀寫常用工具包及使用方法的相關(guān)資料,Java?YAML工具類是一種用于處理YAML格式文件的工具類,可以方便地讀取、寫入和操作YAML文件,需要的朋友可以參考下
    2024-04-04
  • 詳解Struts2標(biāo)簽遍歷

    詳解Struts2標(biāo)簽遍歷

    這篇文章主要介紹了Struts2標(biāo)簽遍歷,以及相關(guān)的用法示例,需要的朋友可以參考下。
    2017-09-09
  • Spring?Boot?Nacos?實(shí)現(xiàn)不停服發(fā)布過程詳解

    Spring?Boot?Nacos?實(shí)現(xiàn)不停服發(fā)布過程詳解

    這篇文章主要為大家介紹了Spring?Boot?Nacos實(shí)現(xiàn)不停服發(fā)布過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Java設(shè)計(jì)模式中橋接模式應(yīng)用詳解

    Java設(shè)計(jì)模式中橋接模式應(yīng)用詳解

    橋接,顧名思義,就是用來連接兩個(gè)部分,使得兩個(gè)部分可以互相通訊。橋接模式將系統(tǒng)的抽象部分與實(shí)現(xiàn)部分分離解耦,使他們可以獨(dú)立的變化。本文通過示例詳細(xì)介紹了橋接模式的原理與使用,需要的可以參考一下
    2022-11-11
  • 在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志

    在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志

    這篇文章主要介紹了在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java?Web開發(fā)環(huán)境配置詳解

    Java?Web開發(fā)環(huán)境配置詳解

    這篇文章主要介紹了Java?Web開發(fā)環(huán)境配置詳解,對初學(xué)者是個(gè)必備的過程,有需要的可以了解一下
    2016-11-11

最新評論