zoukankan      html  css  js  c++  java
  • spring mvc配置完后实现下载功能

     实现是前台:

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
    2.     pageEncoding="UTF-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    7.     <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>  
    8. <title>Insert title here</title>  
    9. </head>  
    10. <body>  
    11.     <input id='fileName' type="text" name="fileName"/>  
    12.     <href="download.do" target="blank"><button>下载</button></a>  
    13. </body>  
    14. <script type="text/javascript">  
    15.     $(function(){  
    16.         $('a').click(function(){  
    17.             var link=this.href+'?'+'fileName='+$('#fileName').val();  
    18.             window.open(link);  
    19.             return false;  
    20.         });  
    21.     });  
    22. </script>  
    23. </html>  


    前台填写要下载的文件,后台从文件夹里查找,如果没有文件则返回错误文件,否则则提供任意文件类型的下载(填写文件时必须写后缀)

    1. package hope.cs.zhku.controller;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileInputStream;  
    5. import java.io.FileNotFoundException;  
    6. import java.io.IOException;  
    7. import java.io.InputStream;  
    8. import java.io.OutputStream;  
    9.   
    10. import javax.servlet.http.HttpServletResponse;  
    11.   
    12. import org.springframework.stereotype.Controller;  
    13. import org.springframework.web.bind.annotation.RequestMapping;  
    14.   
    15. /****************************************************************************** 
    16.  * 名称:UserBasicEditorController.java</br> 
    17.  * 日期:2011-8-15</br> 
    18.  * 功能:</br> 
    19.  * 编写:Willson Huang</br> 
    20.  * 复核:</br> 
    21.  * 其他:</br> 
    22.  * 历史:(说明,修改人,时间)</br> 
    23.  * 1.create ,Willson Huang ,2011-8-15 
    24.  *****************************************************************************/  
    25. @Controller  
    26. public class DownloadController {  
    27.   
    28.     @RequestMapping("download.do")  
    29.     public void downloadFile(String fileName,HttpServletResponse response){  
    30.         response.setCharacterEncoding("utf-8");  
    31.         response.setContentType("multipart/form-data");  
    32.   String downName = new String((downName.getBytes(), "ISO-8859-1"); //downName是从数据库中找出的中文名,这样做是为了防止中文名乱码。
    33.         response.setHeader("Content-Disposition", "attachment;fileName="+fileName);  //这里的fileName可以是保存时文件的名字,也可以换成downName自己命名的下载下来的名字。
    34.         try {  
    35.             File file=new File(fileName);  
    36.             System.out.println(file.getAbsolutePath());  
    37.             InputStream inputStream=new FileInputStream(file);  
    38.             OutputStream os=response.getOutputStream();  
    39.             byte[] b=new byte[1024];  
    40.             int length;  
    41.             while((length=inputStream.read(b))>0){  
    42.                 os.write(b,0,length);  
    43.             }  
    44.             inputStream.close();  
    45.         } catch (FileNotFoundException e) {  
    46.             e.printStackTrace();  
    47.         } catch (IOException e) {  
    48.             e.printStackTrace();  
    49.         }  
    50.     }  
    51. }  


     

  • 相关阅读:
    nginx自定义header支持
    使用 navicat 导入导出数据库
    eureka 服务注册与发现
    反射应用之动态代理
    接口应用之“静态代理”
    工厂模式
    计算程序运行的时间(以求得1-10000之间的素数为例)
    抽象类的模板方法设计模式
    ==和equals()方法的区别
    单例设计模式之饿汉式和懒汉式的区别
  • 原文地址:https://www.cnblogs.com/wjwen/p/4054453.html
Copyright © 2011-2022 走看看