spring mvc配置完后实现下载功能

本文介绍了一个简单的Java文件下载实现方案,包括前端页面设置与后端控制器处理流程。用户可在前端输入框中指定文件名(含后缀),点击按钮后,系统会从服务器指定文件夹中查找该文件并提供下载。若文件不存在,则会返回错误提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="java">[html] view plaincopy
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>  
<title>Insert title here</title>  
</head>  
<body>  
    <input id='fileName' type="text" name="fileName"/>  
    <a href="download.do" target="blank"><button>下载</button></a>  
</body>  
<script type="text/javascript">  
    $(function(){  
        $('a').click(function(){  
            var link=this.href+'?'+'fileName='+$('#fileName').val();  
            window.open(link);  
            return false;  
        });  
    });  
</script>  
</html>  

前台填写要下载的文件,后台从文件夹里查找,如果没有文件则返回错误文件,否则则提供任意文件类型的下载(填写文件时必须写后缀)
[java] view plaincopy
package hope.cs.zhku.controller;  
  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
  
import javax.servlet.http.HttpServletResponse;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
  
/****************************************************************************** 
 * 名称:UserBasicEditorController.java</br> 
 * 日期:2011-8-15</br> 
 * 功能:</br> 
 * 编写:Willson Huang</br> 
 * 复核:</br> 
 * 其他:</br> 
 * 历史:(说明,修改人,时间)</br> 
 * 1.create ,Willson Huang ,2011-8-15 
 *****************************************************************************/  
@Controller  
public class DownloadController {  
  
    @RequestMapping("download.do")  
    public void downloadFile(String fileName,HttpServletResponse response){  
        response.setCharacterEncoding("utf-8");  
        response.setContentType("multipart/form-data");  
  
        response.setHeader("Content-Disposition", "attachment;fileName="+fileName);  
        try {  
            File file=new File(fileName);  
            System.out.println(file.getAbsolutePath());  
            InputStream inputStream=new FileInputStream("file/"+file);  
            OutputStream os=response.getOutputStream();  
            byte[] b=new byte[1024];  
            int length;  
            while((length=inputStream.read(b))>0){  
                os.write(b,0,length);  
            }  
            inputStream.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值