java 下载文件功能
2016-03-09 00:00
148 查看
public String downLoadFileContent() throws IOException{ // 下载网络文件 int bytesum = 0; int byteread = 0; response = CustomerUtil.getResponse(); request = CustomerUtil.getRequest(); String filePath = request.getParameter("filePath");//http://localhost:8080/mypro/img String fileName = request.getParameter("fileName");//201212121212.txt URL url = new URL(filePath+fileName); try { URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); OutputStream fs=response.getOutputStream(); //设置response的编码方式 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1")); response.setHeader("Content-Disposition","attachment;filename="+fileName); byte[] buffer = new byte[1204]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer, 0, byteread); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
本文出自 “小浩” 博客,请务必保留此出处http://zhangchi.blog.51cto.com/5214280/1392282
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- Scrapy的架构介绍
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序