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类, ...
随机推荐
- linux 查看进程所在目录
一下内容转自:https://blog.csdn.net/spring21st/article/details/50561550 通过 ps 及 top 命令查看进程信息时,只能查到 相对路径,查不到 ...
- [ActionScript 3.0] AS3实现3D旋转
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Display ...
- [Swift]字符串大小写转换,同时实现本地化或设置语言环境
在NSString中提供了3种字符串大小写转换方式:1. 转换字符串大小写2. 转换字符串大小写,并实现本地化3. 转换字符串大小写,并设置语言环境. 一. 转换字符串大小写如果只是想单纯的将字符串进 ...
- [Swift]堆栈Stack的两种版本:(1)用类包装Stack (2)用泛型包装Stack
堆栈是一个在计算机科学中经常使用的抽象数据类型.堆栈中的物体具有一个特性: 最后一个放入堆栈中的物体总是被最先拿出来, 这个特性通常称为后进先出(LIFO)队列. 堆栈中定义了一些操作. 两个最重要的 ...
- jquery 实现省市二级联动
效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- (STM32F4) IAP程式碼實現
IAP學習, 主要想了解實際上程式碼放在不同的Flash位置如何轉跳且執行. 我的應用程序只做了Pin12, Pin13 LED閃爍來分辨我的 App1 跟 App2的程式碼 App1 程式碼 int ...
- java面向对象概念1
一.java面向对象学习的三条主线: 1.java类及类的成员:属性.方法.构造器:代码块.内部类 2.面向对象的三大特征:封装性.继承性.多态性.(抽象性) 3.其它关键字:this.super.s ...
- Docker安装Odoo实现快速迁移(四)
一. 安装postgres数据库 root@ubuntu-:~# docker run -d -e POSTGRES_USER=odoo -e POSTGRES_PASSWORD=odoo --nam ...
- pip安装python库总是超时或出错的解决办法
建个文件 ~/.pip/pip.conf, 内容如下 [global] timeout = index-url = http://pypi.douban.com/simple/ [install] u ...
- 洛谷 P4307 [JSOI2009]球队收益 / 球队预算(最小费用最大流)
题面 luogu 题解 最小费用最大流 先假设剩下\(m\)场比赛,双方全输. 考虑\(i\)赢一局的贡献 \(C_i*(a_i+1)^2+D_i*(b_i-1)^2-C_i*a_i^2-D_i*b_ ...