Spring mvc,uploadifive 文件上传实践(转自:https://segmentfault.com/a/1190000004503262)
来源:互联网 发布:美国富豪裸捐 知乎 编辑:程序博客网 时间:2024/05/16 18:42
1、前台页面: 引入js和css
<link type="text/css" rel="stylesheet" href="<%=basePath%>static/js/uploadifive/uploadifive.css" /><script type="text/javascript" src="<%=basePath%>static/js/uploadifive/jquery.min.js"></script><script type="text/javascript" src="<%=basePath%>static/js/uploadifive/jquery.uploadifive.min.js"></script>
2、控件初始化:
<script type="text/javascript"> $(function() { $('#file_upload').uploadifive({ 'auto' : false, 'buttonClass': 'btn btn-primary', 'buttonText': "选择文件", 'queueID' : 'uploadqueue', 'uploadScript' : '/tools/uploadfiles', 'onUploadComplete' : function(file, data) { console.log(data); } }); });</script>
3、body页面内容:
<div id="uploadqueue"><input id="file_upload" name="file_upload" type="file" multiple="true"><a href="javascript:$('#file_upload').uploadifive('upload')">上传文件</a> | <a href="javascript:$('#file_upload').uploadifive('stop')">停止上传!</a></div>
4、后台Controller:
@RequestMapping(value = "/uploadfiles") @ResponseBody public JSONArray uploadFile(HttpServletRequest request){ Calendar calendar=Calendar.getInstance(); List<String> filePathList = new ArrayList<String>(); String filePath= SysConstant.UPLOADPATH; String filePathUrl=SysConstant.UPLOADPATHURL+"/"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH); filePath=filePath+File.separatorChar+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH); try { filePathList = FileUploadUtil.uploadFile(request, filePath,filePathUrl); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(Exception ex){ ex.printStackTrace(); } if(filePathList.size() == 0){ //return "系统错误"; } return JSONArray.fromObject(filePathList); }
5、上传类FileUploadUtil.java
public class FileUploadUtil { private static final Logger log = LoggerFactory.getLogger(FileUploadUtil.class); public static List<String> uploadFile(HttpServletRequest request, String filePath,String filePathUrl) throws FileNotFoundException { List<String> filePathList = new ArrayList<String>(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); String fileName = null; for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile mf = entity.getValue(); fileName = mf.getOriginalFilename(); String newfilepath; newfilepath = filePath + File.separatorChar + fileName; System.out.println("newfilepath=" + newfilepath); File dest = new File(filePath); if (!dest.exists()) { dest.mkdirs(); } File uploadFile = new File(newfilepath); if (uploadFile.exists()) { uploadFile.delete(); } try { log.info("start upload file: " + fileName); FileCopyUtils.copy(mf.getBytes(), uploadFile); } catch (IOException e) { // TODO Auto-generated catch block // e.printStackTrace(); log.info("upload failed. filename: " + fileName + e.getMessage()); return null; } filePathList.add(filePathUrl+"/"+fileName); } return filePathList; }}
6、配置spring-mvc.xml文件:
<!-- 文件上传表单的视图解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set the max upload size100MB --> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean>
7、需要增加的jar文件:
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
总结:这次上传花了一点时间,遇到几个小问题。所以留下来供大家参考。
0 0
- Spring mvc,uploadifive 文件上传实践(转自:https://segmentfault.com/a/1190000004503262)
- https://segmentfault.com/a/1190000003496009
- https://segmentfault.com/a/1190000002620961
- https://segmentfault.com/a/1190000002645623
- php7新特性(1)--https://segmentfault.com/a/1190000003893899
- 单点登录与消息队列(转载自https://segmentfault.com/a/1190000003758029)
- https://segmentfault.com/
- 五个python面试问题(转载)https://segmentfault.com/a/1190000000618513
- 理解CSS中的块级格式化上下文转载于(https://segmentfault.com/a/1190000003068557#articleHeader6)
- golang标准库的分析os包(https://segmentfault.com/a/1190000000376807)
- https://segmentfault.com/u/fengsdesinian
- HTTP 请求头与请求体 摘自https://segmentfault.com/a/1190000006689767
- spring mvc 文件上传
- spring MVC 文件上传
- Spring MVC文件上传
- Spring MVC文件上传
- spring mvc 文件上传
- Spring mvc 文件上传
- android:layout_width=“0dp”有什么用?为什么这么设置?
- ACM_程序设计竞赛:DP:01背包
- POI解析03版07版Word
- 转战自己的博客
- cf448C. Painting Fence【dfs】
- Spring mvc,uploadifive 文件上传实践(转自:https://segmentfault.com/a/1190000004503262)
- Android studio 导入fresco报错can not resolve 'com.facebook.fresco:fresco'的解决思想
- Jquery日历插件介绍及使用
- ffmpeg 视频转ts切片并生成m3u8播放列表文件
- window批处理bat命令详解
- csdn如何转载别人的文章
- windows server 2012设置多个用户远程连接
- JavaScript学习总结(十八)——JavaScript屏蔽Backspace键
- 原生js实时监听input、textarea输入