文件下载,以流的方式
请求方:
public void test2() throws Exception{
System.out.println("test2进来了");
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
String timestr=sdf.format(new Date());
String key="bc3d7bc7b0c2409ebaba6127a7298ba8";String getURL="请求的url";               //这写你自己的
URL getUrl = new URL(getURL);// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
// 服务器
connection.setConnectTimeout(25000);
connection.setReadTimeout(25000);
connection.connect();int status = connection.getResponseCode();if (status == 200) {
DataInputStream in = new DataInputStream( connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream("d:\\a\\c.zip"));
byte[] buffer = new byte[400000];int count = 0;while ((count = in.read(buffer)) > 0) {out.write(buffer, 0, count);}out.close();in.close(); } else {String strResponse = "error";}connection.disconnect();
</pre><pre name="code" class="java">
接收方:
import ins.framework.web.Struts2Action;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionContext;
import com.sinosoft.ebusiness.monitor.aspect.BusinessLogger;
import com.sinosoft.ebusiness.thirdparty.service.spring.InsureServiceSpringImpl;
import com.sinosoft.ebusiness.util.property.CustomizedPropertyPlaceholderConfigurer;/*** * 本方法在后台和请求方交互并以流的形式返回给第三方需要的电子保单* 格式为* * * 规则:返回给第三方的链接格式为** @author WuJie**/public class EstampDownAction extends Struts2Action {//电子保单的本地地址String localAddress="C:/path";String  fileName="file1";EstampDownAction esda;public void estampDown() {try {localAddress = localAddress + fileName+ ".zip";File file = new File(localAddress); // 要下载的文件绝对路径InputStream ins;ins = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[ins.available()];ins.read(buffer);ins.close();HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);response.reset();response.addHeader("Content-Disposition","attachment;filename="+ new String(file.getName().getBytes()));response.addHeader("Content-Length", "" + file.length());OutputStream ous = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");ous.write(buffer);ous.flush();ous.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

response下载zip文件相关推荐

  1. Python压缩解压zip,Django下载zip文件

    Python压缩解压zip 压缩 import zipfile def convert_zip(zip_path, save_path):'''# zip_path 要压缩文件的路径# save_pa ...

  2. vue下载zip文件提示损坏

    vue下载zip文件提示损坏 问题代码 前端 export function exportCheckFile() {return request({url: '/exportCheckFile',me ...

  3. php+下载+网路错误,下载zip文件“网络错误失败”(PHP / NGINX)

    当我尝试从我的实时服务器下载zip文件时,我的浏览器(Chrome和Firefox)出现以下错误:https://mysite.com:网络错误失败. 令人困惑的是,在我的localhost上使用我的 ...

  4. java下载zip文件损坏_使用Java下载.zip文件会导致损坏的.zip文件?

    我写了这个方法来下载最新的Selenium Chrome驱动程序,它不起作用.它导致损坏的.zip文件.任何人都可以发现我的错误在哪里?使用Java下载.zip文件会导致损坏的.zip文件? priv ...

  5. 根据链接下载zip文件并用密码解压

    根据链接下载zip文件并用密码解压(亲测可用) 导入所需要的pom(注意版本) 导入所需要的pom(注意版本) 导入所需要的pom(注意版本) <dependency><groupI ...

  6. Web服务下载zip文件

    需求 平时下载功能最经常用到,服务中也会有完备的封装.最近新完成的一个项目,需要下载多个文件,自然首先想到的是将多个文件封装成zip文件进行下载. 提笔忘字,这种不是经常使用的api,难免会忘记怎么样 ...

  7. Java 下载zip文件

    选中文件打zip包下载 前台:文件列表,选中文件,传文件ID集合传到后台 后台: //接受到文件ID集合,查询出文件urlList List attachFileList = bookService. ...

  8. java 下载zip文件_Java以压缩包方式下载文件

    从云服务器上下载文件,以压缩包方式下载 以下载多个文件为例,需要导入zip4j的jar包,版本不要太高 public void downloadZip(List list, HttpServletRe ...

  9. 基于Python——实现远程下载sftp文件(只下载.zip文件)

    [背景]远程下载发布包等文件时,总是要使用WinSCP等工具登陆拖动.今天就介绍一种使用python下载文件到本地的方法. [代码实现] 1 import paramiko # paramiko模块, ...

  10. python如何下载zip文件_使用python-mechaniz下载zip文件

    我正在使用python2.7.mechanize和beautifulsoup,如果有帮助,我可以使用urllib 好的,我正在尝试下载两个不同的zip文件,它们位于不同的html表中.我知道特定文件在 ...

最新文章

  1. SELECT INTO 和 replace into SELECT 两种表复制语句
  2. linux 虚拟机不能启动不了系统,虚拟机更改linux初始启动5,出现无法启动现象
  3. SQL Server DBA常见面试 笔试问题
  4. 试题 F: 特别数的和 第十届蓝桥杯
  5. gradle挂接到构建生命周期(七)
  6. mysql 四种存储引擎
  7. 【java】关于Java NIO的一切
  8. 专家教你如何设置无线路由器 享受无线乐趣
  9. LINUX警告:检测到时钟错误。您的创建可能是不完整的。-转
  10. (一)关于NLP的概念和处理过程
  11. Shell条件表达式
  12. chrome 安装 gliffy插件以及使用
  13. mysql手册05_存储过程和存储函数
  14. STM32 HAL库学习笔记3-HAL库外设驱动框架概述
  15. 看板工具 Wekan 常见问题汇总
  16. 基于PHP的学生在线考试管理系统
  17. 西门子S7通信案例分享
  18. Linux命令——系统管理
  19. Spring中的IOC介绍
  20. UnityStandardAsset工程、源码分析_7_第三人称场景[玩家控制]_人物逻辑

热门文章

  1. ELK安装( Elasticsearch、Logstash、Kibana)
  2. Houdini11:材质
  3. Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
  4. linux下编译libyuv,Android ndk cmake编译libyuv
  5. 电路元件伏安特性的测量与multisim仿真(附工程文件)
  6. JAVA写一个小型超市管理系统,Java语言课程设计-小型超市管理系统
  7. 【浅谈】日文的文本等出现乱码的情况以及解决办法
  8. 全国地区 mysql表_2017全国省市区数据库-2017全国省市区数据库下载 官方版 - 河东下载站...
  9. 计算机网络体系结构综述(上)
  10. ib网卡命令_InfiniBand 网卡测试