springmvc 는 jquery.form 플러그 인 을 이용 하여 파일 예제 를 비동기 로 업로드 합 니 다.

필요 한 다운로드 파일:
jQuery.form.js
jquery.js
commons-fileupload.jar
commons-io.jar
예시 그림

pom.xml

<!--      --> 
  <dependency> 
   <groupId>commons-fileupload</groupId> 
   <artifactId>commons-fileupload</artifactId> 
   <version>1.3</version> 
  </dependency> 
  <dependency> 
   <groupId>commons-io</groupId> 
   <artifactId>commons-io</artifactId> 
   <version>2.4</version> 
  </dependency> 

웹.xml 업로드 후 중국어 파일 이름 난 장 판 문제 해결

<!--             start --> 
 <filter> 
   <filter-name>Set Character Encoding</filter-name> 
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
   <init-param> 
    <param-name>encoding</param-name> 
    <param-value>UTF-8</param-value> 
   </init-param> 
 </filter> 
 <filter-mapping> 
  <filter-name>Set Character Encoding</filter-name> 
  <url-pattern>/*</url-pattern> 
 </filter-mapping> 
 <!--             end --> 
servlet-context.xml 에 업로드 지원 추가

<!--        --> 
 <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
 </beans:bean> 
jsp 파일

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<% 
 String path = request.getContextPath(); 
 String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path; 
%> 
<html lang="us"> 
<head> 
 <meta charset="utf-8"> 
 <title>springmvc    </title> 
 <link type="text/css" href="<%=basePath%>/resources/css/jquery-ui/jquery-ui.css" rel="stylesheet" /> 
 <link href="<%=basePath%>/resources/themes/bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"> 
</head> 
<body> 
 <h2>springmvc    </h2> 
 <br/> 
 
 
<br/> 
 <div class="alert alert-success" id="formSucc"></div> 
 <br/> 
   
 <form role="form" id="uploadForm" name="uploadForm" enctype="multipart/form-data"> 
  <div class="form-group"> 
    <label>    </label> 
  </div> 
  <div class="form-group"> 
    <label> 
    <input class="form-control" maxlength="30" id="projectName" name="projectName"> 
    </label> 
  </div> 
  <div class="form-group"> 
    <label>File input</label> 
    <input type="file" name="file"> 
   </div> 
   
  <button class="btn" type="button" id="doSave">  </button> 
   
 </form> 
 <div></div> 
</body> 
</html> 
 
 
<script type="text/javascript" src="<%=basePath%>/resources/js/jquery/jquery-1.11.3.min.js"></script> 
<script type="text/javascript" src="<%=basePath%>/resources/js/jquery-ui/jquery-ui.js"></script> 
<script type="text/javascript" src="<%=basePath%>/resources/js/jqueryForm/jquery.form.js"></script> 
<script> 
 
$(function(){ 
 $("#formSucc").hide(); 
  
 $("#doSave").click(function(){ 
  var requestUrl = "<%=basePath%>/widget/saveFile.json"; 
  var projectName = $("#projectName").val(); 
  $("#uploadForm").ajaxSubmit({ 
    type: 'post', 
    url: requestUrl, 
    //data: {projectName: projectName}, //       ,   ,     ,  form            。 
                 //  projectName   "tt",         ,          "tt,tt" 。 
    contentType: "application/x-www-form-urlencoded; charset=utf-8", 
    success: function(data) { 
    if(data.success){ 
     $(".infoTips").remove(); 
     $("#formSucc").show(); 
     $("#formSucc").append("<label class='infoTips'>"+data.message+"</label>"); 
    } 
    } 
  }); 
 }); 
  
}); 
 
 
 
</script> 
자바 의 controller 파일

package com.paincupid.springmvc.widget.controller; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.List; 
 
import javax.annotation.Resource; 
import javax.servlet.http.HttpServletRequest; 
 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
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.bind.annotation.ResponseBody; 
import org.springframework.web.multipart.MultipartFile; 
 
import com.paincupid.springmvc.finance.domain.Finance; 
import com.paincupid.springmvc.test.domain.Person; 
import com.paincupid.springmvc.util.BaseJsonRst; 
import com.paincupid.springmvc.util.CreatMockData; 
 
/** 
 * 
 * @author arthur.paincupid.lee 
 * @since 2016.01.24 
 * 
 */ 
@Controller 
@RequestMapping("/widget") 
public class widgetController { 
 private static final Logger log = LoggerFactory.getLogger(widgetController.class); 
  
 /** 
  *      
  *          : http://localhost:8080/springmvc/widget/uploadFile/view 
  * @return 
  */ 
 @RequestMapping("/uploadFile/view") 
 public String uploadFile() { 
  return "widget/uploadFile"; 
 } 
  
 @ResponseBody 
 @RequestMapping(value="/saveFile", method=RequestMethod.POST) 
 public BaseJsonRst saveFile(@RequestParam MultipartFile file, 
   @RequestParam String projectName) { 
  BaseJsonRst view = new BaseJsonRst(); 
  String orgiginalFileName = ""; 
  try { 
   String fileName = file.getName(); 
   InputStream inputStream = file.getInputStream(); 
   String content = file.getContentType(); 
   orgiginalFileName = file.getOriginalFilename(); 
   log.info("fileName: "+fileName+", inputStream: "+ inputStream 
      +"\r
content: "+content+", orgiginalFileName: ="+ orgiginalFileName +"\r
projectName: "+ projectName); } catch (IOException e) { e.printStackTrace(); } view.setSuccess(true); view.setMessage(" : "+orgiginalFileName+" !"); return view; } }
원본 주소 다운로드:http://xiazai.jb51.net/201701/yuanma/springmvc_jb51.rar
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기