Java文件下载

今天来说说下载的Java实现 思路比较简单,客户端发送下载文件的请求,服务器接受请求,然后返回数据,浏览器接受数据。
直接上代码 最后说一下比较流行的扫面二维码下载

1)前台
一个a标签 点击触发事件

  <a style="text-decoration:none;" href="${basePath}mapService/rest/download/downloadfile?type=android" class="android">
  <img src="${basePath}skin_group_web/images/u1.png" onclick="downloadAndroid();">Android</a>

这是请求资源的Javascript代码

<script type="text/javascript">
    function downloadAndroid(obj){
        document.location.href="${basePath}mapService/rest/download/downloadfile?type=android";
    }
</script>

2 后台 controller 层

package com.sgcc.evoms.map.charge;

/**
 * <P>Description:(Android客户端的下载)</P> 
 * @version 1.0
 * @author:
 * @CreateDate:2015-8-20
 * */

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/download")
public class DownloadFileController {
    @RequestMapping(value = "/downloadfile")
    public void download(HttpServletRequest request,
            HttpServletResponse response) {
        // 通过request参数filepath的方式来解析文件路径
        // 没有将网络路径转换成本地路径,如果传递过来的是网络路径可以将文件路径修改一下
        // 如果需求改变可以随时修改传递参数的方式
        String type = request.getParameter("type");

        String filepath = "";
        if ("android".equals(type)) {
            filepath = "/home/iovsp_jt_android.apk";// 测试文件
        } else if ("ios".equals(type)) {
            filepath = "/home/iovsp_jt_ios.apk";// 测试文件
        } 

        int last = filepath.lastIndexOf("/");
        String filename = filepath.substring(last + 1, filepath.length());


//      读取文件 输出网络文件
        File file = new File(filepath);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 设置header 下载 utf-8编码
        try {
            response.reset();// 删除HTML的空格
            response.setContentType("application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(filename, "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        //网络流里写数据
        int len;
        byte buffer[] = new byte[1024];
        try {
            while ((len = bis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭输入流
        try {
            if (bis != null) {
                bis.close();
            }
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     * 将网络路径转换为本地路径
     */
    public String changePath(String filepath) {
        String path = "";
        return path;
    }
}

运行代码的前提是安装了spring mvc。
3 扫描二维码下载 其实就是一个二维码生成的过程 把资源的请求地址URL生成二维码就可以了,百度一下二维码生成器,即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值