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

了解SpringMVC的上傳和下載

 更新時間:2021年07月12日 09:28:37   作者:wbcra  
今天小編就為大家分享一篇關(guān)于Spring整合Springmvc的相關(guān)介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

springmvc.xml的配置

<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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://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/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--    開啟ioc注解開啟掃描-->
    <context:component-scan base-package="cn.hp"></context:component-scan>
    <!--    開啟mvc的注解掃描  里面有個轉(zhuǎn)換服務(wù)器-->
    <mvc:annotation-driven ></mvc:annotation-driven>
    <!--    將視圖解析器 交給spring-->
    <bean id="resolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        <property name="prefix" value="/WEB-INF/pages/"></property>-->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--    配置一個bean 讓他支持文件上傳-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--        倆屬性 name="defaultEncoding" 編碼格式utf-8
                    name="maxUploadSize" 文件上傳最大 值 100M
        -->
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="10240000"></property>
    </bean>
</beans>

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化時加載springmvc配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>fileEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>Encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>fileEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

主要代碼

package cn.hp.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;
@Controller
public class FileUploadController {
    @RequestMapping(value="upload.do")
    public String upload(MultipartFile file[], HttpServletRequest request) throws IOException {
        //1.獲取上傳文件路徑
        String path = request.getServletContext().getRealPath("/upload");
        for (int i =0;i<file.length;i++) {
            //2.拿到上傳的文件名
            String name = file[i].getOriginalFilename();
            //3.改名,把用戶上傳的文件進(jìn)行改名操作
            String newNmae = new Date().getTime() + new Random().nextInt(999999) + name;
            //4.實例化File類的對象加載上傳的路徑和文件
            File f = new File(path + newNmae);
            //5. MultipartFile里面的方法把這個路徑的文件寫入過去
            file[i].transferTo(f);
        }
        return "success";
    }
    @RequestMapping(value="download.do")
    public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException {
        //1.下載路徑
        String path = request.getServletContext().getRealPath("/download/");
        //2.實例化File類的對象加載下載的路徑和文件
        File f= new File(path+fileName);
        //3.轉(zhuǎn)格式
        String newName = new String(fileName.getBytes("utf-8"),"iso8859-1");
        //4.轉(zhuǎn)流
        HttpHeaders hh = new HttpHeaders();
        hh.setContentDispositionFormData("attachment",newName);
        hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //5.相應(yīng)發(fā)送
        return  new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED);
    }
}

NewFile.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--如果說表單有文件上傳的功能,那么表達(dá)就要設(shè)置這個屬性--%>
<form action="upload" method="post" enctype="multipart/form-data">
    文件上傳<input type="file" name="file" /><br/>
    文件上傳<input type="file" name="file" /><br/>
    文件上傳<input type="file" name="file" /><br/>
    <input type="submit" value="確定" />
</form>
<a href="download/自我介紹模板.docx">下載文件</a>
<a href="download/10.png">下載文件</a>
<a href="download/4.jpg">下載文件1</a>
<a href="download.do?fileName=新建文本文檔.txt">下載文件2</a>
<a href="download/新建文本文檔.txt">下載文件3</a>
</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
上傳成功
</body>
</html>

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Intellij IDEA 熱部署處理方法(圖解)

    Intellij IDEA 熱部署處理方法(圖解)

    本文通過圖文并茂的形式給大家介紹了Intellij IDEA 熱部署處理方法,需要的朋友可以參考下
    2018-02-02
  • java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例

    java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例

    這篇文章主要介紹了java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • Selenium Webdriver實現(xiàn)截圖功能的示例

    Selenium Webdriver實現(xiàn)截圖功能的示例

    今天小編就為大家分享一篇Selenium Webdriver實現(xiàn)截圖功能的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Nginx啟用壓縮及開啟gzip 壓縮的方法

    Nginx啟用壓縮及開啟gzip 壓縮的方法

    這篇文章主要介紹了Nginx啟用壓縮及開啟gzip 壓縮的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • Java關(guān)鍵字synchronized原理與鎖的狀態(tài)詳解

    Java關(guān)鍵字synchronized原理與鎖的狀態(tài)詳解

    在Java當(dāng)中synchronized關(guān)鍵字通常是用來標(biāo)記一個方法或者代碼塊。本文將通過示例為大家詳細(xì)介紹一下Synchronized的各種使用方法,需要的可以參考一下
    2022-08-08
  • Java5 枚舉類詳解及實例代碼

    Java5 枚舉類詳解及實例代碼

    這篇文章主要介紹了Java5 枚舉類詳解及實例代碼的相關(guān)資料,枚舉類是java5 新類型,全部都是類型安全的形式表示,需要的朋友可以參考下
    2016-12-12
  • Intellij IDEA Debug調(diào)試技巧(小結(jié))

    Intellij IDEA Debug調(diào)試技巧(小結(jié))

    這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java?Collections工具類中常用算法解析

    Java?Collections工具類中常用算法解析

    在軟件開發(fā)中,算法是非常重要的一部分,它們可以提供高效的數(shù)據(jù)處理和操作,這篇文章主要為大家介紹了Collections?工具類集合框架中常用算法,感興趣的可以了解一下
    2023-06-06
  • java實現(xiàn)斗地主發(fā)牌功能

    java實現(xiàn)斗地主發(fā)牌功能

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • SpringBoot中獲取profile的方法詳解

    SpringBoot中獲取profile的方法詳解

    這篇文章主要介紹了springboot獲取profile的操作,文中的示例代碼講解詳細(xì),具有很好的參考價值,希望對大家有所幫助
    2022-04-04

最新評論