strtus1.2实现上传下载文件

来源:互联网 发布:淘宝海外买手直播 编辑:程序博客网 时间:2024/04/26 13:07
 

1 上传页面html:主要是form和input,已经导入前的文件格式限制

 

view plaincopy to clipboardprint?
  1. <form action="" enctype="multipart/form-data" method="post" name="form">   
  2.   input type="file" name="importFile" style="width: 255px;"></input>   
  3.   <input type="button" value="确定上传" onclick="JavaScript:importData();"/>  
  4.  <script language="javascript"type="text/javascript">  
  5. <!--  
  6.  function importData(){   
  7.    var fileName = document.all.importFile.value;   
  8.    if(fileName!='') {   
  9.     var endStr = fileName.substring(fileName.length-3,fileName.length);   
  10.       if('xls'==endStr.toLowerCase()){  
  11.        if(confirm("你确定上传吗?")){   
  12.            document.all.form.action=   
  13.             "/importData.doaction=importCheckDate";               
  14.             document.all.form.submit();   
  15.         }   
  16.       }else{  
  17.          alert("导入文件的格式错误,请输入excel文件(.xls)!");   
  18.      }   
  19.   }else{   
  20.           alert("请输入需要导入的文件!" );   
  21.    }   
  22. }  
  23. // --></script>  

 

 

view plaincopy to clipboardprint?
  1. <script language="javascript" type="text/javascript"><!--  
  2. function importData(){  
  3.     var fileName = document.all.importFile.value;  
  4.     if(fileName!='') {  
  5.         var endStr = fileName.substring(fileName.length-3,fileName.length);  
  6.         if('xls'==endStr.toLowerCase()){  
  7.             if(confirm("你确定上传吗?")){  
  8.                 document.all.form.action = "/school/importData.do?action=importCheckData&type=<%=type%>";  
  9.                 document.all.form.submit();  
  10.                 document.getElementById("waitMessage").style.display="block";  
  11.             }  
  12.         }else{  
  13.             alert("导入文件的格式错误,请输入excel文件(.xls)!");  
  14.         }  
  15.     }else{  
  16.         alert("请输入需要导入的文件!" );  
  17.     }  
  18. }    
  19. // --></script>  

 

 

 

2 struts上传中的form

与页面中的input中的name属性相同,并提供getter后台setter方法

view plaincopy to clipboardprint?
  1. import org.apache.struts.action.ActionForm;  
  2. import org.apache.struts.upload.FormFile;  
  3. public class ImportSchoolDataForm extends ActionForm {  
  4.     private static final long serialVersionUID = 1L;  
  5.       
  6.     private FormFile importFile;  
  7.       
  8.     public FormFile getImportFile() {  
  9.         return importFile;  
  10.     }  
  11.     public void setImportFile(FormFile importFile) {  
  12.         this.importFile = importFile;  
  13.     }  
  14.      
  15. }  

 

3 action配置需要的form

 

 

view plaincopy to clipboardprint?
  1. <form-beans>  
  2.         <form-bean name="ImportDataForm" type="com.xx.form.ImportDataForm"></form-bean>   
  3.     </form-beans>   
  4. <action parameter="action" path="/school/importData"  
  5.          name="ImportDataForm"  
  6.          scope="request"  
  7.          type="com..actions.ImportDataAction"/>  
  8.     

 

4 在action中

 

view plaincopy to clipboardprint?
  1. ImportDataForm fileForm=(ImportDataForm)form;  
  2.             FormFile file = fileForm.getImportFile();  
  3.             InputStream inputStream = file.getInputStream();  
  4. //业务逻辑处理   
  5. inputStream.close();  
  6. //跳转到结果页面  

 

5 文件下载

view plaincopy to clipboardprint?
  1. //指定下载excel文件   
  2. response.setContentType("application/msexcel");  
  3. //设置下载文件的文件名,(必须用'ISO8859_1'进行编码)                   response.setHeader("Content-disposition", "attachment; filename=/""+ new String("测试.els".getBytes("GBK"),"ISO8859_1") + "/"");  
  4. //找到项目的根目录   
  5.  String root = request.getSession().getServletContext().getRealPath("");  
  6. String path=root+"/down_files;  
  7. //没有就创建目录   
  8. File filePath=new File(path);  
  9. if(!filePath.exists()){  
  10.    filePath.mkdir();  
  11. }  
  12. //读取文件   
  13. FileInputStream fis = new FileInputStream(path + "/下载文件.els" );  
  14. BufferedInputStream bis = null;  
  15. BufferedOutputStream bos = null;  
  16. FileInputStream fis= null;  
  17. try {  
  18.     // 把数据以规定的格式输出   
  19.     response.setContentType("application/msexcel");  
  20.     response.setHeader("Content-disposition""attachment; filename=/""  
  21.             + new String(fileName.getBytes("GBK"),"ISO8859_1") + "/"");  
  22.      String root = request.getSession().getServletContext().getRealPath("");  
  23.      String fileFolder = SchoolUtil.DIR;  
  24.      String path = generalFilePath(root, fileFolder);  
  25.      //保存在系统目录中的模板文件名称   
  26.      String modelFileName = SchoolUtil.FILE_NAME_STUDENT;  
  27.         if(SchoolUtil.TEACHER.equals(type)) {  
  28.             modelFileName = SchoolUtil.FILE_NAME_TEACHER;  
  29.         }  
  30.         if(SchoolUtil.TEACHER_CLASS.equals(type)) {  
  31.             modelFileName = SchoolUtil.FILE_NAME_TEACHER_CLASS;  
  32.         }  
  33.     // 将内存里导出的EXCEL文件包装成FileInputStream流然后返回给ACTION  
  34.      fis = new FileInputStream(path + "/" + modelFileName);  
  35.       
  36.     bis = new BufferedInputStream(fis);  
  37.     OutputStream out = response.getOutputStream();  
  38.     bos = new BufferedOutputStream(out);  
  39.     byte[] buff = new byte[2048];  
  40.     while (-1 != (bis.read(buff, 0, buff.length))) {  
  41.         bos.write(buff, 0, buff.length);  
  42.     }  
  43. catch (Exception e) {  
  44.     e.printStackTrace();  
  45. finally {//最终关闭所有的io,否则将出现错误  
  46.     if (bis != null) {  
  47.         try {  
  48.             bis.close();  
  49.         } catch (Exception e) {  
  50.             e.printStackTrace();  
  51.         }finally{  
  52.             if (bos != null) {  
  53.                 try {  
  54.                     bos.close();  
  55.                 } catch (Exception e) {  
  56.                     e.printStackTrace();  
  57.                 }finally{  
  58.                     if(fis!=null){  
  59.                         fis.close();  
  60.                     }  
  61.                 }  
  62.             }  
  63.            }  
  64.         }  
  65.     }  
  66. }  

 

原创粉丝点击