瞭解SpringMVC的上傳和下載
阿新 • • 發佈:2021-07-13
目錄
- springmvc.xml的配置
- web.xml的配置
- 主要程式碼
- NewFile.p
- success.jsp
- 總結
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的註解掃描 裡面有個轉換伺服器--> <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/ee" 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> &lwww.cppcns.comt;/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.springframewoNgWrCdpusrk.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.改名,把使用者上傳的檔案進行改名操作
String newNmae = new Date().getTime() + new Random().nextInt(999999) + name;
//4.例項化File類的物件載入上傳的路徑和檔案
File f = new File(path + newNmae);
//5. MultipartFiNgWrCdpusle裡面的方法把這個路徑的檔案寫入過去
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.轉格式
String newName = new String(fileName.getBytes("utf-8"),"iso8859-1");
//4.轉流
HttpHeaders hh = new HttpHeaders();
hh.setContentDispositionFormData("attachment",newName);
hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//5.相應傳送
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> <%--如果說表單有檔案上傳的功能,那麼表達就要設定這個屬性--%> <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>
總結
本篇文章就到這裡了,希望能給你帶來幫助,也希望您能夠多多關注我們的更多內容!