Springmvc的檔案上傳功能實現
如何在用Springmvc實現檔案的上傳功能,在這裡做一個詳細的解答:
1.新建Web專案,匯入相關jar包。
2.在src包下配置springmvc的配置檔案springmvc.xml
①.配置檔案頭,加入context和mvc標籤:
②.利用context標籤中的這個方法來掃描指定包下面的檔案<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<context:component-scan base-package="">
③.之後的程式碼:<mvc:annotation-driven/> <mvc:resources location="/pic/" mapping="/resource/*"/> /*配置檢視解析器*/ <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> //字首名 <property name="prefix" value="/"/> //字尾名 <property name="suffix" value=".jsp"/> </bean> /*配置MultipartResolver用來處理檔案上傳*/ <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> //設定上傳檔案最大大小 <property name="maxUploadSize" value="100000"/> </bean> </beans>
3.編寫檔案類,該類實現Serializable介面,私有屬性:檔名,MultipartFile。獲取get&set方法以及無參構造和有參構造方法。
4.配置完畢後在com.controller包下編寫控制器java類
①.在類的前面用註解寫入Controller以及RequestMapping的返回值:
@Controller
@RequestMapping("/upload")
②.在類中定義上傳方法並同樣在方法前用註解寫入RequestMapping的二級返回值,在這個方法的引數列表中:@RequestParam("pic") MultipartFile pic 接收前臺傳過來的圖片@RequestMapping("/uploadPic") public String uploadPic(@RequestParam("pic") MultipartFile pic,String title,HttpServletRequest request)
pic.transferTo(File file);將pic相當於copy到另外一個檔案中
③.將檔案上傳到相應目錄,構建要coye的路徑,
用getOriginalFilename()拿到檔案的名字
④.利用MultipartFile的transferTo()方法將pic複製到另外一個檔案中
⑤.new一個HttpSession的物件來接收request.getSession()並新建一個泛型為Photo的List集合,使用new出的session物件來獲取Attribute中這個list集合(強轉),如果集合為空,則新建一個ArrayList集合,並用add方法把檔案加入進去。
⑥.使用session設定Attritube(list集合),最後返回頁面名稱。
5.根據自己需要編寫jsp頁面(如圖片檔案可以編寫jsp頁面實現展示圖片的功能)
相關推薦
Springmvc的檔案上傳功能實現
如何在用Springmvc實現檔案的上傳功能,在這裡做一個詳細的解答:1.新建Web專案,匯入相關jar包。2.在src包下配置springmvc的配置檔案springmvc.xml ①.配置檔案頭,加入context和mvc標籤:<?xml ve
SpringMVC之檔案上傳功能實現
在客戶端傳送上傳檔案請求時,一般會與普通請求一樣,將請求傳送給SpringMVC的前端控制器DispatcherServlet,然後由DispatcherServlet呼叫HandlerMapping找到處理該請求的Controller。然後Dispatch
WebApi的檔案上傳功能實現
1.自主宿主HttpSelfHost的實現 #region Web Api監聽 Assembly.Load("Lind.DDD.TestApi"); //手工載入某個api程式集的controller var c
SpringMVC + ueditor + 七牛 實現富文字檔案上傳功能
1.下載ueditor開發包(原始碼以及JSP版本UTF-8版),並匯入專案中,也可以匯入jar包。 2.pom 新增相關依賴 3.解壓並將原始碼拷貝到專案中: 解壓後原始碼如下圖: 拷貝至專案中: 4. 匯入控制元件 5.修改配置檔案 此方法對應後臺方
WebUploader+SpringMVC實現檔案上傳功能
官方文件及更多示例請參考: http://fex.baidu.com/webuploader/ 不扯廢話了,由於我需要的只是上傳圖片功能,官網上邊還說“WebUploader只包含檔案上傳的底層實現,不包括UI部分,所以互動方面可以自由發揮。”但是我又看到官網的例子不錯,就把demo的js和css扒了下
springMVC圖片檔案上傳功能的實現
在工程依賴庫下新增檔案上傳jar包commons-fileupload-1.2.2.jarcommons-io-2.4.jar2.jsp頁面設定form表單屬性enctype在表單中上傳圖片時,需要在form的屬性設定中新增enctype="multipart/form-da
原生javascript實現檔案上傳功能程式碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, ini
【SSH網上商城專案實戰13】Struts2實現檔案上傳功能
轉自:https://blog.csdn.net/eson_15/article/details/51366384 上一節我們做完了新增和更新商品的功能,這兩個部分裡有涉及到商品圖片的上傳,並沒有詳細解說。為此,這篇文章詳細介紹一下Struts2實現檔案上傳的功能。 1
檔案上傳功能的實現
前提:拷入兩個包: 一、Jsp頁面設定: 製作表單: <form action="${pageContext.request.contextPath}/AddProdServlet" method="post"enctype="multipart/form-data">
Spring MVC實現MultipartFile檔案上傳功能
前端 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改資訊</title> </head> <body
django檔案上傳功能的實現
Y11 1、 建立新app,配置路由和settings(Y10部落格有具體操作) 2、建立一個上傳頁面(html),在views中渲染頁面,配置路由,配置css,引入css,引入共享檔案jquery 3、建立common_static共享檔案目錄,templates/app前端
Django - 實現簡單的檔案上傳功能
前端 <form action="" method="post" enctype="multipart/form-data"> {#<form action="" method="post" enctype="application/x-www-form-urlenc
Atitit 檔案上傳功能的實現 圖片 視訊 目錄 1. 上傳原理 1 1.1. http post編碼 multipart / form-data 1 1.2. 臨時檔案模式 最簡單 2 1.3
Atitit 檔案上傳功能的實現 圖片 視訊 目錄 1. 上傳原理 1 1.1. http post編碼 multipart / form-data 1 1.2. 臨時檔案模式 最簡單 2 1.3. 位元組陣列模式 簡單 2
C#實現大檔案上傳功能(二)---webuploader上傳
一、問題來源 近些時候,處理專案的時候發現如果使用者上傳大檔案的時候使用HtmlInputFile通過httppostfilebase 來實現上傳,如果檔案較小的話較小的話,上傳沒有問題(4M)
利用拖拽效果實現檔案上傳功能
1、獲取檔案的方法file物件是來自使用者在一個<input>表單元素上選擇檔案後返回的filelist物件,也可以是來自自由拖拽操作生成的dataTransfer物件,dataTransf
php實現檔案上傳功能
無聊的很 就來寫個部落格吧 這次來簡單寫下 php 怎麼實現上傳檔案 廢話不多說 開搞! 這是index檔案 1 <form action="upload_file.php" method="post" enctype="multipart/form-d
實現HTTP協議Get、Post和檔案上傳功能——使用libcurl介面實現
之前我們已經詳細介紹了WinHttp介面如何實現Http的相關功能。本文我將主要講解如何使用libcurl庫去實現相關功能。(轉載請指明出於breaksoftware的csdn部落格) libcurl在http://curl.haxx.se/
框架中如何根據fileupload工具包實現檔案上傳功能
工具包 Apache-fileupload.jar – 檔案上傳核心包。 Apache-commons-io.jar – 這個包是fileupload的依賴包。同時又是一個工具包。 程式碼 servletFileUpload官方API /*獲取tomcat的wor
用ASP.NET的FileUpload控制元件實現帶對話方塊的FTP檔案上傳功能
最近不得已開始做網頁,一個ASP.NET的專案,需要實現FTP上傳功能,於是上網廣搜程式碼,發現FTP上傳的程式碼到處都是,隨手拈來,可是一般來說客戶端上傳檔案都需要一個檔案選擇對話方塊,正好自帶的FileUpload控制元件能夠實現。但是網上搜到FileUpl
django實戰(三)實現簡單的檔案上傳功能
今天利用Django實現一個簡單的檔案上傳功能。各種軟體版本:Django:1.10.2+Python3.6+pycharm2016.1.4。 實現步驟: 1)建立專案Django_upload:dja