controller:

/* #region */

    @RequestMapping(produces = "text/html;charset=UTF-8", value = "/unisi-plugins-core-files/component-async/upload")
@ResponseBody
public String upload(@RequestParam("uname") String uname, @RequestParam MultipartFile[] image_file,
HttpServletRequest request, HttpServletResponse response) throws IOException {
// 可以在上传文件的同时接收其它参数
com.unisi.framework.core.utilities.ConsoleHelper.printOut("收到用户的文件上传请求");
// 如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\upload\\文件夹中
// 这里实现文件上传操作用的是commons.io.FileUtils类,它会自动判断/upload是否存在,不存在会自动创建
String realPath = request.getSession().getServletContext().getRealPath("/upload");
// 设置响应给前台内容的数据格式
response.setContentType("text/plain; charset=UTF-8");
// 设置响应给前台内容的PrintWriter对象
PrintWriter out = response.getWriter();
// 上传文件的原名(即上传前的文件名字)
String originalFilename = null;
// 如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
// 如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且要指定@RequestParam注解
// 上传多个文件时,前台表单中的所有<input
// type="file"/>的name都应该是image_file,否则参数里的image_file无法获取到所有上传的文件
for (MultipartFile myfile : image_file) {
if (myfile.isEmpty()) {
out.print("1`请选择文件后上传");
out.flush();
return null;
} else {
originalFilename = myfile.getOriginalFilename();
com.unisi.framework.core.utilities.ConsoleHelper.printOut("文件原名: " + originalFilename);
com.unisi.framework.core.utilities.ConsoleHelper.printOut("文件名称: " + myfile.getName());
com.unisi.framework.core.utilities.ConsoleHelper.printOut("文件长度: " + myfile.getSize());
com.unisi.framework.core.utilities.ConsoleHelper.printOut("文件类型: " + myfile.getContentType());
try {
// 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
// 此处也可以使用Spring提供的MultipartFile.transferTo(File
// dest)方法实现文件的上传
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, originalFilename));
} catch (IOException e) {
com.unisi.framework.core.utilities.ConsoleHelper
.printOut("文件[" + originalFilename + "]上传失败,堆栈轨迹如下");
e.printStackTrace();
out.print("1`文件上传失败,请重试!");
out.flush();
return null;
}
}
}
// 此时在Windows下输出的是[D:\Develop\apache-tomcat-6.0.36\webapps\AjaxFileUpload\\upload\愤怒的小鸟.jpg]
// System.out.println(realPath + "\\" + originalFilename);
// 此时在Windows下输出的是[/AjaxFileUpload/upload/愤怒的小鸟.jpg]
// System.out.println(request.getContextPath() + "/upload/" +
// originalFilename);
// 不推荐返回[realPath + "\\" + originalFilename]的值
// 因为在Windows下<img src="file:///D:/aa.jpg">能被firefox显示,而<img
// src="D:/aa.jpg">firefox是不认的
out.print("0`" + request.getContextPath() + "/upload/" + originalFilename);
out.flush();
return null;
}
/* #endregion */

springmvc配置文件:

<!-- region upload config -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>
<!-- endregion spring -->

前台:


<script type="text/javascript">
function ajaxFileUpload(){
    //执行上传文件操作的函数
    $.ajaxFileUpload({
        //处理文件上传操作的服务器端地址(可以传参数,已亲测可用)
        url:'地址?uname=玄玉',
        secureuri:false,                       //是否启用安全提交,默认为false
        fileElementId:'image_file',           //文件选择框的id属性
        dataType:'text',                       //服务器返回的格式,可以是json或xml等
        success:function(data, status){        //服务器响应成功时的处理函数
           alert(111);
        },
        error:function(data, status, e){ //服务器响应失败时的处理函数
            alert(222);
        }
    });
}
</script> <input type="file" name="image_file" id="image_file" />
<input value="Upload" type="button" id="upload-form" name="upload-form" onclick="ajaxFileUpload()"/>

当然,除以上配置代码外要引用ajaxfileupload.js。如果还有问题可以留言给我。

springmvc环境下使用ajaxfileupload.js进行文件上传的更多相关文章

  1. Windows环境下用C#编程将文件上传至阿里云OSS笔记

    Windows环境下用C#编程将文件上传至阿里云OSS笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/detai ...

  2. 使用ajaxfileupload.js实现文件上传

    ajaxFileUpload是一个异步上传文件的jQuery插件 语法:$.ajaxFileUpload([options]) options参数说明: 1.url  上传处理程序地址. 2,file ...

  3. jQuery插件AjaxFileUpload实现ajax文件上传

    转自:http://www.cnblogs.com/linjiqin/p/3530848.html jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个 ...

  4. ajaxFileUpload+struts2多文件上传(动态添加文件上传框)

    上一篇文章http://blog.csdn.net/itmyhome1990/article/details/36396291介绍了ajaxfileupload实现多文件上传, 但仅仅是固定的文件个数 ...

  5. jQuery插件AjaxFileUpload实现ajax文件上传时老是运行error方法 问题原因

    今天在用jQuery插件AjaxFileUpload实现ajax文件上传时,遇到一个问题,如图: 老是运行error.无法运行succes方法,追踪ajaxfileupload.js源代码发现: wa ...

  6. js获取文件上传进度

    js获取文件上传进度: <input name="file" id="FileUpload" type="file" /> &l ...

  7. ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)

    在写这篇的时候本来想把标题直接写成报错的提示,如下: “SecurityError:Blocked a frame with origin "http://localhost:55080&q ...

  8. SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】

    Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...

  9. 十九、多文件上传(ajaxFileupload实现多文件上传功能)

    来源于https://www.jb51.net/article/128647.htm 打开google 搜索"ajaxFileupload' ‘多文件上传"可以搜到许许多多类似的, ...

随机推荐

  1. <CentOS7>如何设置hostname

    在CentOS/RHEL 7中,有个叫hostnamectl的命令行工具,它允许你查看或修改与主机名相关的配置: ceph@client-node ~]$ hostnamectlstatus Stat ...

  2. 关于WebService、WebApi的跨域问题

    随着移动互联网的发展, 传统营销模式往网站以及移动客户端转移已经成为一种趋势.接触过互联网开发的开发者肯定会很熟悉两种网络服务WebApi.WebService.在使用JavaScript进行数据交互 ...

  3. 在 ubuntu 下优雅的使用 Sublime Text 3 写 Python

    此文章非技术文,就是一些对于 Sublime 俺之前经常用的 方法(快捷键 )和 工具 有一些工具俺也用过,但是效果不太好,可以说跟shi 一样,可能每个人的用处不一样,咱就不提了,免得招 来口舌之争 ...

  4. single number i && ii && iii

    Problem statement Elementary knowledge: There is a popular question when I seeked my job at Beijing: ...

  5. IOS中的通知NSNotification

    类似于Android中的广播接收者发送者 1.一般完整的通知包含三个属性 -(NSString *)name ;//通知的名称 -(id)object ;//通知发布者(是谁要发布通知) -(NSDi ...

  6. stl string常用函数

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  7. Dockerfile 常用指令 - 每天5分钟玩转 Docker 容器技术(16)

    是时候系统学习 Dockerfile 了.下面列出了 Dockerfile 中最常用的指令,完整列表和说明可参看官方文档. FROM指定 base 镜像. MAINTAINER设置镜像的作者,可以是任 ...

  8. 受够了if (ModelState.IsValid)?ActionFitlter也是一路的坑啊!

    这篇博客真是干货,干得估计还有点“磕牙”,所以还提供视频和代码.但基础稍弱的同学,怕还是得自行补充一些基础知识——就一篇文章,确实没办法面面俱到. 视频和代码下载:Demo - 百度云盘 · 一起帮 ...

  9. Unity中提升像素字体清晰度

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity5.6.0f3 Unity UI系统是非常好的,但默认情况下,使用像像素艺术风格游戏那样需要非常锋利的边框的字体 ...

  10. web.xml报错

    The content of element type "web-app" must match "(icon?,display-name?,description?,d ...