Java文件下载功能,点击展开另存为

public class DownloadUtils {

/*
* 文件(图片)下载通用类
*/


public void download(NotifyAttach  natch ,HttpServletResponse response) { 

        String filePath = natch.getSavepath();  
        //截取
        String nowSuffix = (natch.getFilename()).substring((natch.getFilename()).indexOf(".")+1);
        String nowfilePath = natch.getFilename();
        //截取
        nowfilePath = nowfilePath.substring(0, nowfilePath.indexOf("."));
        
        try {  
       
            File file = new File(filePath);  
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);  
            fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
            
//           response.setContentType("application/octet-stream"); 
//           response.addHeader("Content-Disposition", "attachment;filename="+natch.getFilename());
            //获取文件格式
            if(StringUtils.isNotBlank(nowSuffix) && ("xls".equals(nowSuffix) ||"xlsx".equals(nowSuffix))){
           
            response.setContentType("application/vnd.ms-excel");  
            response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".xls");
           
            }else if(StringUtils.isNotBlank(nowSuffix) && "pdf".equals(nowSuffix)){
            //支持pdf格式
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".pdf");
           
            }else if(StringUtils.isNotBlank(nowSuffix) && "txt".equals(nowSuffix)){
            //支持文本格式
            response.setContentType("text/plain");
            response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".txt");
           
            }else if(StringUtils.isNotBlank(nowSuffix) && "doc".equals(nowSuffix)){
            //支持doc
            response.setContentType("application/msword");
            response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".doc");
           
            }else{
           
            //默认格式
            response.setContentType("application/octet-stream"); 
                response.addHeader("Content-Disposition", "attachment;filename="+natch.getFilename());
            }
            


            String len = String.valueOf(file.length());  
            response.setHeader("Content-Length", len);  
            OutputStream out = response.getOutputStream();  
            FileInputStream in = new FileInputStream(file);  
            
            byte[] b = new byte[1024];  
            int n;  
            
            while((n=in.read(b))!=-1){  
                out.write(b, 0, n);  
            }  
            //关闭操作
            in.close();  
            out.close();  
            
        } catch (FileNotFoundException e) {  
       
            e.printStackTrace();  
            
        } catch (IOException e) {  
       
            e.printStackTrace(); 
            
        }  
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值