java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

spring mvc(娉ㄨВ)涓婁紶鏂囦欢鐨勭畝鍗曚緥瀛?
杩欐湁鍑犱釜闇€瑕佹敞鎰忕殑鍦版柟
1.form鐨別nctype=”multipart/form-data” 杩欎釜鏄笂浼犳枃浠跺繀椤荤殑
2.applicationContext.xml涓?class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 鍏充簬鏂囦欢涓婁紶鐨勯厤缃笉鑳藉皯
3銆佷翰杩欎袱涓猨ar鍖呬笉鑳藉皯鍝︼紝涔嬪墠灏辨槸鍥犱负灏戜簡杩欎釜涓や釜jar锛屼竴鐩存姤涓€浜涘钁╃殑閿欙紝缁欐垜绱簡涓崐姝粇锛堢増鏈病浠€涔堣姹傦級
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
澶у鍙互鐪嬪叿浣撲唬鐮佸涓?
web.xml
view plain copy
print?


webtest org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-INF/config/applicationContext.xml
/WEB-INF/config/codeifAction.xml


dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation /WEB-INF/config/codeifAction.xml
1



dispatcherServlet
*.do


index.do


applicationContext.xml
view plain copy
print?

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"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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"
default-lazy-init="true">









codeifAction.xml
view plain copy
print?

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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"
default-lazy-init="true">


UploadAction.java
view plain copy
print?
package com.codeif.action;
import java.io.File;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UploadAction {
@RequestMapping(value = "/upload.do")
public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) {
System.out.println("寮€濮?);
String path = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();
// String fileName = new Date().getTime()+".jpg";
System.out.println(path);
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//淇濆瓨
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
return "result";
}
}
index.jsp
view plain copy
print?
<%@ page pageEncoding="utf-8"%>




涓婁紶鍥剧墖



WEB-INF/jsp/涓嬬殑result.jsp
view plain copy
print?
<%@ page pageEncoding="utf-8"%>




涓婁紶缁撴灉


spring mvc(娉ㄨВ)涓婁紶鏂囦欢鐨勭畝鍗曚緥瀛?- 鐖辩▼搴忕綉-飞网

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。