SpringMVC文件上傳-創(chuàng)新互聯(lián)

SpringMVC上傳文件DEMO

成都創(chuàng)新互聯(lián)致力于做網(wǎng)站、成都做網(wǎng)站,成都網(wǎng)站設(shè)計(jì),集團(tuán)網(wǎng)站建設(shè)等服務(wù)標(biāo)準(zhǔn)化,推過標(biāo)準(zhǔn)化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務(wù)水平進(jìn)行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場(chǎng)競(jìng)爭(zhēng)中脫穎而出。 選擇成都創(chuàng)新互聯(lián),就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務(wù)!

    SpringMVC為文件上傳提供了直接的支持,這種支持是即插即用的MultipartResolver實(shí)現(xiàn)的。SpringMVC使用Apache Commons FileUpload技術(shù)實(shí)現(xiàn)了一個(gè)MultipartResolver實(shí)現(xiàn)類:CommonsMultipartResolver.因此SpringMVC的文件上傳需要依賴ApacheCommons FileUpload的組件(commons-io-2.5.jar以及commons-fileupload-1.3.2.jar)。

    1.編寫jsp

    需要注意的點(diǎn):表單的method必須為post方式,負(fù)責(zé)上傳文件的表單編碼類型必須是“multipart/form-data"。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springMVC文件上傳</title>
</head>
<body>
	<h2>文件上傳</h2>
	<form action="upload" enctype="multipart/form-data" method="post">
		<table>
			<tr>
				<td>文件描述:</td>
				<td><input type="text" name="description"></td>
			</tr>
			<tr>
				<td>請(qǐng)選擇文件</td>
				<td><input type="file" name="file"/></td>
			</tr>
			<tr>
				<td>
					<input type="submit" value="上傳"/>
				</td>
			</tr>		
		</table>
	
	</form>
</body>
</html>

    2.編寫Controller

package com.cn.controller;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
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;

@Controller
public class FileUploadController {
	@RequestMapping(value="uploadForm")
	public String uploadForm() {
		return "uploadForm";
	}
	@RequestMapping(value="upload",method=RequestMethod.POST)
	public String upload(HttpServletRequest request, @RequestParam("description") String description,
			@RequestParam("file") MultipartFile file) throws Exception{
		System.out.println(description);
		if(!file.isEmpty()) {
		        //上傳文件的路徑
			String path = request.getServletContext().getRealPath("/p_w_picpaths/");                       //上傳文件名
			String fileName = file.getOriginalFilename();
			File filePath = new File(path,fileName);
			if(!filePath.getParentFile().exists()) {
				filePath.getParentFile().mkdirs();
			}
			//將文件保存到目標(biāo)文件中
			file.transferTo(new File(path + File.separator + fileName));
			return "success";
		}else{
			return "error";
		}
				
		
	}
}

    3.配置springmvc-config.xml

    這里需要配置MultipartResolver。需要注意的是:請(qǐng)求的編碼格式必須和jsp中的pageEncoding屬性一致,以便正確讀取表單的內(nèi)容,默認(rèn)值為“iso8859-1”。

<?xml version="1.0"
 encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop    
    	http://www.springframework.org/schema/aop/spring-aop-4.2.xsd   
    	http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		"> 
	<!-- Spring 可以自動(dòng)去掃描base-pack下面的包或者子包下面的java文件 -->
	<!-- 如果掃描到spring相關(guān)的注解類,將其注冊(cè)為spring的bean -->
	<context:component-scan base-package="com.cn.controller" />
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/content/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>		
		</property>
	</bean>
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize">
			<value>10485760</value>
		</property>
		<property name="defaultEncoding">
			<value>UTF-8</value>		
		</property>
	
	
	</bean>
	
</beans>

    4.配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVCTest</display-name>
  <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>
  
  <!-- 定義Spring MVC前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/springmvc-config.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 讓Spring MVC的前端控制器攔截所有請(qǐng)求 -->
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

分享題目:SpringMVC文件上傳-創(chuàng)新互聯(lián)
文章來源:http://bm7419.com/article32/gdepc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、自適應(yīng)網(wǎng)站、網(wǎng)站營銷、域名注冊(cè)、外貿(mào)建站品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名