Java文件下载
来源:互联网 发布:fastjson map转json 编辑:程序博客网 时间:2024/06/04 00:55
今天来说说下载的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 0
- java下载文件
- java 文件下载
- java 文件下载
- java下载文件
- java实现文件下载
- java 文件下载
- Java文件下载
- Java 网络文件下载
- java文件下载
- java 文件下载疑问
- java 文件下载 详解
- java文件下载
- Java 下载文件
- java 下载文件
- Java文件下载相关
- 文件下载(java) android
- java 文件下载
- java 文件下载
- USACO 1.2 Transformations (用结构体来模拟)
- 巴菲特投资秘诀
- LeetCode(64) Minimum Path Sum
- fstat、stat和lstat 区别(转) http://www.cnitblog.com/guopingleee/archive/2008/11/13/51411.aspx
- (4.1.35.4)android中的native crash
- Java文件下载
- ios-断点续传
- JAVA- FileInputStream与FileReader的区别
- spray.testkit的使用
- Spring Mvc常用注解
- 【POJ2516】【最小费用流】
- uva 1395 - Slim Span(生成树)
- 简单的动画播放
- 1.Linux链接概念 http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html