激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - springmvc利用jquery.form插件異步上傳文件示例

springmvc利用jquery.form插件異步上傳文件示例

2020-08-01 15:29paincupid Java教程

本篇文章主要介紹了springmvc利用jquery.form插件異步上傳文件示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

需要的下載文件:

jQuery.form.js

jquery.js

commons-fileupload.jar

commons-io.jar

示例圖片

springmvc利用jquery.form插件異步上傳文件示例

pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
<!-- 文件上傳 -->
  <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>

web.xml 解決上傳后中文文件名亂碼問(wèn)題

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 解決提交時(shí)中文亂碼問(wèn)題 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>
 <!-- 解決提交時(shí)中文亂碼問(wèn)題 end -->

servlet-context.xml中添加對(duì)上傳的支持

?
1
2
3
<!-- 支持文件上傳 -->
 <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 </beans:bean>

jsp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<%@ 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>項(xiàng)目名稱(chēng)</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}, //應(yīng)該把這個(gè)去掉,要不然,值會(huì)有重復(fù),因?yàn)閒orm提交時(shí)已經(jīng)做了一次提交了。
                 //如果projectName的值為"tt",如果這個(gè)地方不去掉,那么提交接收的值變成"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>

Java的controller文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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);
  
 /**
  * 上傳文件
  * 在前臺(tái)的訪問(wèn)路徑為: 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\n content: "+content+", orgiginalFileName: ="+ orgiginalFileName
      +"\r\n projectName: "+ projectName); 
  } catch (IOException e) {
   e.printStackTrace();
  }
  view.setSuccess(true);
  view.setMessage("上傳: "+orgiginalFileName+" 文件成功!");
  return view;
 }
 
  
}

下載源碼地址:springmvc.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/paincupid/article/details/50934222?locationNum=4&fps=1

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 19禁国产精品福利视频 | 又黄又爽免费无遮挡在线观看 | 91精品国产刺激国语对白 | 国产一区二区三区视频观看 | 日韩av在线资源 | 久久国产精品久久久久久久久久 | 欧美一级特黄a | 免费一级高清毛片 | 国产亚洲美女精品久久久2020 | 天天草夜夜 | 精品一区二区亚洲 | 久久999精品 | 操皮视频 | 91成人影院 | av在线播放电影 | 羞羞色在线观看 | 国产亚洲欧美日韩在线观看不卡 | 久久国产精品二国产精品中国洋人 | 视频一区免费观看 | 偿还的影视高清在线观看 | 久久99国产精品久久99果冻传媒 | av电影免费观看 | 亚洲精品 欧美 | 黄污网站在线 | 91久久国产综合久久91猫猫 | 看免费的毛片 | 欧美精品亚洲人成在线观看 | 黄a大片| 视频一区二区国产 | av手机在线免费播放 | 性猛aa久久久 | 日本娇小videos高潮 | 一区二区三区国产好的精 | www.99热视频| 欧美一级黄色免费看 | 中国一级无毛黄色 | 日本xxxx色视频在线观看免费, | 欧美日韩国产成人在线 | 久久精品久久精品国产大片 | 久久成人国产精品 | 狼人狠狠干 |