程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> springMVC設置裝備擺設情況完成文件上傳和下載

springMVC設置裝備擺設情況完成文件上傳和下載

編輯:關於JAVA

springMVC設置裝備擺設情況完成文件上傳和下載。本站提示廣大學習愛好者:(springMVC設置裝備擺設情況完成文件上傳和下載)文章只能為提供參考,不一定能成為您想要的結果。以下是springMVC設置裝備擺設情況完成文件上傳和下載正文


比來的項目頂用到了文件的上傳和下載功效,我覺著這個功效比擬主要,是以特地把它提掏出來本身停止了測驗考試。

上面就是springMVC設置裝備擺設情況完成文件上傳和下載的詳細步調,供年夜家參考,詳細內容以下

1、 基本設置裝備擺設:

maven導包及設置裝備擺設pom.xml,導包時除開springmvc的基本依附外,須要導入文件上傳下載時用到的commons-io.jsr和commons-fileupload.jar:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
 <modelVersion>4.0.0</modelVersion> 
 <groupId>filLoadTest</groupId> 
 <artifactId>filLoadTest</artifactId> 
 <packaging>war</packaging> 
 <version>0.0.1-SNAPSHOT</version> 
 <name>filLoadTest Maven Webapp</name> 
 <url>http://maven.apache.org</url> 
 <build> 
 <finalName>filLoadTest</finalName> 
 <plugins> 
  <!-- 以下設置裝備擺設可以包管每次強迫更新時jre版本不會變更,那我的eclipse4.4.1,maven3.2.5為例,假如不設置這個,每次強迫更新時jre就會變回1.5 --> 
  <plugin> 
  <artifactId>maven-compiler-plugin</artifactId> 
  <version>2.3.2</version> 
  <configuration> 
   <source>1.7</source> 
   <target>1.7</target> 
   <encoding>UTF-8</encoding> 
   <compilerArguments> 
   <verbose /> 
   <bootclasspath>${java.home}\lib\rt.jar</bootclasspath> 
   </compilerArguments> 
  </configuration> 
  </plugin> 
 </plugins> 
 </build> 
 <dependencies> 
 <dependency> 
 <groupId>junit</groupId> 
 <artifactId>junit</artifactId> 
 <version>3.8.1</version> 
 <scope>test</scope> 
 </dependency> 
 <dependency> 
 <groupId>org.springframework</groupId> 
 <artifactId>spring-webmvc</artifactId> 
 <version>4.0.6.RELEASE</version> 
 </dependency> 
 <dependency> 
 <groupId>com.fasterxml.jackson.core</groupId> 
 <artifactId>jackson-annotations</artifactId> 
 <version>2.2.3</version> 
 </dependency> 
 <dependency> 
 <groupId>com.fasterxml.jackson.core</groupId> 
 <artifactId>jackson-core</artifactId> 
 <version>2.2.3</version> 
 </dependency> 
 <dependency> 
 <groupId>com.fasterxml.jackson.core</groupId> 
 <artifactId>jackson-databind</artifactId> 
 <version>2.2.3</version> 
 </dependency> 
 <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> 
 </dependencies> 
</project> 

web.xml基本設置裝備擺設:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 id="WebApp_ID" version="2.5"> 
 <filter> 
 <description>字符集過濾器</description> 
 <filter-name>encodingFilter</filter-name> 
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
 <init-param> 
  <description>字符集編碼</description> 
  <param-name>encoding</param-name> 
  <param-value>UTF-8</param-value> 
 </init-param> 
 </filter> 
 <filter-mapping> 
 <filter-name>encodingFilter</filter-name> 
 <url-pattern>/*</url-pattern> 
 </filter-mapping> 
 <servlet> 
 <servlet-name>springMVC</servlet-name> 
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 <init-param> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>classpath:spring.xml</param-value> 
 </init-param> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>springMVC</servlet-name> 
 <url-pattern>*.do</url-pattern> 
 </servlet-mapping> 
 <welcome-file-list> 
 <welcome-file>index.html</welcome-file> 
 <welcome-file>index.htm</welcome-file> 
 <welcome-file>index.jsp</welcome-file> 
 <welcome-file>default.html</welcome-file> 
 <welcome-file>default.htm</welcome-file> 
 <welcome-file>default.jsp</welcome-file> 
 </welcome-file-list> 
</web-app> 

spring基本設置裝備擺設spring.xml:

<?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.xsd"> 
 
 
 <context:annotation-config /> 
 <mvc:annotation-driven /> 
 <context:component-scan base-package="controllers" /> 
 
 <!-- 防止IE履行AJAX時,前往JSON湧現下載文件 --> 
 <bean id="mappingJacksonHttpMessageConverter" 
 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
 <property name="supportedMediaTypes"> 
  <list> 
  <value>text/html;charset=utf-8</value> 
  </list> 
 </property> 
 </bean> 
 
 
 <!-- 啟動Spring MVC的注解功效,完成要求和注解POJO的映照 --> 
 <bean 
 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
 <property name="messageConverters"> 
  <list> 
  <ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉換器 --> 
  </list> 
 </property> 
 </bean> 
 
 <!-- 支撐上傳文件 --> 
 <bean id="multipartResolver" 
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > 
 <!-- 100M --> 
 <property name="maxUploadSize" value="104857600"></property> 
 <property name="defaultEncoding" value="utf-8"></property> 
 </bean> 
</beans> 

2、文件上傳的代碼

上傳的簡略頁面index.html,要留意form表單提交時的entype的設置:

<!DOCTYPE html> 
<html> 
<head> 
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 
<body> 
 <form action="./upLoadFile.do" method="post" enctype="multipart/form-data"> 
   選擇文件:<input type="file" name="file"/> 
   <input type="submit" value="提交"/> 
 </form> 
</body> 
</html> 

上傳對應的後台java代碼,詳細成績見正文:

package controllers; 
 
 
import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
import javax.servlet.http.HttpServletRequest; 
import org.apache.commons.io.FileUtils; 
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.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.MultipartFile; 
import org.springframework.web.multipart.MultipartHttpServletRequest; 
import org.springframework.web.multipart.commons.CommonsMultipartResolver; 
 
 
@Controller 
public class fileController { 
 
 
 /** 
 * 文件上傳 
 * 
 * @author:tuzongxun 
 * @Title: upLoadFile 
 * @param @param file 
 * @param @param request 
 * @param @throws IllegalStateException 
 * @param @throws IOException 
 * @return void 
 * @date Apr 28, 2016 1:22:00 PM 
 * @throws 
 */ 
 @RequestMapping(value = "/upLoadFile.do", method = RequestMethod.POST) 
 public void upLoadFile(HttpServletRequest request) 
  throws IllegalStateException, IOException { 
 // @RequestParam("file") MultipartFile file, 
 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( 
  request.getSession().getServletContext()); 
 // 斷定 request 能否有文件上傳,即多部門要求 
 if (multipartResolver.isMultipart(request)) { 
  // 轉換成多部門request 
  MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; 
  // 獲得request中的一切文件名 
  Iterator<String> iter = multiRequest.getFileNames(); 
  while (iter.hasNext()) { 
  // 獲得上傳文件 
  MultipartFile f = multiRequest.getFile(iter.next()); 
  if (f != null) { 
   // 獲得以後上傳文件的文件稱號 
   String myFileName = f.getOriginalFilename(); 
   // 假如稱號不為“”,解釋該文件存在,不然解釋該文件不存在 
   if (myFileName.trim() != "") { 
   // 界說上傳途徑 
   String path = "C:\\Users\\tuzongxun123\\Desktop\\" 
    + myFileName; 
   File localFile = new File(path); 
   f.transferTo(localFile); 
   } 
  } 
  } 
 } 
 } 
} 

被選擇文件提交後,便會看到選中的文件被傳到了代碼中指定的地位,頁面後果圖以下

3、文件下載

文件下載須要獲得下載文件的存儲途徑,這裡只是手動填入,假如是在詳細項目中,可以把文件名和上傳後的存儲途徑保留在數據庫中。然後增長一個文件列表的頁面展現文件名和文件途徑,然後點擊下載的時刻把響應的文件名和途徑傳到後台操作。

/** 
  * 文件下載,須要文件名和文件地址 
  * 
  * @author:tuzongxun 
  * @Title: download 
  * @param@param name 
  * @param@param path 
  * @param@return 
  * @param@throws IOException 
  * @returnResponseEntity<byte[]> 
  * @date Apr 28,2016 1:21:32 PM 
  * @throws 
  */ 
 @RequestMapping(value = "/downLoadFile.do") 
 public ResponseEntity<byte[]> download(@RequestParam("name") String name, 
   @RequestParam("filePath") String path) throws IOException { 
  File file = new File(path); 
  HttpHeaders headers = new HttpHeaders(); 
  String fileName = new String(name.getBytes("UTF-8"), "iso-8859-1");// 為懂得決中文稱號亂碼成績 
  headers.setContentDispositionFormData("attachment", fileName); 
  headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
  return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), 
    headers, HttpStatus.CREATED); 
 } 

Html頁面,這裡只是簡略的填寫文件名和文件途徑,用form表單提交到後台,然後後台會掌握response在頁面彈出保留文件途徑的選擇框,固然了,這裡的後台我沒有做甚麼處置,假如文件不存在是會報錯的,可以停止響應的處置:
文件下載:  

 </br> </br> 
 <form action="./downLoadFile.do" method="post"> 
   文件名:<input type="text" name="name"/></br></br> 
   文件途徑:<input type="text" name="filePath"/></br></br> 
   <input type="submit" value="確認下載"/> 
 </form> 

頁面視圖以下:

假如文件不存在,報錯以下:

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved