Struts2.0多文件上传下载
2016-07-14 15:59
483 查看
一、文件上传
Struts2并没有提供文件上传的组件,所以想要实现上传的功能就必须通过第三方组件来实现,在Struts2引用的jar中包含了文件上传的组件,它是通过commons-fileupload.jar和commons-io.jar来实现的。这其中的处理细节不需要太懂,在使用中慢慢思考就可以做到灵活运用。
1、编写Action类
文件上传的核心实现使用的还是java的数据流io操作,上传文件时要防止文件上传重复覆盖,
文件上传操作类:
action调用类:
jsp页面:
一、文件下载
Action代码:
jsp页面:显示文件名
Map
下载最主要的是struts-xml文件的配置:
Struts2并没有提供文件上传的组件,所以想要实现上传的功能就必须通过第三方组件来实现,在Struts2引用的jar中包含了文件上传的组件,它是通过commons-fileupload.jar和commons-io.jar来实现的。这其中的处理细节不需要太懂,在使用中慢慢思考就可以做到灵活运用。
1、编写Action类
文件上传的核心实现使用的还是java的数据流io操作,上传文件时要防止文件上传重复覆盖,
文件上传操作类:
public String setFile(List<File> fileList,List<String> fileListFileName,List<String> fileListContentType) throws IOException{ String root = ServletActionContext.getServletContext().getRealPath("/upload/sample")+File.separator;//+tmpPath+File.separator; System.out.println("root=="+root); File f = new File(root); if(!f.exists()){ f.mkdirs(); } File[] files = FileUtil.listSortedFiles(f); String path=""; for(int i = 0; i < fileList.size(); i++) { InputStream is = new FileInputStream(fileList.get(i)); String filename = fileListFileName.get(i); String fileName1 = filename.substring(0,filename.lastIndexOf(".")); String fileSuffix1 = filename.substring(filename.lastIndexOf(".")); Integer fileIndex = -1; for (File file : files) { if(file.isDirectory())continue; String fileName = file.getName(); String fileSuffix = ""; if(file.getName().lastIndexOf(".") > -1){ fileName = file.getName().substring(0,file.getName().lastIndexOf(".")); fileSuffix = file.getName().substring(file.getName().lastIndexOf(".")); } if(fileName.indexOf(fileName1)==0 && fileSuffix.equals(fileSuffix1)){ if(fileName.equals(fileName1)){ if(fileIndex == -1){ filename = fileName + "-副本" + fileSuffix; fileIndex = 0; } }else if(fileName.indexOf("-副本")>0){ int index = fileName.indexOf("-副本"); String num = "".equals(fileName.substring(index+3))?"0":fileName.substring(index+3); Integer tmp = (Integer.parseInt(num)+1); if(fileIndex < tmp){ fileIndex = tmp; filename = fileName1 + "-副本" + tmp + fileSuffix; } } } } OutputStream os = new FileOutputStream(new File(root, filename));// path += ("".equals(path)?"":";")+root+filename; byte[] buffer = new byte[500]; @SuppressWarnings("unused") int length = 0; while(-1 != (length = is.read(buffer, 0, buffer.length))) { os.write(buffer); } os.close(); is.close(); } return path; }
action调用类:
private List<File> cerFiles; private List<String> cerFilesFileName; private List<String> cerFilesContentType; public String save() throws IOException{ String path1=this.setFile(cerFiles, cerFilesFileName, cerFilesContentType); sampleCertificationInfo.setAttachmentCerPath(path1); }
jsp页面:
<script type="text/javascript"> $(function() { $("#button1").click(function() { var html = $("<input type='file' name='cerFiles'>"); var button = $("<input type='button' name='button' value='删除'><br>"); $("#add1").append(html).append(button); button.click(function() { html.remove(); button.remove(); }) }) }) </script> <form action="sampleCertificationAction_save.action" method="post" enctype="multipart/form-data"> <input type="file" name="cerFiles"> <input type="button" value="添加" id="button1"><br> <div id="add1"></div> </form>
一、文件下载
Action代码:
private String fileName; private InputStream fileInput; public InputStream getDownloadFile() { return ServletActionContext.getServletContext().getResourceAsStream("upload\\sample\\"+fileName); } public String getFileDown(){ System.out.println("下载的文件是==="+fileName); fileInput=ServletActionContext.getServletContext().getResourceAsStream("upload\\sample\\"+fileName); try { fileName = new String(fileName.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if(fileInput == null){ return ""; } System.out.println("fileInput=="+fileInput); return "success"; } public InputStream getFileInput() { return fileInput; } public void setFileInput(InputStream fileInput) { this.fileInput = fileInput; }
jsp页面:显示文件名
Map
<s:iterator value="#filenamesList" id="val"> <a href="sampleBaseInfoAction_getFileDown.action?fileName=<s:property value="#val"/>" target="tmp_upload_iframe"><s:property value="#val"/></a></br> </s:iterator> 如果直接是List<String>集合存储文件名,jsp显示用以下方式: <s:iterator value="#infoList" id="val"> <a href="sampleBaseInfoAction_getFileDown.action?fileName=<s:property value="#val"/>" target="tmp_upload_iframe"><s:property value="#val"/></a></br> </s:iterator></td>
下载最主要的是struts-xml文件的配置:
<result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">fileInput</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">4096</param> </result>
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- java struts常见错误以及原因分析
- Struts之logic标签库详解
- 通过实例深入学习Java的Struts框架中的OGNL表达式使用
- Java的Struts框架中append标签与generator标签的使用
- struts2的select标签用法实例分析
- SSH框架网上商城项目第20战之在线支付平台
- JavaWeb Struts文件上传功能实现详解
- 在Java的Struts框架下进行web编程的入门教程
- 详解Java的Struts框架中注释的用法
- java中struts 框架的实现
- 利用Java的Struts框架实现电子邮件发送功能
- 在Java的Struts框架中ONGL表达式的基础使用入门
- 详解Java的Struts框架中栈值和OGNL的使用
- Java的Struts框架简介与环境配置教程
- Java的Struts框架中的if/else标签使用详解
- 简单说明Java的Struts框架中merge标签的使用方法
- 详解Java的Struts框架中上传文件和客户端验证的实现
- Java的Struts框架中Action的编写与拦截器的使用方法
- Java的Struts2框架中拦截器使用的实例教程