一、前端页面

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实现上传图片的更多相关文章

  1. spring mvc 的上传图片是怎么实现的?

    spring mvc 的上传图片是怎么实现的? 导入jar包,commons-io.jar 及 commons-fileupload.jar 在springmvc的配置文件中配置Mutipart解析器 ...

  2. jquery调用spring mvc接口返回字符串匹配

    背景:有个增删改页面,用jquery祭出ajax异步调用接口,spring mvc响应对象是个json字符串,jquery根据响应结果判断,如果删除成功给出提示.那么问题来了,接口里响应的字符串怎么匹 ...

  3. spring mvc 在上传图片时,浏览器报The request sent by the client was syntactically incorrect

    项目中,在一个jsp页面里其它图片上传是功能是可以使用的,当我自己新加了一个图片上传时,提交表单后,浏览器报The request sent by the client was syntactical ...

  4. spring mvc做上传图片,文件小于10k就不生成临时文件了

    这是spring-mvc.xml中的 <bean id="multipartResolver" class="org.springframework.web.mul ...

  5. spring MVC框架入门(外加SSM整合)

    spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...

  6. 在php中使用jquery uploadify进行多图片上传

    jquery uploadify是一款Ajax风格的批量图片上传插件,在PHP中使用jquery uploadify很方便,请按照本文介绍的方法和步骤,为你的PHP程序增加jquery uploadi ...

  7. MVC中使用jquery uploadify上传图片报302错误

    使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误.原来更新了fl ...

  8. JQuery文件上传插件uploadify在MVC中Session丢失的解决方案

    <script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthenticati ...

  9. jquery.uploadify上传文件配置详解(asp.net mvc)

    页面源码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

随机推荐

  1. Object类—复写equals方法,hashCode方法,toString方法

    Object:所有类的根类. Object是不断抽取而来,具备着所有对象都具备的共性内容. class Person extends Object { private int age; Person( ...

  2. drcom 不耍流氓

    最近在研究PPPoE,于是做出了一个可以使用WIFI的客户端. 本软件只可以使用于学习交流,不可以使用于商业用途. 本软件在广工西区测试可以使用,软件开启360wifi可以使用.因为心跳包可能修改,本 ...

  3. win10 uwp 分治法

    其实我想说Path,因为最近在做一个简单的分治. 算法涉及到了一个平面几何的知识.就是三角形p1p2p3的面积等于以下行列式的二分之一: 而且当点P3 在射线P1P2的左侧的时候,表达式为正,右侧表达 ...

  4. 前端工程化grunt

    1.grunt是什么? grunt是基于nodejs的前端构建工具.grunt用于解决前端开发的工程问题. 2.安装nodejs Grunt和所有grunt插件都是基于nodejs来运行的. 安装了n ...

  5. 神经网络JOONE的实践

    什么是joone Joone是一个免费的神经网络框架来创建,训练和测试人造神经网络.目标是为最热门的Java技术创造一个强大的环境,为热情和专业的用户. Joone由一个中央引擎组成,这是Joone开 ...

  6. java统计英文字母、空格、数字和其它字符的数目

    package tes; import java.util.Scanner; //java统计英文字母,空格,数字和其它字符的数目 public class ZiFuTongJi { public s ...

  7. (转)log4j使用介绍

    原文出自: log4j使用介绍 日志是应用软件中不可缺少的部分,Apache的开源项目Log4j是一个功能强大的日志组件,提供方便的日志记录.以下是个人经验,具体请参考Log4j文档指南. Log4j ...

  8. 运用El表达式截取字符串/获取list的长度

    ${fn:substring(wjcd.lrsj, 0, 16)} 使用functions函数来获取list的长度 ${fn:length(list)} 引入 <%@ taglib prefix ...

  9. thinkphp3.2开发网页实现第三方登录

    1.在要添加登录的html里添加登录按钮: <a href="{:U('Public/login/',array('type'=>'weixin'))}">< ...

  10. WPF获得全局窗体句柄,并响应全局键盘事件

    场景 wpf窗体运行后,只能捕获当前Active窗体的按键事件,如果要监听windows全局事件,并对当前窗口事件响应. 第一步:导入Winows API public class Win32 { [ ...