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

SpringMVC實(shí)現(xiàn)上傳下載文件

 更新時(shí)間:2022年09月06日 11:24:26   作者:凌冰_  
這篇文章主要為大家詳細(xì)介紹了SpringMVC實(shí)現(xiàn)上傳下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了SpringMVC實(shí)現(xiàn)上傳下載文件的具體代碼,供大家參考,具體內(nèi)容如下

一、SpringMVC專門提供了CommonsMultipartResolver組件用于文件上傳:

(1)maxUploadSize 文件最大限制,單位是byte
(2)maxInMemorySize 低于這個(gè)大小的文件暫存內(nèi)存中
(3)defaultEncoding 默認(rèn)編碼utf-8

必須在spring-mvc.xml文件

<!-- (2)配置 MultipartResolver 實(shí)現(xiàn)文件上傳 ?
?? ? ? ? ? ? ? ? ?注意:id="multipartResolver"是固定寫法
?? ? ? -->
?? ? ? <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
?? ? ? ? <!-- 字符編碼 -->
?? ? ? ?<property name="defaultEncoding" value="utf-8"/>
?? ? ? ?<!-- max size 10M -->
?? ? ? ?<property name="maxUploadSize" value="10485760000"/>
?? ? ? ?<!--內(nèi)存中最大 4K ?-->
?? ? ? ?<property name="maxInMemorySize" value="4096"/>
?? ? ? </bean>

二、SpringMVC文件上傳引入jar包 

 

必須在配置Pom.xml文件

<!-- fileupload start -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>commons-fileupload</groupId>
?? ??? ??? ?<artifactId>commons-fileupload</artifactId>
?? ??? ??? ?<version>1.3.1</version>
?? ??? ?</dependency>
?
?? ??? ?<dependency>
?? ??? ??? ?<groupId>commons-io</groupId>
?? ??? ??? ?<artifactId>commons-io</artifactId>
?? ??? ??? ?<version>2.4</version>
?? ??? ?</dependency>
?
?? ??? ?<!-- end -->

三、實(shí)現(xiàn)【單個(gè)文件】上傳

(1)JSP頁面必須放在WEB-INF下 upload1.jsp 必須添加enctype="multipart/form-data" 

<body>
?? ?<div style="margin: 0 auto; margin-top: 100px; background:snow">
?? ??? ?<form method="post" action="upload1.html" name="form1"
?? ??? ??? ?enctype="multipart/form-data">
?? ??? ??? ?<p>
?? ??? ??? ??? ? ?照片:<input type="file" name="imagefile">
?? ??? ??? ??? ?<input type="submit" value="上傳" name="button1"> <br>
?? ??? ??? ?</p>
?? ??? ?</form>
?? ?</div>

 (2) 寫控制類 UploadController.java

@Controller
public class UploadController {
?
?? ?@RequestMapping("upload1")
?? ?public String getUpload(@RequestParam("imagefile") MultipartFile imagefile,
?? ??? ??? ?HttpServletRequest request) {
?? ??? ?// 獲取上傳的服務(wù)器路徑
?? ??? ?String pathString = request.getSession().getServletContext().getRealPath("/upload/");
?? ??? ?// 獲取文件
?? ??? ?String fileName = imagefile.getOriginalFilename();
?? ??? ?
?? ??? ?System.out.println(fileName);
?
?? ??? ?// 判斷上傳的路徑是否存在
?? ??? ?File file = new File(pathString);
?? ??? ?if (!file.exists()) {
?? ??? ??? ?file.mkdirs();
?? ??? ?}
?
?? ??? ?System.out.println("上傳路徑=" + pathString +"/"+ fileName);
?? ??? ?// 文件不存在
?? ??? ?File targetFile = new File(pathString +"/"+ fileName);
?? ??? ?if (!targetFile.exists()) {
?? ??? ??? ?try {
?? ??? ??? ??? ?targetFile.createNewFile();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?
?? ??? ?try {
?? ??? ??? ?// 上傳
?? ??? ??? ?imagefile.transferTo(targetFile);
?? ??? ?} catch (IllegalStateException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?
?? ??? ?//注意:/springmvc5/WEB-INF/jsp/http:/localhost:8888/springmvc5/upload/1.gif.jsp
?? ??? ?//返回文件,必須是重定向文件
?? ??? ?return "redirect:http://localhost:8888/springmvc5/upload/" + fileName;
?
?? ?} }

(3)效果

選擇圖片路徑:

單擊上傳:

四、實(shí)現(xiàn)【多個(gè)文件】上傳

(1)JSP頁面(必須放在WEB-INF下) upload2.jsp  必須添加enctype="multipart/form-data" 

<body>
?? ?<div style="margin: 0 auto; margin-top: 100px; background:snow">
?? ??? ?<form method="post" action="upload2.html" name="form1"
?? ??? ??? ?enctype="multipart/form-data">
?? ??? ??? ?<p>
?? ??? ??? ??? ? ?照片1:<input type="file" name="imagefile1"><p/>
?? ??? ??? ??? ? ?照片2:<input type="file" name="imagefile2"><p/>
?? ??? ??? ??? ?<input type="submit" value="上傳" name="button1"> <br>
?? ??? ??? ?</p>
?? ??? ?</form>
?? ?</div>
</body>

 (2) 寫控制類 UploadController.java

@RequestMapping("upload2")
?? ?public String getUpload2(HttpServletRequest request) {
?
?? ??? ?// 多文件上傳
?? ??? ?MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
?? ??? ?// 獲得多個(gè)文件
?? ??? ?Map<String, MultipartFile> map = multipartRequest.getFileMap();
?
?? ??? ?// 獲取上傳的服務(wù)器路徑
?? ??? ?String pathString = request.getSession().getServletContext().getRealPath("/upload/");
?? ??? ?// 判斷上傳的路徑是否存在
?? ??? ?File file1 = new File(pathString);
?? ??? ?if (!file1.exists()) {
?? ??? ??? ?file1.mkdirs();
?? ??? ?}
?
?? ??? ?// 獲取文件
?? ??? ?List<String> list = new ArrayList<String>();
?
?? ??? ?// 遍歷數(shù)據(jù)
?? ??? ?for (MultipartFile file : map.values()) {
?? ??? ??? ?String fileName = file.getOriginalFilename();
?? ??? ??? ?System.out.println("上傳路徑=" + pathString +"/"+ fileName);
?? ??? ??? ?// 文件不存在
?? ??? ??? ?File targetFile = new File(pathString ?+"/"+ fileName);
?? ??? ??? ?if (!targetFile.exists()) {
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?targetFile.createNewFile();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?
?? ??? ??? ?try {
?? ??? ??? ??? ?// 上傳
?? ??? ??? ??? ?file.transferTo(targetFile);
?
?? ??? ??? ??? ?// 保存路徑
?? ??? ??? ??? ?list.add("http://localhost:8888/springmvc5/upload/" + fileName);
?? ??? ??? ?} catch (IllegalStateException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?
?? ??? ?}
?? ??? ?// 保存每個(gè)上傳的路徑
?? ??? ?request.setAttribute("files", list);
?? ??? ?
?? ??? ?return "showUpload"; ? //跳轉(zhuǎn)到showUpload.jsp頁面哦!
?
?? ?}

注意:return "showUpload";是具體顯示的頁面;必須配置視圖解析器在spring-mvc.xml文件中

<!--(1) spring 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" ?p:suffix=".jsp"></bean>

(3)JSP頁面: showUpload.jsp

<div style="margin: 0 auto; margin-top: 100px; background:snow">
?? ? ?<%
?? ? ? ?List<String> list =(List<String>) request.getAttribute("files");
?? ? ? ?
?? ? ? ?for(String str:list){
?? ? ?
?? ? ? %>
?? ? ? ? ?<a href="<%=str%>" rel="external nofollow" ><img src="<%=str%>" alt=""/></a>
?? ? ??
?? ? ? <%} %>
</div>

(4)效果

單擊上傳:

查看上傳到服務(wù)器的圖片

五、下載圖片

 (1)JSP頁面 login.jsp

<a href="download.html?fileName=08.gif" >下載圖片</a><p/>

 (2)控制類DownController

@Controller
public class DownController {
?
?? ?@RequestMapping("/download")
?? ?public String download(@RequestParam String fileName,
?? ??? ??? ?HttpServletRequest request, HttpServletResponse response) {
?
?? ??? ?// 設(shè)置響應(yīng)編碼
?? ??? ?response.setContentType("text/html;charset=utf-8");
?
?? ??? ?// 設(shè)置請(qǐng)求編碼
?? ??? ?try {
?? ??? ??? ?request.setCharacterEncoding("utf-8");
?? ??? ?} catch (UnsupportedEncodingException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?
?? ??? ?// 字節(jié)流
?? ??? ?BufferedInputStream bis=null;
?? ??? ?BufferedOutputStream bos=null;
?
?? ??? ?// 獲取服務(wù)器的路徑
?? ??? ?String path = request.getSession().getServletContext().getRealPath("/upload/");
?
?? ??? ?// 下載的路徑
?? ??? ?String downPath = path +"/"+ fileName;
?
?? ??? ?try {
?? ??? ??? ?// 文件大小
?? ??? ??? ?long fileSize = new File(downPath).length();
?? ??? ??? ?
?? ??? ??? ?//設(shè)置內(nèi)容類型
?? ??? ??? ?response.setContentType("application/x-msdownload");
?? ??? ??? ?//設(shè)置頭信息
?? ??? ??? ?response.setHeader("Content-disposition", "attachment; filename="+new String(fileName.getBytes("utf-8"),"ISO8859-1"));
?? ??? ??? ?response.setHeader("Content-Length",String.valueOf(fileSize));
?? ??? ??? ?//字節(jié)流
?? ??? ??? ?bis = new BufferedInputStream(new FileInputStream(downPath));
?? ??? ??? ?bos= new BufferedOutputStream(response.getOutputStream());
?? ??? ??? ?//字節(jié)數(shù)組
?? ??? ??? ?byte[] by = new byte[2048];
?? ??? ??? ?
?? ??? ??? ?//
?? ??? ??? ?int length=0;
?? ??? ??? ?
?? ??? ??? ?//讀取
?? ??? ??? ?while((length=bis.read(by,0,by.length))!=-1){
?? ??? ??? ??? ?//寫入
?? ??? ??? ??? ?bos.write(by, 0, length);
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?//關(guān)閉連接
?? ??? ??? ? if(bis!=null){
?? ??? ??? ??? ? try {
?? ??? ??? ??? ??? ?bis.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ? }
?? ??? ??? ? if(bos!=null){
?? ??? ??? ??? ? try {
?? ??? ??? ??? ??? ? bos.close();
?? ??? ??? ??? ? } catch (IOException e) {
?? ??? ??? ??? ??? ? // TODO Auto-generated catch block
?? ??? ??? ??? ??? ? e.printStackTrace();
?? ??? ??? ??? ? }
?? ??? ??? ? }
?? ??? ?}
?
?? ??? ?return null;
?
?? ?}
}

(3)效果

保存或打開如下:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入理解Java定時(shí)調(diào)度(Timer)機(jī)制

    深入理解Java定時(shí)調(diào)度(Timer)機(jī)制

    這篇文章主要介紹了深入理解Java定時(shí)調(diào)度(Timer)機(jī)制,本節(jié)我們主要分析 Timer 的功能。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • SpringBoot實(shí)現(xiàn)國際化i18n詳解

    SpringBoot實(shí)現(xiàn)國際化i18n詳解

    國際化(Internationalization,簡稱i18n)是指在軟件應(yīng)用中支持多種語言和文化的能力,本文將介紹如何在Spring?Boot應(yīng)用中實(shí)現(xiàn)國際化,需要的可以參考下
    2024-12-12
  • SpringBoot將Bean放入容器的五種方式

    SpringBoot將Bean放入容器的五種方式

    這篇文章給大家介紹了SpringBoot將Bean放入容器的五種方式,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • Java線程創(chuàng)建靜態(tài)代理模式代碼實(shí)例

    Java線程創(chuàng)建靜態(tài)代理模式代碼實(shí)例

    這篇文章主要介紹了Java線程創(chuàng)建靜態(tài)代理模式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java中操作Xml使用方法備忘錄(Hutool工具類XmlUtil、XStream)

    Java中操作Xml使用方法備忘錄(Hutool工具類XmlUtil、XStream)

    這篇文章主要給大家介紹了關(guān)于Java中操作Xml使用方法(Hutool工具類XmlUtil、XStream)的相關(guān)資料,XMLUtil是一個(gè)工具類,主要用于讀取XML配置文件并提供相應(yīng)的操作方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • java random.nextInt的坑及解決

    java random.nextInt的坑及解決

    這篇文章主要介紹了java random.nextInt的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java容器ArrayList知識(shí)點(diǎn)總結(jié)

    Java容器ArrayList知識(shí)點(diǎn)總結(jié)

    本篇文章給大家分享了Java容器ArrayList的相關(guān)知識(shí)點(diǎn),對(duì)此有需要的朋友可以跟著學(xué)習(xí)參考下。
    2018-05-05
  • Java中的隱式參數(shù)和顯示參數(shù)實(shí)例詳解

    Java中的隱式參數(shù)和顯示參數(shù)實(shí)例詳解

    這篇文章主要介紹了Java中的隱式參數(shù)和顯示參數(shù)是什么,另外還有兩個(gè)小例子幫助大家理解,需要的朋友可以參考下。
    2017-08-08
  • Java基數(shù)排序radix sort原理及用法解析

    Java基數(shù)排序radix sort原理及用法解析

    這篇文章主要介紹了Java基數(shù)排序radix sort原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring Cloud Feign簡單使用詳解

    Spring Cloud Feign簡單使用詳解

    本篇文章主要介紹了Spring Cloud Feign簡單使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02

最新評(píng)論

襄城县| 威信县| 金昌市| 平和县| 金华市| 沧州市| 普宁市| 都江堰市| 连平县| 布尔津县| 隆化县| 岑巩县| 安阳县| 江川县| 虞城县| 宁夏| 澎湖县| 拜泉县| 镇赉县| 双流县| 邮箱| 黑山县| 新乡县| 阳山县| 中山市| 深水埗区| 宣城市| 揭阳市| 新田县| 鄂伦春自治旗| 重庆市| 沂水县| 嘉义市| 石嘴山市| 克拉玛依市| 修文县| 巩留县| 平定县| 永泰县| 林周县| 马龙县|