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>
web.xml 解決上傳后中文文件名亂碼問(wèn)題
<!-- 解決提交時(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ì)上傳的支持
<!-- 支持文件上傳 --> <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>項(xià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文件
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;
}
}
下載源碼地址:http://xiazai.jb51.net/201701/yuanma/springmvc_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java?redis中緩存穿透?緩存擊穿?雪崩三種現(xiàn)象以及解決方法
緩存穿透是指緩存和數(shù)據(jù)庫(kù)中都沒有的數(shù)據(jù),而用戶不斷發(fā)起請(qǐng)求,如發(fā)起為id為“-1”的數(shù)據(jù)或id為特別大不存在的數(shù)據(jù)。這時(shí)的用戶很可能是攻擊者,攻擊會(huì)導(dǎo)致數(shù)據(jù)庫(kù)壓力過(guò)大2022-01-01
使用hibernate和struts2實(shí)現(xiàn)分頁(yè)功能的示例
本篇文章主要介紹了使用hibernate和struts2實(shí)現(xiàn)分頁(yè)功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Java 中的HashMap詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java 中的HashMap詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-05-05
Java實(shí)現(xiàn)文件分割和文件合并實(shí)例
本篇文章主要介紹了Java實(shí)現(xiàn)文件分割和文件合并實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
dubbo?filter中有關(guān)bean注入和配置文件讀取方式
這篇文章主要介紹了dubbo?filter中有關(guān)bean注入和配置文件讀取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot詳解整合MyBatis過(guò)程中可能遇到的問(wèn)題
因?yàn)镾pring Boot框架開發(fā)的便利性,所以實(shí)現(xiàn)Spring Boot與數(shù)據(jù)訪問(wèn)層框架(例如MyBatis)的整合非常簡(jiǎn)單,主要是引入對(duì)應(yīng)的依賴啟動(dòng)器,并進(jìn)行數(shù)據(jù)庫(kù)相關(guān)參數(shù)設(shè)置即可2022-07-07

