溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-08-20 19:31:15 來源:腳本之家 閱讀:143 作者:EXTRA· 欄目:編程語言

這篇文章主要介紹了通過簡單步驟實(shí)現(xiàn)SpringMVC文件上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(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)建文件對(duì)象
      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)建文件對(duì)象
        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就是瀏覽器請(qǐng)求地址-->
    <!--<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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI