jquery+springMVC实现文件上传
此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台
一. jar包介绍
1. commons-fileupload-1.3.1.jar
二. 相关程序代码
1. applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize">
<value>52428800</value>
</property>
<property name="maxInMemorySize">
<value>2048</value>
</property>
</bean> </beans>
2. TestController.java
package com.ims.web.controller; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON;
import com.ims.common.FileUtil; @Controller
@RequestMapping("test")
public class TestController extends BaseController{ @RequestMapping("view")
public ModelAndView test(){
ModelAndView view = new ModelAndView("test.jsp");
return view;
} @RequestMapping("fileUpload")
public void fileUpload(MultipartHttpServletRequest multipartRequest){
Map<String, Object> result = new HashMap<String, Object>();
try{
MultipartFile uploadFile = multipartRequest.getFile("uploadFile");
String fileName = uploadFile.getOriginalFilename();
String uploadPath = System.getProperty("webapp.root")+"uploadFile\\";
FileUtil.saveFileFromInputStream(uploadFile.getInputStream(),
uploadPath+fileName);
} catch (IOException e) {
result.put("status", STATUS_ERROR);
result.put("message", "文件上传失败");
}
ajax(JSON.toJSONString(result),"text/html");
} }
3. fileUpload.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
<%@ include file="/common/basePath.jsp"%>
</head>
<body>
~~~~~~~~~~~~~~~~~~~~~~文件上传~~~~~~~~~~~~~~~~~~~~~~~~
<br><br>
文件选择:<input type="file" id="uploadFile" style="width: 350px;"/>
<br><br>
<button type="button" onclick="upload();">上传</button>
<br><br><br>
<script type="text/javascript" src="content/js/jquery/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="content/js/jquery-plugin/fileUpload/jquery.ajaxFileUpload.js"></script>
<script type="text/javascript"> function upload(){
$.ajaxFileUpload({
url:rootPath+"/test/file!upload.do",
secureuri:false,
fileElementId: ['uploadFile'],
dataType: 'json',
success: function (data){
},
error: function(data){
}
});
} </script>
</body>
</html>
三. 测试
访问:http://localhost:8080/ims/test/fileUpload.do

选择一文件,点上传,则在 %工程发布目录%\WebContent\uploadFile 下可看到上传的文件
jquery+springMVC实现文件上传的更多相关文章
- SpringMVC+ajax文件上传实例教程
		
原文地址:https://blog.csdn.net/weixin_41092717/article/details/81080152 文件上传文件上传是项目开发中最常见的功能.为了能上传文件,必须将 ...
 - springmvc图片文件上传接口
		
springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...
 - SpringMVC学习--文件上传
		
简介 文件上传是web开发中常见的需求之一,springMVC将文件上传进行了集成,可以方便快捷的进行开发. springmvc中对多部件类型解析 在 页面form中提交enctype="m ...
 - Spring +SpringMVC  实现文件上传功能。。。
		
要实现Spring +SpringMVC 实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...
 - 转: 如何实现jQuery的Ajax文件上传
		
[PHP文件上传] 在开始之前,我觉得是有必要把通WEB上传文件的原理简单说一下的.实际上,在这里不管是PHP,JSP,还是ASP处理上传的文件,其实都是WEB早已把文件上传到服务器了,我们只是运用上 ...
 - jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能
		
 Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文 ...
 - SpringMVC之文件上传异常处理
		
一般情况下,对上传的文件会进行大小的限制.如果超过指定大小时会抛出异常,一般会对异常进行捕获并友好的显示出来.以下用SpringMVC之文件上传进行完善. 首先配置CommonsMultipartRe ...
 - springmvc实现文件上传
		
springmvc实现文件上传 多数文件上传都是通过表单形式提交给后台服务器的,因此,要实现文件上传功能,就需要提供一个文件上传的表单,而该表单就要满足以下3个条件 (1)form表彰的method属 ...
 - 【SpringMVC】文件上传Expected MultipartHttpServletRequest: is a MultipartResolver错误解决
		
本文转载自:https://blog.csdn.net/lzgs_4/article/details/50465617 使用SpringMVC实现文件上传时,后台使用了 MultipartFile类, ...
 
随机推荐
- [Maven实战-许晓斌]-[第二章]-2.4设置HTTP代理
 - 解决wordcloud的一个error:Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
			
环境: 操作系统:Windows 7 64位 语言:Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:17:26) w ...
 - Ionic2文档整理
			
来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...
 - iOS 逆向工程
			
HOOK(钩子函数)在OCD动态化语言中使用swizzle method (交换方法来实现) 实际上静态C语言中的函数也是有办法hook的,这也说明绝对的静态语言是不存在的 Mach-O:对于每个操作 ...
 - nginx高性能WEB服务器系列之九--nginx运维故障日常解决方案
			
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
 - opencv学习笔记(七)---图像金字塔
			
图像金字塔指的是同一图像不同分辨率的子图的集合,有向下取样金字塔,向上取样金字塔,拉普拉斯金字塔....它是图像多尺度表达的一种,最主要的是用于图像的分割 向下取样金字塔指高分辨率图像向低分辨率图像的 ...
 - docker 容器container运行后,如何进入容器内部?
			
docker exec -ti 容器ID /bin/bash
 - sql2008R2新建链接服务器。
			
1:用sql新建链接服务器对象: /****** Object: LinkedServer [pad] Script Date: 10/23/2018 15:47:45 ******/ EXEC ma ...
 - my34_脚本冥等添加自动任务-mysql监控部署
			
场景: 定义一套添加mysql监控的脚本,在mysql安装完毕后,一键执行添加监控 已有以下的等一系列命令可以读取mysql从库的延迟时间并推向influxdb,变化的部分为 -P 端口.-k k ...
 - NestJS  用TypeScript开发 nodeJS后端
			
NestJS A progressive Node.js framework for building efficient and scalable server-side applications ...