http下载文件
2015-12-14 00:00
543 查看
InputStream bis = null; BufferedOutputStream bos = null; try { response.setContentType("UTF-8"); response.setCharacterEncoding("UTF-8"); // if (uploadFile.getExtend().equals("text")) { // response.setContentType("text/plain;"); // } else if (uploadFile.getExtend().equals("doc")) { // response.setContentType("application/msword;"); // } else if (uploadFile.getExtend().equals("xls")) { // response.setContentType("application/ms-excel;"); // } else if (uploadFile.getExtend().equals("pdf")) { // response.setContentType("application/pdf;"); // } else if (uploadFile.getExtend().equals("jpg") || uploadFile.getExtend().equals("jpeg")) { // response.setContentType("image/jpeg;"); // } else { // response.setContentType("application/x-msdownload;"); // } response.setContentType("text/plain;"); response.setHeader("Content-disposition", "attachment; filename=menu.json"); response.setHeader("Content-Length", ""+(new File(fileName)).length()); bos = new BufferedOutputStream(response.getOutputStream()); bis = new BufferedInputStream(new FileInputStream(fileName)); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- 自定义的投票协议的通讯(tcp/ip 与 udp示例)
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- HTTP Request header
- java TCP和UDP 连接
- CXF客户端调用https Webservice
- Nginx的https设置
- fork http://coolshell.cn/articles/7965.html
- HttpClient第一章(三)
- 【转载】HTTP状态码的来龙去脉
- 认识 HTTP 请求
- TCPIP详解 卷一核心概念整理
- 神经网络-激活函数
- Http协议的常见参数
- TCP/IP详解卷1 读书笔记:HTTP协议
- 新浪自媒体重新开放注册 一点资讯全面布局?
- HTTP协议简略
- TCP和UDP的区别
- Android基础的sharedPreferences存储,ListView,以及httpclient,和asynchttp框架请求的使用
- Go语言中Tcp协议粘包问题处理
- 利用Socket建立网络连接的步骤