了解SpringMVC的上傳和下載
更新時(shí)間:2021年07月12日 09:28:37 作者:wbcra
今天小編就為大家分享一篇關(guān)于Spring整合Springmvc的相關(guān)介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
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的注解掃描 里面有個(gè)轉(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>
<!-- 配置一個(gè)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>
<!-- 初始化時(shí)加載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.實(shí)例化File類的對(duì)象加載上傳的路徑和文件
File f = new File(path + newNmae);
//5. MultipartFile里面的方法把這個(gè)路徑的文件寫入過去
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.實(shí)例化File類的對(duì)象加載下載的路徑和文件
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è)置這個(gè)屬性--%>
<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)文章
Mapper層繼承BaseMapper<T>需要引入的pom依賴方式
這篇文章主要介紹了Mapper層繼承BaseMapper<T>需要引入的pom依賴方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(20)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例
這篇文章主要介紹了Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
SpringBoot超詳細(xì)講解Thymeleaf模板引擎
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-07-07
解決Test類中不能使用Autowired注入bean的問題
這篇文章主要介紹了解決Test類中不能使用Autowired注入bean的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址
這篇文章主要介紹了如何利用Java語言實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的參考價(jià)值,快跟隨小編一起學(xué)習(xí)一下吧2022-06-06
Java對(duì)數(shù)組實(shí)現(xiàn)選擇排序算法的實(shí)例詳解
這篇文章主要介紹了Java對(duì)數(shù)組實(shí)現(xiàn)選擇排序算法的實(shí)例,選擇排序的比較次數(shù)為 O(N^2)而交換數(shù)為O(N),需要的朋友可以參考下2016-04-04

