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

SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2021年02月01日 13:58:56   作者:jiawei3998  
這篇文章主要介紹了SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
   version="5.0">
 <!--注冊(cè)DispatcherServlet-->
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

SpringMVC配置文件 applicationContext.xml

上傳文件的核心配置類:CommonsMultipartResolver,注意id="multipartResolver"不要寫錯(cuò)

<?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/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  https://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  https://www.springframework.org/schema/mvc/spring-mvc.xsd">

 <!--配置自動(dòng)掃描controller包-->
 <context:component-scan base-package="com.pro.controller"/>
 <!--配置靜態(tài)資源過濾-->
 <mvc:default-servlet-handler/>
 <!--配置注解驅(qū)動(dòng)-->
 <mvc:annotation-driven/>

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

 <!--SpringMVC文件上傳配置-->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!--設(shè)置請(qǐng)求的編碼格式, 必須和pageEncoding的屬性一致, 以便正確讀取表單的值, 默認(rèn)為ISO-8859-1-->
  <property name="defaultEncoding" value="utf-8"/>
  <!--上傳文件的大小限制, 單位為字節(jié) (10485760 = 10M)-->
  <property name="maxUploadSize" value="10485760"/>
  <property name="maxInMemorySize" value="40960"/>
 </bean>
</beans>

文件上傳 Controller

上傳實(shí)現(xiàn)一

package com.pro.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {
  /*
   * 采用file.transferTo 來保存上傳的文件
   */
  @RequestMapping("/upload2")
  public Map fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

    //上傳路徑保存設(shè)置
    String path = request.getServletContext().getRealPath("/upload");
    File realPath = new File(path);
    if (!realPath.exists()){
      realPath.mkdir();
    }
    //上傳文件地址
    System.out.println("上傳文件保存地址 --> "+realPath);

    //通過CommonsMultipartFile的方法直接寫文件(注意這個(gè)時(shí)候)
    file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));

    Map<Object, Object> hashMap = new HashMap<>();
    hashMap.put("code", 0);
    hashMap.put("msg", "上傳成功");

    return hashMap;
  }
}

上傳實(shí)現(xiàn)二

這里的文件名稱沒有使用 UUID組合名稱 為了方便測(cè)試

package com.pro.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {

  // @RequestParam("file") 將 name=file 控件得到的文件封裝成 CommonsMultipartFile 對(duì)象
  // 批量上傳把 CommonsMultipartFile 改為數(shù)組即可
  @RequestMapping("/upload")
  public String upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
    // 獲取文件名稱
    String uploadFileName = file.getOriginalFilename();

    // 如果文件名為空, 直接返回首頁
    if ("".equals(uploadFileName)) {
      return "file upload error";
    }

    System.out.println("上傳文件名 --> " + uploadFileName);


    // 設(shè)置文件的保存位置
    String path = request.getServletContext().getRealPath("/upload");
    // 判斷路徑是否存在
    File realPath = new File(path);
    if (!realPath.exists()) {
      // 如果不存在就創(chuàng)建
      realPath.mkdir();
    }

    System.out.println("文件保存路徑 --> " + realPath);

    // 獲取文件輸入流
    InputStream is = file.getInputStream();
    // 獲取文件輸出流
    FileOutputStream os = new FileOutputStream(new File(realPath, uploadFileName));

    // 緩沖區(qū)讀寫文件
    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) != -1) {
      os.write(buffer, 0, len);
      os.flush();
    }

    // 關(guān)閉流
    os.close();
    is.close();

    return "file upload success";
  }
}

測(cè)試

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
  <title>$Title$</title>
 </head>
 <body>
  <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/upload2" method="post">
   <input type="file" name="file">
   <input type="submit" value="上傳實(shí)現(xiàn)一">
  </form>
  
  
  <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/upload" method="post">
   <input type="file" name="file">
   <input type="submit" value="上傳實(shí)現(xiàn)二">
  </form>
 </body>
</html>

依賴

核心依賴就是 commons-fileupload

<!--導(dǎo)入依賴-->
<dependencies>
  <!--單元測(cè)試-->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
  </dependency>
  <!--spring-->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
  </dependency>
  <!--文件上傳-->
  <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
  </dependency>
  <!--servlet-api導(dǎo)入高版本的-->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
  </dependency>
  <!--jsp-->
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
  </dependency>
  <!--jstl表達(dá)式-->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>
</dependencies>

到此這篇關(guān)于SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳的文章就介紹到這了,更多相關(guān)SpringMVC 實(shí)現(xiàn)文件上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 剖析Java中的事件處理與異常處理機(jī)制

    剖析Java中的事件處理與異常處理機(jī)制

    這篇文章主要介紹了Java中的事件處理與異常處理機(jī)制,講解Java是如何對(duì)事件或者異常作出響應(yīng)以及定義異常的一些方法,需要的朋友可以參考下
    2016-01-01
  • 利用Mybatis?Plus實(shí)現(xiàn)一個(gè)SQL攔截器

    利用Mybatis?Plus實(shí)現(xiàn)一個(gè)SQL攔截器

    SQL攔截器是一種用于攔截和修改Mybatis執(zhí)行的SQL語句的工具,通過使用SQL攔截器,開發(fā)人員可以在執(zhí)行SQL語句之前或之后對(duì)其進(jìn)行修改或記錄,本文就來借助一下Mybatis-Plus實(shí)現(xiàn)一個(gè)SQL攔截器吧
    2023-05-05
  • JVM之內(nèi)存分配和回收機(jī)制

    JVM之內(nèi)存分配和回收機(jī)制

    本篇主要介紹JVM內(nèi)存分配和回收策略,內(nèi)容主要節(jié)選自《深入理解java虛擬機(jī)》,需要的朋友可以參考下
    2023-05-05
  • java復(fù)制文件的4種方式及拷貝文件到另一個(gè)目錄下的實(shí)例代碼

    java復(fù)制文件的4種方式及拷貝文件到另一個(gè)目錄下的實(shí)例代碼

    這篇文章主要介紹了java復(fù)制文件的4種方式,通過實(shí)例帶給大家介紹了java 拷貝文件到另一個(gè)目錄下的方法,需要的朋友可以參考下
    2018-06-06
  • SpringBoot整合Java DL4J實(shí)現(xiàn)文本分類系統(tǒng)

    SpringBoot整合Java DL4J實(shí)現(xiàn)文本分類系統(tǒng)

    在當(dāng)今信息爆炸的時(shí)代,自然語言處理領(lǐng)域中的文本分類顯得尤為重要,文本分類能夠高效地組織和管理海量的文本數(shù)據(jù),隨著互聯(lián)網(wǎng)的飛速發(fā)展,我們每天都被大量的文本信息所包圍,本文將介紹如何使用 Spring Boot 整合 Java Deeplearning4j 來構(gòu)建一個(gè)文本分類系統(tǒng)
    2024-10-10
  • Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例

    Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例

    下面小編就為大家分享一篇Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-12-12
  • SpringBoot整合BootStrap實(shí)戰(zhàn)

    SpringBoot整合BootStrap實(shí)戰(zhàn)

    這篇文章主要介紹了SpringBoot整合BootStrap實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java異步編程的5種異步實(shí)現(xiàn)方式詳解

    Java異步編程的5種異步實(shí)現(xiàn)方式詳解

    這篇文章主要介紹了Java異步編程的5種異步實(shí)現(xiàn)方式詳解,異步編程是程序并發(fā)運(yùn)行的一種手段,它允許多個(gè)事件同時(shí)發(fā)生,當(dāng)程序調(diào)用需要長(zhǎng)時(shí)間運(yùn)行的方法時(shí),它不會(huì)阻塞當(dāng)前的執(zhí)行流程,程序可以繼續(xù)運(yùn)行,需要的朋友可以參考下
    2024-01-01
  • Java獲取線程ID的實(shí)例

    Java獲取線程ID的實(shí)例

    以下實(shí)例演示了如何使用 getThreadId() 方法獲取線程id,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • 實(shí)戰(zhàn)干貨之基于SpringBoot的RabbitMQ多種模式隊(duì)列

    實(shí)戰(zhàn)干貨之基于SpringBoot的RabbitMQ多種模式隊(duì)列

    RabbitMQ 是一個(gè)由Erlang語言開發(fā)的AMQP的開源實(shí)現(xiàn),支持多種客戶端。用于在分布式系統(tǒng)中存儲(chǔ)轉(zhuǎn)發(fā)消息,在易用性、擴(kuò)展性、高可用性等方面表現(xiàn)不俗,下文將帶你深入了解 RabbitMQ 多種模式隊(duì)列
    2021-09-09

最新評(píng)論

安义县| 镇原县| 肇庆市| 库尔勒市| 东乡族自治县| 安顺市| 德庆县| 闸北区| 威信县| 城固县| 五指山市| 盐亭县| 永兴县| 济源市| 平阴县| 海城市| 娄底市| 株洲市| 安化县| 辽阳市| 福海县| 社旗县| 蒲江县| 乌拉特后旗| 徐汇区| 都匀市| 乐平市| 九龙坡区| 庐江县| 巴楚县| 蒙城县| 黄梅县| 新河县| 武功县| 平陆县| 怀化市| 万宁市| 托里县| 新竹县| 烟台市| 永川市|