已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
相关资源下载地址:/detail/geloin/4506561 本文基于Spring MVC 注解,让Spring跑起来。 (1) 导入jar包:ant.jar、commons-fileupload.jar、connom-io.jar。 (2) 在src/context/dispatcher.xml中添加java view plain copy print?1. 注意,需要在头部添加内容,添加后如下所示:java view plain copy print?1. (3) 添加工具类FileOperateUtil.java java view plain copy print?1. /*2. *3. *authorgeloin4. *date2012-5-5下午12:05:575. */6. packagecom.geloin.spring.util;7. 8. importjava.io.BufferedInputStream;9. importjava.io.BufferedOutputStream;10. importjava.io.File;11. importjava.io.FileInputStream;12. importjava.io.FileOutputStream;13. importjava.text.SimpleDateFormat;14. importjava.util.ArrayList;15. importjava.util.Date;16. importjava.util.HashMap;17. importjava.util.Iterator;18. importjava.util.List;19. importjava.util.Map;20. 21. importjavax.servlet.http.HttpServletRequest;22. importjavax.servlet.http.HttpServletResponse;23. 24. importorg.apache.tools.zip.ZipEntry;25. importorg.apache.tools.zip.ZipOutputStream;26. importorg.springframework.util.FileCopyUtils;27. importorg.springframework.web.multipart.MultipartFile;28. importorg.springframework.web.multipart.MultipartHttpServletRequest;29. 30. /*31. *32. *authorgeloin33. *date2012-5-5下午12:05:5734. */35. publicclassFileOperateUtil36. privatestaticfinalStringREALNAME=realName;37. privatestaticfinalStringSTORENAME=storeName;38. privatestaticfinalStringSIZE=size;39. privatestaticfinalStringSUFFIX=suffix;40. privatestaticfinalStringCONTENTTYPE=contentType;41. privatestaticfinalStringCREATETIME=createTime;42. privatestaticfinalStringUPLOADDIR=uploadDir/;43. 44. /*45. *将上传的文件进行重命名46. *47. *authorgeloin48. *date2012-3-29下午3:39:5349. *paramname50. *return51. */52. privatestaticStringrename(Stringname)53. 54. Longnow=Long.parseLong(newSimpleDateFormat(yyyyMMddHHmmss)55. .format(newDate();56. Longrandom=(long)(Math.random()*now);57. StringfileName=now+random;58. 59. if(name.indexOf(.)!=-1)60. fileName+=name.substring(name.lastIndexOf(.);61. 62. 63. returnfileName;64. 65. 66. /*67. *压缩后的文件名68. *69. *authorgeloin70. *date2012-3-29下午6:21:3271. *paramname72. *return73. */74. privatestaticStringzipName(Stringname)75. Stringprefix=;76. if(name.indexOf(.)!=-1)77. prefix=name.substring(0,name.lastIndexOf(.);78. else79. prefix=name;80. 81. returnprefix+.zip;82. 83. 84. /*85. *上传文件86. *87. *authorgeloin88. *date2012-5-5下午12:25:4789. *paramrequest90. *paramparams91. *paramvalues92. *return93. *throwsException94. */95. publicstaticListMapupload(HttpServletRequestrequest,96. Stringparams,Mapvalues)throwsException97. 98. ListMapresult=newArrayListMap();99. 100. MultipartHttpServletRequestmRequest=(MultipartHttpServletRequest)request;101. MapfileMap=mRequest.getFileMap();102. 103. StringuploadDir=request.getSession().getServletContext()104. .getRealPath(/)105. +FileOperateUtil.UPLOADDIR;106. Filefile=newFile(uploadDir);107. 108. if(!file.exists()109. file.mkdir();110. 111. 112. StringfileName=null;113. inti=0;114. for(IteratorMap.Entryit=fileMap.entrySet()115. .iterator();it.hasNext();i+)116. 117. Map.Entryentry=it.next();118. MultipartFilemFile=entry.getValue();119. 120. fileName=mFile.getOriginalFilename();121. 122. StringstoreName=rename(fileName);123. 124. StringnoZipName=uploadDir+storeName;125. StringzipName=zipName(noZipName);126. 127. /上传成为压缩文件128. ZipOutputStreamoutputStream=newZipOutputStream(129. newBufferedOutputStream(newFileOutputStream(zipName);130. outputStream.putNextEntry(newZipEntry(fileName);131. outputStream.setEncoding(GBK);132. 133. FileCopyUtils.copy(mFile.getInputStream(),outputStream);134. 135. Mapmap=newHashMap();136. /固定参数值对137. map.put(FileOperateUtil.REALNAME,zipName(fileName);138. map.put(FileOperateUtil.STORENAME,zipName(storeName);139. map.put(FileOperateUtil.SIZE,newFile(zipName).length();140. map.put(FileOperateUtil.SUFFIX,zip);141. map.put(FileOperateUtil.CONTENTTYPE,application/octet-stream);142. map.put(FileOperateUtil.CREATETIME,newDate();143. 144. /自定义参数值对145. for(Stringparam:params)146. map.put(param,values.get(param)i);147. 148. 149. result.add(map);150. 151. returnresult;152. 153. 154. /*155. *下载156. *157. *authorgeloin158. *date2012-5-5下午12:25:39159. *paramrequest160. *paramresponse161. *paramstoreName162. *paramcontentType163. *paramrealName164. *throwsException165. */166. publicstaticvoiddownload(HttpServletRequestrequest,167. HttpServletResponseresponse,StringstoreName,StringcontentType,168. StringrealName)throwsException169. response.setContentType(text/html;charset=UTF-8);170. request.setCharacterEncoding(UTF-8);171. BufferedInputStreambis=null;172. BufferedOutputStreambos=null;173. 174. StringctxPath=request.getSession().getServletContext()175. .getRealPath(/)176. +FileOperateUtil.UPLOADDIR;177. StringdownLoadPath=ctxPath+storeName;178. 179. longfileLength=newFile(downLoadPath).length();180. 181. response.setContentType(contentType);182. response.setHeader(Content-disposition,attachment;filename=183. +newString(realName.getBytes(utf-8),ISO8859-1);184. response.setHeader(Content-Length,String.valueOf(fileLength);185. 186. bis=newBufferedInputStream(newFileInputStream(downLoadPath);187. bos=newBufferedOutputStream(response.getOutputStream();188. bytebuff=newbyte2048;189. intbytesRead;190. while(-1!=(bytesRead=bis.read(buff,0,buff.length)191. bos.write(buff,0,bytesRead);192. 193. bis.close();194. bos.close();195. 196. /* * * author geloin * date 2012-5-5 下午12:05:57 */package com.geloin.spring.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;import org.springframework.util.FileCopyUtils;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;/* * * author geloin * date 2012-5-5 下午12:05:57 */public class FileOperateUtil private static final String REALNAME = realName;private static final String STORENAME = storeName;private static final String SIZE = size;private static final String SUFFIX = suffix;private static final String CONTENTTYPE = contentType;private static final String CREATETIME = createTime;private static final String UPLOADDIR = uploadDir/;/* * 将上传的文件进行重命名 * * author geloin * date 2012-3-29 下午3:39:53 * param name * return */private static String rename(String name) Long now = Long.parseLong(new SimpleDateFormat(yyyyMMddHHmmss).format(new Date();Long random = (long) (Math.random() * now);String fileName = now + + random;if (name.indexOf(.) != -1) fileName += name.substring(name.lastIndexOf(.);return fileName;/* * 压缩后的文件名 * * author geloin * date 2012-3-29 下午6:21:32 * param name * return */private static String zipName(String name) String prefix = ;if (name.indexOf(.) != -1) prefix = name.substring(0, name.lastIndexOf(.); else prefix = name;return prefix + .zip;/* * 上传文件 * * author geloin * date 2012-5-5 下午12:25:47 * param request * param params * param values * return * throws Exception */public static ListMap upload(HttpServletRequest request,String params, Map values) throws Exception ListMap result = new ArrayListMap();MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;Map fileMap = mRequest.getFileMap();String uploadDir = request.getSession().getServletContext().getRealPath(/)+ FileOperateUtil.UPLOADDIR;File file = new File(uploadDir);if (!file.exists() file.mkdir();String fileName = null;int i = 0;for (IteratorMap.Entry it = fileMap.entrySet().iterator(); it.hasNext(); i+) Map.Entry entry = it.next();MultipartFile mFile = entry.getValue();fileName = mFile.getOriginalFilename();String storeName = rename(fileName);String noZipName = uploadDir + storeName;String zipName = zipName(noZipName);/ 上传成为压缩文件ZipOutputStream outputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName);outputStream.putNextEntry(new ZipEntry(fileName);outputStream.setEncoding(GBK);FileCopyUtils.copy(mFile.getInputStream(), outputStream);Map map = new HashMap();/ 固定参数值对map.put(FileOperateUtil.REALNAME, zipName(fileName);map.put(FileOperateUtil.STORENAME, zipName(storeName);map.put(FileOperateUtil.SIZE, new File(zipName).length();map.put(FileOperateUtil.SUFFIX, zip);map.put(FileOperateUtil.CONTENTTYPE, application/octet-stream);map.put(FileOperateUtil.CREATETIME, new Date();/ 自定义参数值对for (String param : params) map.put(param, values.get(param)i);result.add(map);return result;/* * 下载 * * author geloin * date 2012-5-5 下午12:25:39 * param request * param response * param storeName * param contentType * param realName * throws Exception */public static void download(HttpServletRequest request,HttpServletResponse response, String storeName, String contentType,String realName) throws Exception response.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);BufferedInputStream bis = null;BufferedOutputStream bos = null;String ctxPath = request.getSession().getServletContext().getRealPath(/)+ FileOperateUtil.UPLOADDIR;String downLoadPath = ctxPath + storeName;long fileLength = new File(downLoadPath).length();response.setContentType(contentType);response.setHeader(Content-disposition, attachment; filename=+ new String(realName.getBytes(utf-8), ISO8859-1);response.setHeader(Content-Length, String.valueOf(fileLength);bis = new BufferedInputStream(new FileInputStream(downLoadPath);bos = new BufferedOutputStream(response.getOutputStream();byte buff = new byte2048;int bytesRead;while (-1 != (bytesRead = bis.read(buff, 0, buff.length) bos.write(buff, 0, bytesRead);bis.close();bos.close(); 可完全使用而不必改变该类,需要注意的是,该类中设定将上传后的文件放置在WebContent/uploadDir下。 (4) 添加FileOperateController.Javajava view plain copy print?1. /*2. *3. *authorgeloin4. *date2012-5-5上午11:56:355. */6. packagecom.geloin.spring.controller;7. 8. importjava.util.HashMap;9. importjava.util.List;10. importjava.util.Map;11. 12. importjavax.servlet.http.HttpServletRequest;13. importjavax.servlet.http.HttpServletResponse;14. 15. importorg.springframework.stereotype.Controller;16. importorg.springframework.web.bind.ServletRequestUtils;17. importorg.springframework.web.bind.annotation.RequestMapping;18. importorg.springframework.web.servlet.ModelAndView;19. 20. importcom.geloin.spring.util.FileOperateUtil;21. 22. /*23. *24. *authorgeloin25. *date2012-5-5上午11:56:3526. */27. Controller28. RequestMapping(value=background/fileOperate)29. publicclassFileOperateController30. /*31. *到上传文件的位置32. *33. *authorgeloin34. *date2012-3-29下午4:01:3135. *return36. */37. RequestMapping(value=to_upload)38. publicModelAndViewtoUpload()39. returnnewModelAndView(background/fileOperate/upload);40. 41. 42. /*43. *上传文件44. *45. *authorgeloin46. *date2012-3-29下午4:01:4147. *paramrequest48. *return49. *throwsException50. */51. RequestMapping(value=upload)52. publicModelAndViewupload(HttpServletRequestrequest)throwsException53. 54. Mapmap=newHashMap();55. 56. /别名57. Stringalaises=ServletRequestUtils.getStringParameters(request,58. alais);59. 60. Stringparams=newStringalais;61. Mapvalues=newHashMap();62. values.put(alais,alaises);63. 64. ListMapresult=FileOperateUtil.upload(request,65. params,values);66. 67. map.put(result,result);68. 69. returnnewModelAndView(background/fileOperate/list,map);70. 71. 72. /*73. *下载74. *75. *authorgeloin76. *date2012-3-29下午5:24:1477. *paramattachment78. *paramrequest79. *paramresponse80. *return81. *throwsException82. */83. RequestMapping(value=download)84. publicModelAndViewdownload(HttpServletRequestrequest,85. HttpServletResponseresponse)throwsException86. 87. StringstoreName=201205051340364510870879724.zip;88. StringrealName=Java设计模式.zip;89. StringcontentType=application/octet-stream;90. 91. FileOperateUtil.download(requ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年江西科技学院单招职业适应性考试必刷测试卷附答案解析
- 2026年四川现代职业学院单招职业适应性测试必刷测试卷及答案解析(名师系列)
- 2026年浙江建设职业技术学院单招职业适应性测试题库带答案解析
- 2026年大连职业技术学院单招职业技能考试必刷测试卷及答案解析(夺冠系列)
- 2026年宁夏吴忠市单招职业适应性测试必刷测试卷带答案解析
- 2026年大庆医学高等专科学校单招综合素质考试题库附答案解析
- 2026年山东圣翰财贸职业学院单招职业倾向性考试必刷测试卷附答案解析
- 2026年山东理工职业学院单招职业倾向性考试题库及答案解析(夺冠系列)
- 2026年延安职业技术学院单招职业倾向性测试题库附答案解析
- 2026年德阳农业科技职业学院单招综合素质考试题库附答案解析
- 数学组教学比武活动方案
- 2025年华医网选修课(广东省卫生系统继续教育-选修课18学时)考试答案
- 航空航天复合材料 课件 第1章复合材料概论
- 英文【品牌价值】2025年最有价值和最强的高端奢侈品牌年度报告
- 2025秋二年级上册语文上课课件 18 古诗二首
- 2025年内蒙古航开城市建设投资有限责任公司及子公司招聘笔试参考题库含答案解析
- 银行三年发展战略规划项目建议书
- 中国精神障碍分类与诊断标准第3版
- 手表抵押合同协议
- 租车号合同协议
- 2020水利信息系统软件开发集成规范
评论
0/150
提交评论