strtus1.2实现上传下载文件
来源:互联网 发布:淘宝海外买手直播 编辑:程序博客网 时间:2024/04/26 13:07
1 上传页面html:主要是form和input,已经导入前的文件格式限制
- <form action="" enctype="multipart/form-data" method="post" name="form">
- input type="file" name="importFile" style="width: 255px;"></input>
- <input type="button" value="确定上传" onclick="JavaScript:importData();"/>
- <script language="javascript"type="text/javascript">
- <!--
- function importData(){
- var fileName = document.all.importFile.value;
- if(fileName!='') {
- var endStr = fileName.substring(fileName.length-3,fileName.length);
- if('xls'==endStr.toLowerCase()){
- if(confirm("你确定上传吗?")){
- document.all.form.action=
- "/importData.doaction=importCheckDate";
- document.all.form.submit();
- }
- }else{
- alert("导入文件的格式错误,请输入excel文件(.xls)!");
- }
- }else{
- alert("请输入需要导入的文件!" );
- }
- }
- // --></script>
- <script language="javascript" type="text/javascript"><!--
- function importData(){
- var fileName = document.all.importFile.value;
- if(fileName!='') {
- var endStr = fileName.substring(fileName.length-3,fileName.length);
- if('xls'==endStr.toLowerCase()){
- if(confirm("你确定上传吗?")){
- document.all.form.action = "/school/importData.do?action=importCheckData&type=<%=type%>";
- document.all.form.submit();
- document.getElementById("waitMessage").style.display="block";
- }
- }else{
- alert("导入文件的格式错误,请输入excel文件(.xls)!");
- }
- }else{
- alert("请输入需要导入的文件!" );
- }
- }
- // --></script>
2 struts上传中的form
与页面中的input中的name属性相同,并提供getter后台setter方法
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.upload.FormFile;
- public class ImportSchoolDataForm extends ActionForm {
- private static final long serialVersionUID = 1L;
- private FormFile importFile;
- public FormFile getImportFile() {
- return importFile;
- }
- public void setImportFile(FormFile importFile) {
- this.importFile = importFile;
- }
- }
3 action配置需要的form
- <form-beans>
- <form-bean name="ImportDataForm" type="com.xx.form.ImportDataForm"></form-bean>
- </form-beans>
- <action parameter="action" path="/school/importData"
- name="ImportDataForm"
- scope="request"
- type="com..actions.ImportDataAction"/>
4 在action中
- ImportDataForm fileForm=(ImportDataForm)form;
- FormFile file = fileForm.getImportFile();
- InputStream inputStream = file.getInputStream();
- //业务逻辑处理
- inputStream.close();
- //跳转到结果页面
5 文件下载
- //指定下载excel文件
- response.setContentType("application/msexcel");
- //设置下载文件的文件名,(必须用'ISO8859_1'进行编码) response.setHeader("Content-disposition", "attachment; filename=/""+ new String("测试.els".getBytes("GBK"),"ISO8859_1") + "/"");
- //找到项目的根目录
- String root = request.getSession().getServletContext().getRealPath("");
- String path=root+"/down_files;
- //没有就创建目录
- File filePath=new File(path);
- if(!filePath.exists()){
- filePath.mkdir();
- }
- //读取文件
- FileInputStream fis = new FileInputStream(path + "/下载文件.els" );
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- FileInputStream fis= null;
- try {
- // 把数据以规定的格式输出
- response.setContentType("application/msexcel");
- response.setHeader("Content-disposition", "attachment; filename=/""
- + new String(fileName.getBytes("GBK"),"ISO8859_1") + "/"");
- String root = request.getSession().getServletContext().getRealPath("");
- String fileFolder = SchoolUtil.DIR;
- String path = generalFilePath(root, fileFolder);
- //保存在系统目录中的模板文件名称
- String modelFileName = SchoolUtil.FILE_NAME_STUDENT;
- if(SchoolUtil.TEACHER.equals(type)) {
- modelFileName = SchoolUtil.FILE_NAME_TEACHER;
- }
- if(SchoolUtil.TEACHER_CLASS.equals(type)) {
- modelFileName = SchoolUtil.FILE_NAME_TEACHER_CLASS;
- }
- // 将内存里导出的EXCEL文件包装成FileInputStream流然后返回给ACTION
- fis = new FileInputStream(path + "/" + modelFileName);
- bis = new BufferedInputStream(fis);
- OutputStream out = response.getOutputStream();
- bos = new BufferedOutputStream(out);
- byte[] buff = new byte[2048];
- while (-1 != (bis.read(buff, 0, buff.length))) {
- bos.write(buff, 0, buff.length);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {//最终关闭所有的io,否则将出现错误
- if (bis != null) {
- try {
- bis.close();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if (bos != null) {
- try {
- bos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(fis!=null){
- fis.close();
- }
- }
- }
- }
- }
- }
- }
- strtus1.2实现上传下载文件
- 2、基于srtuts2实现文件上传下载
- jspsmart实现文件上传下载
- jspsmart实现文件上传下载
- c#实现文件上传下载
- jspSmartUpload 实现文件上传下载
- BCB实现文件上传下载
- jspsmart实现文件上传下载
- jsp实现文件上传下载
- 文件流实现上传下载
- iOS-实现文件上传下载
- YII实现文件上传下载
- iOS-实现文件上传下载 .
- iOS 实现文件上传下载
- socket 实现文件上传下载
- GWT实现文件上传下载
- C#实现文件上传下载
- iOS-实现文件上传下载
- c++学习总结
- linux笔试题
- Taglib中常用的属性值与tld常用标签简介与web.xml设置自定义标签的uri
- C++程序员应聘常见面试试题深入剖析
- No configuration found for the specified action警告总结
- strtus1.2实现上传下载文件
- 网站上传容易忽视的错误
- Android学习历程之四 Animation初识一
- android系统结构
- dp、dp、dip、sp等单位
- 业务建模
- Linux Top 命令详解
- 整数的素因子分解
- 心得