文件下载,以流的方式
请求方:
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 block
e.printStackTrace();
}
}
}