如何保存远程文件到本机?我的为什么不能保存大文件?谢谢

sadfasfasd
06-06-06 1 172

想要根据文件地址保存远程文件到本机,但一保存超过30k的文件就会有损坏,为什么?怎么解决?

代码如下:

<%@ page import="java.io.*"%>
<%@ page import="java.io.Writer"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.Properties"%>
<%
System.out.println("start");

//?程文件路径
String s1 = "http://news.xinhuanet.com/photo/2006-06/05/xinsrc_26206030515427031430915.jpg";
//本地存放路径
String s2 = "D:\\mili\\"+System.currentTimeMillis()+".jpg";

URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(s2);

try{

urlfile = new URL(s1);
httpUrl = (HttpURLConnection)urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
}catch(Exception e){
System.out.println(e.toString());
}

try{
bos = new BufferedOutputStream(new FileOutputStream(f));;
byte[] b = new byte[4096];
while(bis.read(b)!=-1) {
bos.write(b);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
try{
bos.flush();
bis.close();
httpUrl.disconnect();
System.out.println("program completed");
}catch(Exception e){
System.out.println(e.toString());
}
}

%>


tjmzq
2006-06-08 17:13

没有设置表头吧。
// 以下是用С桑澹悖澹
response.setContentType("application/msexcel;charset=UTF-16");
response.setHeader("Content-Disposition", "attachment; filename=\""+ excelName + "\"");