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 ...
随机推荐
- 彻底区分html的attribute与dom的property
当初在学html时始终没有弄清楚的关于attribute与property的区别,竟然在看angular文档时弄明白了. angular官方文档的数据绑定一节提到html attribute与dom ...
- ASP.NET Web API 2中的错误处理
前几天在webapi项目中遇到一个问题:Controller构造函数中抛出异常时全局过滤器捕获不到,于是网搜一把写下这篇博客作为总结. HttpResponseException 通常在WebAPI的 ...
- linux命令综合
查找文件中指定字符串并且高亮显示: find .|xargs grep --color=auto "hello" dos下查找: netstat -ano|findstr &quo ...
- Nginx功能展示实验
Nginx功能展示实验 Nging可以作为反代服务器:也可以作为负载均衡器,并自带根据对后端服务器健康状态检测具有增删服务器的功能:也可以作为纯Web服务器,提供Web服务. 本实验将使用Nginx实 ...
- ubuntu软件使用汇总
ubuntu使用合集 安装Ubuntu时的硬盘分区 Linux入门(1)--Ubuntu16.04安装搜狗拼音 Linux入门(2)--Ubuntu16.04安装wineQQ Linux入门(3)-- ...
- 【转】scatterlist && DMA
原文:scatterlist && DMA DMA是一种无须CPU的参与就可以让外设与系统内存之间进行双向数据传输的硬件机制.使用DMA可以是系统CPU从实际的IO数据传输过程中摆脱出 ...
- 从狗日的Oracle上下载jdk
就算因为需要我要用到java,我依然要说java是最垃圾的语言,现在oracle又让我明白什么叫最垃圾的公司. 从oracle下载文件要求你同意他的协议,但是你会发现很多时候就算你点了同意依然不可以下 ...
- Python 数据分析Windows环境搭建
1. 下载相应的Python软件并安装 python-3.6.0-amd64 2. 配置相应的环境变量path ;C:\Users\Administrator\AppData\Local\Progr ...
- Leetcode题解(七)
24.Swap Nodes in Pairs 题目 看到此题,第一想法是利用两个指针,分别将其所指向的节点的value交换.然后同时向后移动2个节点,代码如下: struct ListNode { i ...
- AngularJS学习篇(二)
AngularJS 指令 AngularJS 通过被称为 指令 的新属性来扩展 HTML. AngularJS 通过内置的指令来为应用添加功能. AngularJS 允许你自定义指令. Angular ...