jquery.uploadify+spring mvc实现上传图片
一、前端页面
1.下载jquery.uploadify
去uploadify官网(http://www.uploadify.com/download/)下载压缩包,解压后放在如下路径:

2.html结构
form表单的上传控件部分:
<div class="control-group">
<label class="control-label" for="coverImage">代表图</label>
<div class="controls">
<input type="hidden" th:field="*{coverImage}">
<input class="input-file" id="fileInput" type="file">
<img id="previewCoverImage" src="#">
</div>
</div>
3.页面引入uploadify
<link rel="stylesheet" th:href="@{/static/uploadify/uploadify.css}”>
<script type="text/javascript" th:src="@{/static/uploadify/jquery.uploadify.js}"></script>
4.自定义上传代码
<script th:inline="javascript">
/*<![CDATA[*/
$(document).ready(function () {
$("#fileInput").uploadify(
{
'swf': /*[[@{/static/uploadify/uploadify.swf}]]*/,
'uploader': /*[[@{/upload/uploadCoverImage}]]*/, //后台action地址
'queueID': 'fileQueue',
'auto': true,
'multi': false,
'buttonText': '上传图片',
'fileObjName': 'pic', //对应action中的参数字段名称
'width': 70,
'height': 20,
'onUploadSuccess': function (file, data, response) {
if (data != null) {
$("#coverImage").val(data); //赋值给hidden控件,便于提交form表单
$("#previewCoverImage").attr("src",data); //复制给img控件用来预览
}
}
});
});
/*]]>*/ </script>
二、站点配置
1.调整springmvc-servlet.xml文件,添加配置支持文件上传
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
2.添加maven依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
三、后台代码
1.controller
@Controller
@RequestMapping("/upload")
public class UploadController { @RequestMapping(value = "/uploadCoverImage", method = RequestMethod.POST)
@ResponseBody
public String uploadCoverImage(@RequestParam("pic") CommonsMultipartFile pic, HttpServletRequest req, HttpServletResponse response) throws IOException {
//上传文件信息
String fileName = pic.getOriginalFilename();
String fileType = fileName.split("[.]")[1]; //生成文件信息
String filePath = req.getSession().getServletContext().getRealPath(FilePathConst.COVER_IMAGE_UPLOAD);
String uuid = UUID.randomUUID().toString().replace("-", "");
String uuidFileName = uuid + fileName; //保存文件
File f = new File(filePath + "/" + uuid + "." + fileType);
FileUtils.uploadFile(pic.getInputStream(), uuidFileName, filePath); return SiteConst.SITE_DOMAIN + FilePathConst.COVER_IMAGE_UPLOAD + uuidFileName;
}
}
2.FileUtils工具类
public class FileUtils {
public static void uploadFile(InputStream is, String fileName, String filePath) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
File f = new File(filePath + "/" + fileName);
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
byte[] bt = new byte[4096];
int len;
while ((len = bis.read(bt)) > 0) {
bos.write(bt, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bos) {
bos.close();
bos = null;
}
if (null != fos) {
fos.close();
fos = null;
}
if (null != is) {
is.close();
is = null;
}
if (null != bis) {
bis.close();
bis = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
jquery.uploadify+spring mvc实现上传图片的更多相关文章
- spring mvc 的上传图片是怎么实现的?
spring mvc 的上传图片是怎么实现的? 导入jar包,commons-io.jar 及 commons-fileupload.jar 在springmvc的配置文件中配置Mutipart解析器 ...
- jquery调用spring mvc接口返回字符串匹配
背景:有个增删改页面,用jquery祭出ajax异步调用接口,spring mvc响应对象是个json字符串,jquery根据响应结果判断,如果删除成功给出提示.那么问题来了,接口里响应的字符串怎么匹 ...
- spring mvc 在上传图片时,浏览器报The request sent by the client was syntactically incorrect
项目中,在一个jsp页面里其它图片上传是功能是可以使用的,当我自己新加了一个图片上传时,提交表单后,浏览器报The request sent by the client was syntactical ...
- spring mvc做上传图片,文件小于10k就不生成临时文件了
这是spring-mvc.xml中的 <bean id="multipartResolver" class="org.springframework.web.mul ...
- spring MVC框架入门(外加SSM整合)
spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...
- 在php中使用jquery uploadify进行多图片上传
jquery uploadify是一款Ajax风格的批量图片上传插件,在PHP中使用jquery uploadify很方便,请按照本文介绍的方法和步骤,为你的PHP程序增加jquery uploadi ...
- MVC中使用jquery uploadify上传图片报302错误
使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误.原来更新了fl ...
- JQuery文件上传插件uploadify在MVC中Session丢失的解决方案
<script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthenticati ...
- jquery.uploadify上传文件配置详解(asp.net mvc)
页面源码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
随机推荐
- 微信公众号第三方 推送component_verify_ticket协议
整了一天,终于弄明白了 component_verify_ticket 怎么获取的了.在此先批一下微信公众号平台,文档又没写清楚,又没有客服,想搞哪样哈! 好,回归正题. 第一,先通过开发者资质认证, ...
- C#委托
关于什么是委托,委托如何使用,我在这里就不说了. 需要说的: 委托是函数指针链 委托的 BeginInvoke 委托如果出现异常,会如何 如果不知道函数指针,可以继续往下看,我来告诉大家,为何需要委托 ...
- 使用getCurrentPosition方法实时获取当前Geolocation信息(赋源码文件)--html5、JavaScript
使用getCurrentPosition方法实时获取当前Geolocation信息: 1.typeof 运算符返回一个用来表示表达式的数据类型的字符串 <script> alert(typ ...
- 怎用不用工具创建一个javaWeb项目
整体步骤是: 1: 新建一个文件夹: 以项目名称命名 2:在新建的文件夹下再新建一个文件夹 WEB-INF (注意:大小写) 和 一个文件 index.jsp : index.jsp里面的代码: ...
- 《剑指Offer》面试题5-替换空格
题目:请实现一个函数,把字符串中的每个空格替换成"%20".例如输入"We are happy.",则输出"We%20are%20happy.&quo ...
- 对ajax请求的简单封装,操作更方便
我这里的接口数据调用的js叫interface.js,接口路径管理的js叫webSiteControl.js /** * Created by l2776 on 2017/7/11. * 接口数据调用 ...
- PHP设计模式三:原型设计模式
一.什么是原型设计模式 原型设计模式使用一种克隆技术来复制实例化的对象,新对象是通过复制原型实例创建的.原型设计模式的目的是通过使用克隆以减少 实例化对象的开销. 在原型设计模式中,Client类是不 ...
- C# Request.InputStream 读取输入流为空的原因处理
今天在手机App测试接口的时候发现一个通过POST方式的接口 获取body中的参数一直为空,但是在数据量小的时候却可以获取到数据,开始怀疑是不是POST的长度有限制,然后在web.config中修改了 ...
- Coredata中的多线程
=================== 疑问: 1.coredata是什么?结构 2.如果在简单的demo中,我们可以在主线程中使用coredata.但是如果在真正的大项目中,这样可行么? 3.假设都 ...
- IT连创业系列:说说苹果商店AppStore上架App应用前后遇到的那些神坑
前言: IT连创业的这个系列,又隔空了一个多月了. 不知道为什么,最近写文的冲动感下降了很多,如果不是因为特别忙,大概就因为上了年纪的原因了. 群里关注我创业的朋友,一直都在问,啥时候有新的文章讲述创 ...