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

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

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

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

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

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

必須在spring-mvc.xml文件

<!-- (2)配置 MultipartResolver 實現(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"/>
?? ? ? ?<!--內存中最大 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 -->

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

(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) {
?? ??? ?// 獲取上傳的服務器路徑
?? ??? ?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)效果

選擇圖片路徑:

單擊上傳:

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

(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;
?? ??? ?// 獲得多個文件
?? ??? ?Map<String, MultipartFile> map = multipartRequest.getFileMap();
?
?? ??? ?// 獲取上傳的服務器路徑
?? ??? ?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();
?? ??? ??? ?}
?
?? ??? ?}
?? ??? ?// 保存每個上傳的路徑
?? ??? ?request.setAttribute("files", list);
?? ??? ?
?? ??? ?return "showUpload"; ? //跳轉到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)效果

單擊上傳:

查看上傳到服務器的圖片

五、下載圖片

 (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) {
?
?? ??? ?// 設置響應編碼
?? ??? ?response.setContentType("text/html;charset=utf-8");
?
?? ??? ?// 設置請求編碼
?? ??? ?try {
?? ??? ??? ?request.setCharacterEncoding("utf-8");
?? ??? ?} catch (UnsupportedEncodingException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?
?? ??? ?// 字節(jié)流
?? ??? ?BufferedInputStream bis=null;
?? ??? ?BufferedOutputStream bos=null;
?
?? ??? ?// 獲取服務器的路徑
?? ??? ?String path = request.getSession().getServletContext().getRealPath("/upload/");
?
?? ??? ?// 下載的路徑
?? ??? ?String downPath = path +"/"+ fileName;
?
?? ??? ?try {
?? ??? ??? ?// 文件大小
?? ??? ??? ?long fileSize = new File(downPath).length();
?? ??? ??? ?
?? ??? ??? ?//設置內容類型
?? ??? ??? ?response.setContentType("application/x-msdownload");
?? ??? ??? ?//設置頭信息
?? ??? ??? ?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{
?? ??? ??? ?//關閉連接
?? ??? ??? ? 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)效果

保存或打開如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 深入理解Java定時調度(Timer)機制

    深入理解Java定時調度(Timer)機制

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

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

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

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

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

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

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

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

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

    java random.nextInt的坑及解決

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

    Java容器ArrayList知識點總結

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

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

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

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

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

    Spring Cloud Feign簡單使用詳解

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

最新評論

江都市| 巴林左旗| 庄河市| 柳河县| 宿迁市| 锡林浩特市| 佳木斯市| 子洲县| 巨鹿县| 沐川县| 鲜城| 泰兴市| 娱乐| 鄂尔多斯市| 通城县| 林周县| 荣昌县| 衡南县| 博兴县| 上高县| 和平区| 华池县| 吴堡县| 广丰县| 桦川县| 稷山县| 察隅县| 延津县| 资源县| 醴陵市| 朝阳县| 武功县| 根河市| 柳江县| 廊坊市| 泸水县| 嘉定区| 永善县| 犍为县| 鸡泽县| 都昌县|