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类, ...
随机推荐
- [ActionScript 3.0] 利用InteractivePNG.as类精确选择识别png图片有像素的区域
用法:如果是把png直接导入flash转换成影片剪辑,只需在影片剪辑属性中勾选为ActionScript导出(x),并把基类里的flash.display.MovieClip替换成Interactiv ...
- ArchLinux借助Winetricks-zh安裝WineQQ8.1
Wine是一个在x86.x86-64上容许类Unix操作系统在X Window System下运行Microsoft Windows程序的软件.Wine有另一个非官方名称,"Windows ...
- Binaries和Source、tgz和zip的区别
在下载页面会有2种下载分类,一个是Binaries,一个是source,一般开放原代码软件都会有两个版本发布: Source Distribution 和 Binary Distribution ,二 ...
- scrollto 到指定位置
goTo = function(target){ var scrollT = document.body.scrollTop|| document.documentElement.scrollTop ...
- 基础篇:6.2)形位公差-符号 Symbol
本章目的:了解定义形位公差的符号. 1.公差特征项目的符号(GM新标准) //形位公差共:5类14个,4,2,3,3,2. 2.附加符号(GM新标准) //①基本尺寸(理论尺寸)没有公差,无需检验(不 ...
- Shell输出颜色设置
echo的三个参数 -E 关闭转义(默认) -e 识别转义 -n 不自动输出换行符 Shell识别颜色参数 \033[cor_id1;cor_id2;...]m \033和\e是相同 ...
- reduce的用法
在不增加变量的情况下,统计数组中各元素出现的次数. ```jsfunction countItem (arr) { // 写入你的代码}countItem(['a', 'b', 'a', 'c', ' ...
- (转)CentOS7 搭建LVS+keepalived负载均衡(一)
原文:http://blog.csdn.net/u012852986/article/details/52386306 CentOS7 搭建LVS+keepalived负载均衡(一) CentOS7 ...
- Message Queue基本使用说明
一.安装Message Queue: 在Win7之前,控制面板,添加删除组件(Windows Message Queue). Win7~Win8:控制面板,程序和功能,启用或关闭Windows功能(找 ...
- 理解C#反射
参考文章:http://blog.csdn.net/educast/article/details/2894892 上面的文章将C#反射要用到的方法都给出了,下面我将写个例子,帮助理解 [1.使用反射 ...